2

I'm trying to implement a simple web socket server and client. This is my first time looking at them, so sorry if this question is obvious.

I'm using Chrome v15, which I believe uses the 09 (version 8) protocol?

I just don't seem to be able to get them to connect. The request I'm getting from the browser is:

GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8181
Sec-WebSocket-Origin: http://localhost:51990
Sec-WebSocket-Key: JFAw5PLk45TodN2ERD1ePA==
Sec-WebSocket-Version: 8
Cookie: size=4; CP.mode=B; ASP.NET_SessionId=zzx3d3ajepwwycqjaj1nyex4; .ASPXAUTH=781D3791DC2483756B3DA9FA8E031A9BACD357EBD8FA23B7BCB8BDA6526F28F77FC798A0D4BEC4E2B166700B5C08FA60CBF588D292BFC1D050C9B034522C93ACBEF28BC6D51FDC5B40F6050F03758DA1A3E4D3F7484BC4F7DA3602A5FAFD3023C8D4D5929B69F88DB417CA6F366A83F334807818E2C07E23C0D0993F25B3C9BDE02A; name=Test

I've noticed that the Sec-WebSocket-Protocol header is missing.

The response I'm sending looks like this:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: VAuGgaNDB/reVQpGfDF8KXeZx5o=
Sec-WebSocket-Protocol: chat

Can anyone shed some light on what I'm doing wrong please?

EDIT: The code I'm using to generate the accept key is:

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

EDIT: This is the response code:

        StringBuilder mResponse = new StringBuilder();
        mResponse.Append("HTTP/1.1 101 Switching Protocols" + Environment.NewLine);
        mResponse.Append("Upgrade: WebSocket" + Environment.NewLine);
        mResponse.Append("Connection: Upgrade" + Environment.NewLine);
        mResponse.Append(String.Format("Sec-WebSocket-Accept: {0}", ComputeWebSocketHandshakeSecurityHash09(secKey)) + Environment.NewLine);


        // Build the response for the client
        byte[] HandshakeText = Encoding.UTF8.GetBytes(mResponse.ToString());

        logger.Log("");
        logger.Log("Sending handshake ...");

        ConnectionSocket.BeginSend(HandshakeText, 0, HandshakeText.Length, 0, HandshakeFinished, null);
RexMundi
  • 21
  • 1
  • 3
  • Unless I'm mistaking, your accept key seems to be incorrect. It should be `tixAAehY4D7GQBBUYkVMLZ3yccE=` according to the input. How are you calculating it? (Could you post the code for that please?) – pimvdb Nov 11 '11 at 11:13
  • ok, figured it out. the 'Sec-WebSocket-Protocol: chat' was not required in this version. It connects now, but only calls the onclose method clientside, not on open. Any Ideas? – RexMundi Nov 11 '11 at 14:14
  • That happens when the response was not correct in some way. Did you ensure adding two `CRLF`s? – pimvdb Nov 11 '11 at 14:33
  • Yeah, adding Environment.Newline. I've added the code i'm using above. – RexMundi Nov 11 '11 at 15:05
  • Right, getting there! Got the onopen (clientside) function to call. Seemed it need two CRLF's at the end of the response. However, it not disconnects as soon as it connects. – RexMundi Nov 11 '11 at 15:21
  • It does or does not disconnect? If it does - does it disconnect when sending something or right after the `onopen`? – pimvdb Nov 11 '11 at 15:57
  • Sorry, it does disconnect. Appears to be on sending a message from the server. I have tried wrapping the text with 0x00 and 0xff. – RexMundi Nov 11 '11 at 16:08
  • The message protocol has changed completely as well (the `00 ff` is of the previous WebSocket version) - have a look at some pseudocode I posted previously for decoding client->server messages: http://stackoverflow.com/q/7045885. For server->client messsages, you need to follow the specification as well. Could you try sending the bytes `81 03 61 62 63` (hex) and see whether you receive `"abc"` on the client? If that works I'll post some code for server->client messages if you want. – pimvdb Nov 11 '11 at 16:14
  • `Environment.Newline` is *wrong* on non-Windows platforms. Use `\r\n`. – user253751 Apr 16 '14 at 10:49

1 Answers1

0

I managed to connect but when I send messages I cannot decode it. Take a look at this question.

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470