【发布时间】:2020-03-25 15:37:02
【问题描述】:
我正在使用 javascript 创建客户端 websocket 并且丢失了将这个密钥“Sec-WebSocket-Key : dGhlIHNhbXBsZSBub25jZQ==”放在哪里 尝试使用下面的代码,但出现错误
index.html:14 未捕获的 DOMException:无法构造“WebSocket”:子协议“dGhlIHNhbXBsZSBub25jZQ==”无效。 在 WebSocketTest (file:///C:/handshake/index.html:14:16) 在:1:1
<script type = "text/javascript">
function WebSocketTest() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
//var ws = new WebSocket("ws://192.168.1.104:9001");
var ws = new WebSocket("wss://192.168.1.104:9001", "dGhlIHNhbXBsZSBub25jZQ==");
ws.onopen = function() {
//test open connectin
alert("Connection Established...");
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
alert("Message is received...");
};
ws.onclose = function() {
// websocket is closed.
alert("Connection is closed...");
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
<body>
<div id = "sse">
<a href = "javascript:WebSocketTest()">Run WebSocket</a>
<p> Tested </p>
</div>
</body>
</body>
【问题讨论】:
标签: javascript html websocket