Click here to Skip to main content
15,886,545 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
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????
Thanks.


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

this is to get the accept:
C#
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
C#
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:
JavaScript
function ToggleConnectionClicked() {
            if (SocketCreated && (ws.readyState == 0 || ws.readyState == 1)) {  // Web Socket connecting or connected
                ws.close();
            } else {
                Log("Trying to connect to the server ...");
                try {
                    ws = new WebSocket("ws://" + document.getElementById("Connection").value);
                    SocketCreated = true;
                } catch (ex) {
                    Log(ex, "ERROR");
                    return;
                }

                document.getElementById("ToggleConnection").innerHTML = "Disconnect";
                ws.onopen = function () {
                    alert("Socket has been opened!");
                }  
                ws.onmessage = WSonMessage;
                ws.onclose = WSonClose;
                ws.onerror = WSonError;
            }
        };
Posted
Updated 13-Sep-11 0:16am
v2
Comments
Sergey Alexandrovich Kryukov 13-Sep-11 1:31am    
What, no code? Seriously? Are you sure you came to right forum?
--SA
Caioketo 13-Sep-11 6:21am    
Im sorry for not posting the code, but now it is there.
Thanks
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900