我试图在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字符串(dXOwyU6vuIMy61iK64Xi0VrmyOQ =),无论如何,WebSocket似乎都没有处理握手响应。我尝试使用Chrome开发者工具对其进行调试,但我没有从中获取有用的信息。有什么提示吗?
答案 0 :(得分:2)
有一件事立刻突出。服务器响应中的Sec-WebSocket-Accept值比正确的值长得多。实际上,该密钥的正确值应为“dXOwyU6vuIMy61iK64Xi0VrmyOQ =”。我的猜测是你在SHA1结果的字符串表示上进行base-64编码。编码应在SHA1结果的字节表示上完成。