Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WebSocket
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:
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
                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 12-Sep-11 12:50pm
Edited 13-Sep-11 1:16am
v2
Comments
SAKryukov at 13-Sep-11 1:31am
   
What, no code? Seriously? Are you sure you came to right forum?
--SA
Caioketo at 13-Sep-11 6:21am
   
Im sorry for not posting the code, but now it is there.
Thanks
Caioketo at 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 at 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
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 13 Sep 2011
Copyright © CodeProject, 1999-2014
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