javawebsocketresponse

Java Websocket Client Not Receiving Response from Server


I've spent some time trawling through stack overflow and google, but alas I was unable to find the what I've been looking for (there may be something I've missed, sorry if there is). This is driving me insane as I'm so certain it all looks right, but obviously something is wrong and I just can't see it.

Basically, the handshake is fine. I receive and parse packets from the client fine.

When I try to send framed packet data back to the client, the client does nothing (not even an error). I've tried it on the latest Firefox and Chrome browsers using the example on echo.websocket.org and my own written javascript object. I can see the packet being sent and received via Wireshark on both the client and the server.

Here is an example of what I'm sending and a breakdown of the packet according to the RFC 6455 standards.

        Entire Response Packet Sent : 
            00 16 d3 65 d0 50 f4 6d 
            04 00 5f 84 08 00 45 00 
            00 32 5f e3 40 00 80 06 
            17 8c c0 a8 01 02 c0 a8 
            01 04 1f 91 05 18 fd 56 
            6f 03 ea 17 6d 4c 50 18 
            01 00 c0 fa 00 00 81 84 
            b2 3f de 11 83 0d ed 25

        Payload Data Unmasked Should Read : 1234

        Data : 
            81 84 B2 3F DE 11 83 0D ED 25

            FIN, RSV 1-3 and Op Code :

            0x81    1000 0001
                FIN     1 
                RSV1    0
                RSV2    0
                RSV3    0
                Opcode  1

            Mask and Payload Length :

            0x84    1000 0100
                Mask    1
                Length  4

            Mask Key :

            0xB2    1011 0010
            0x3F    0011 1111
            0xDE    1101 1110
            0x11    0001 0001

            Masked Payload Data : 

            0x83    1000 0011
            0x0D    0000 1101
            0xED    1110 1101
            0x25    0010 0101

            Unmasked Data

            Binary          Decimal         Hex         UTF-8

            1011 0010  
            1000 0011 ^
            -----------
            0011 0001       49              31          1

            0011 1111 
            0000 1101 ^
            -----------
            0011 0010       50              32          2

            1101 1110
            1110 1101 ^
            -----------
            0011 0011       51              33          3

            0001 0001 
            0010 0101 ^
            -----------
            0011 0100       52              34          4

My entire codebase is on GitHub, but here are the core files :

Data Frame Structure : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrame.java

Data Frame Response Builder : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrameResponse.java

Client Socket Connection Thread : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/threads/SocketClient.java

Whole repository : https://github.com/willitscale/simplejavawebsocket

Any help would be much appreciated!


Solution

  • Finally got to the bottom of it!

    Although the handshake appeared to work it actually screwed up the buffer on the client.

    My response to the client was a half and half, half from the RFC6455 and half from HYBI standards where the handshake key was in the body of the response.

    This is just a theory and I've not looked into it but I believe it stopped reading the buffer after the "\r\n\r\n", when I sent a message the first thing it must of read was the accept key left over from the buffer the first time.

                "Sec-WebSocket-Accept: " + Utils.generateKey( wsrRequest.socketKey ) + "\r\n" +
                "\r\n" + Utils.generateKey( wsrRequest.socketKey );
    

    Thanks for all your help, never fear it will not be in vein as you've opened my eyes to Autobahn Test Suite!

    Curious though Autobahn didn't pick this issue with the handshake up, nor did chrome or firebug? A bug perhaps?

    Why is it always me who finds these kinds of things /sigh

    Last week it was an actual bug with a payment service after days of investigating and thinking I was wrong, it was the service that didn't do what the documentation said. Luckily in this cause I was wrong!

    Either way, onward and upward I can finally continue my development. After about a day and half set back while I was convinced that java was screwing up the bytes, tcp packets or Websockets would only work on a non-blocking full duplex socket connection and not a half duplex socket connection.

    I'm my own worst enemy