Click here to Skip to main content
13,044,428 members (115,688 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm trying to test a websocket server in C#.
I made the handshake allright, the server thinks the client is connected, but however in the client, it never executes the onopen function. It just ignores.

Client Handshake:
GET /test HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8181
Sec-WebSocket-Origin: http://localhost:8080
Sec-WebSocket-Key: WSPssvnny/8wcttUvZ0KvA==
Sec-WebSocket-Version: 8

Server Handshake:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: RN219+qVLgOwA+oRF4NG9aweYCQ=
Sec-WebScoket-Protocol: chat

Someone can tells me if there is anything wrong in the handshake, or why it doesnt execute the onopen funcion????

Edit: I'm sorry i'll add the code

this is to get the accept:
public static String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
            const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            String secWebSocketAccept = String.Empty;
            // 1. Combine the request Sec-WebSocket-Key with magic key.
            String ret = secWebSocketKey + MagicKEY;
            // 2. Compute the SHA1 hash
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
            // 3. Base64 encode the hash
            secWebSocketAccept = Convert.ToBase64String(sha1Hash);
            return secWebSocketAccept;

and to send it
string handshakeTeste = "Sec-WebSocket-Accept: " + ComputeWebSocketHandshakeSecurityHash09(key);
Handshake += handshakeTeste + Environment.NewLine + "Sec-WebSocket-Protocol: chat"; 
ConnectionSocket.BeginSend(HandshakeText, 0, HandshakeText.Length, 0, HandshakeFinished, null);

The server executes the "HandshakeFinished" method.

and the client code:
function ToggleConnectionClicked() {
            if (SocketCreated && (ws.readyState == 0 || ws.readyState == 1)) {  // Web Socket connecting or connected
            } else {
                Log("Trying to connect to the server ...");
                try {
                    ws = new WebSocket("ws://" + document.getElementById("Connection").value);
                    SocketCreated = true;
                } catch (ex) {
                    Log(ex, "ERROR");
                document.getElementById("ToggleConnection").innerHTML = "Disconnect";
                ws.onopen = function () {
                    alert("Socket has been opened!");
                ws.onmessage = WSonMessage;
                ws.onclose = WSonClose;
                ws.onerror = WSonError;
Posted 12-Sep-11 11:50am
Updated 13-Sep-11 0:16am
SAKryukov 13-Sep-11 1:31am
What, no code? Seriously? Are you sure you came to right forum?
Caioketo 13-Sep-11 6:21am
Im sorry for not posting the code, but now it is there.
Caioketo 13-Sep-11 19:11pm
I debugged on chrome 14 and its like the response doesn't exists.
But in server side it executes the method after BeginSend(), wich means it sent.
Member 7425520 30-Sep-11 7:41am
I've got the same issue, trying to solve it, and unfortunately can't.
Doing almost the same way as you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Sep 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100