Steppschuh
Steppschuh

Reputation: 409

WebSockets Handshake not working

Im trying to get a connection between a JavaScript WebSocket and a TCP Server/Client applicat written in VisualBasic .Net . My problem is that the handshake fails. I do get a handshake request from the local website, but it does not accept my response.

The code of the JavaScript file:

<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>

The VB.Net code output:

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

I just cant get the WebSocket to accept the handshake, I hope anyone can help me out. Maybe the hash-generation contains errors?

Edit:

I now get the correct Sec-WebSocket-Accept String (dXOwyU6vuIMy61iK64Xi0VrmyOQ=), anyway the WebSocket seems not to handle the handshake response. I tried debugging it with the Chrome Developer Tools, but I don't get usefull information from it. Any tips?

Upvotes: 0

Views: 4021

Answers (1)

Aaron
Aaron

Reputation: 136

One thing sticks out immediately. The Sec-WebSocket-Accept value in the server response is much longer than what a correct value looks like. In fact, the correct value for that Key should be "dXOwyU6vuIMy61iK64Xi0VrmyOQ=". My guess is you're doing the base-64 encoding on the string representation of the SHA1 result. The encoding should be done on the byte representation of SHA1 result.

Upvotes: 2

Related Questions