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 )