1

I'm writing a websocket server in python, and I've run into a problem when sending messages back to the client. Using chrome's javascript console, I can get as far as finding this error message:

Compressed bit must be 0 if no negotiated deflate-frame extension

But farther than that I can't find any documentation on what that actually means. You can find the start of this problem (and the code) here. I'm kind of at a loss. I've tried playing around with the first byte, changing it to all 0's, but according to this, the second bit, which is 0 in my case, should be the "compressed bit" if this documentation is correct;

"One reserved bit RSV1 in the WebSocket frame header is allocated to control application of compression for each message."

So I'm not entirely sure where to go from here. In firefox the error is simply a "The connection to ws://localhost:8080/ was interrupted while the page was loading." error.

EDIT: Here is the parse code when receiving a message:

def parse( self, data, client_socket ):
    display_server_message( "Received message:\n%s" % str( data ) )
    byte_array = bytearray( data )
    mask = []

    message_string = ""
    for i in range( 0, len( byte_array ) ):
        b = byte_array[ i ]
        if i > 0 and i < 2:
            b = b & 0b01111111
        if i > 1 and i < 6:
            mask.append( byte_array[ i ] )
        if i > 5:
            b = b ^ mask[ (i - 2) % 4 ]
            message_string = message_string + chr( b )

    display_server_message( "Decoded Message:\n%s" % message_string )
    if ( message_string == "a test message" ):
        time.sleep( 5 )
        self.send_message( "Hello, how are you?", client_socket )


    pat = re.compile( "Sec-WebSocket-Key: (\S+)", re.MULTILINE )

    for match in pat.findall( data ):
        magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

        message = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n" % base64.standard_b64encode( hashlib.sha1( match + magic_string ).digest() )

        client_socket.send( message )
        display_server_message( "Message Sent:\n%s" % message )
Community
  • 1
  • 1
Pete.Mertz
  • 1,302
  • 2
  • 18
  • 34
  • 1
    Can you either post the complete code to your server or grab a wireshark/tcpdump capture of the wire. Either one should point to the problem pretty clearly. – kanaka Sep 06 '12 at 22:53
  • @kanaka added the parsing/header code – Pete.Mertz Sep 07 '12 at 13:41
  • The complete runnable code (to a pastebin of github gist) is what I'm after (so that I can run it and quickly tell you what's wrong). Or a wireshark capture which would allow me to see exactly what's wrong with the message. – kanaka Sep 07 '12 at 14:00
  • @kanaka sorry, I got what you mean now, [here](http://pastebin.com/7jDvBzGB) is the server code and [here](http://pastebin.com/viZLcbr1) is the wireshark transaction. – Pete.Mertz Sep 07 '12 at 15:12

1 Answers1

1

This is the same answer as for your other question: WebSocket onmessage not firing

You're problem is that you are using the bytes function in python 2.X but expecting it to work the way it does in python 3.X

python2.6.6:

>>> bytes([129, 19])
'[129, 19]'

python 3:

>>> bytes([129, 19])
b'\x81\x13'

By replacing the send with the following in your server code it works for me in python2.X:

client_socket.send( "".join( [chr(c) for c in formatted_bytes] )

A couple of other notes:

  • The code you linked to is adding 1 to the message length which is incorrect.

  • You should really have the handshake code separate from the normal message parsing code. If somebody happened to send a message which contained "Sec-WebSocket-Key: " you would end up corrupting the stream by sending a raw handshake back again.

Community
  • 1
  • 1
kanaka
  • 70,845
  • 23
  • 144
  • 140
  • I'd actually just figured out the format part myself, frustrating... anyway, the actual problem was, as you said, the adding 1, because the browser was still waiting for another byte. Very frustrating! Thanks a ton! I'll mark both answers as correct. – Pete.Mertz Sep 07 '12 at 16:08