我试图在 JavaScript WebSocket 和用 VisualBasic .Net 编写的 TCP 服务器/客户端应用程序之间建立连接。我的问题是握手失败。我确实收到了来自本地网站的握手请求,但它不接受我的回复。
JavaScript 文件的代码:
<script type="text/javascript">
var ws;
function connect() {
if("WebSocket" in window) {
debugger;
ws = new WebSocket("ws://192.168.193.178:1925");
ws.onopen = function() {
alert("Connection established");
};
ws.onmessage = function(evt) {
var received_msg = evt.data;
alert("Message is received: " + received_msg);
};
ws.onerror = function(evt) {
alert("Error");
var received_msg = evt.data;
alert("Error: " + received_msg);
};
ws.onclose = function() {
// websocket is closed.
alert("Connection closed");
};
//ws.send("Test");
//ws.close();
} else {
alert("WebSocket NOT supported by your Browser!");
}
}
function disconnect() {
ws.close();
}
function send(message) {
ws.send(message);
alert("Sent: " + message);
}
</script>
VB.Net 代码输出:
Received:
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 192.168.193.178:1925
Origin: http://127.0.0.1:8020
Sec-WebSocket-Key: eGzO0afUD5jCeUdzdoxwjw==
Sec-WebSocket-Version: 13
Sent:
HTTP/1.1 101 Web Socket Protocol Handshake\r\n
Upgrade: WebSocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Origin: null\r\n
Sec-WebSocket-Accept: NzU3M2IwYzk0ZWFmYjg4MzMyZWI1ODhhZWI4NWUyZDE1YWU2YzhlNA==\r\n
\r\n
我只是无法让 WebSocket 接受握手,我希望任何人都可以帮助我。也许哈希生成包含错误?
编辑:
我现在得到了正确的 Sec-WebSocket-Accept String (dXOwyU6vuIMy61iK64Xi0VrmyOQ=),无论如何 WebSocket 似乎没有处理握手响应。我尝试使用 Chrome 开发人员工具对其进行调试,但我没有从中获得有用的信息。有小费吗?