2

I'm trying to implement my own websocket server and am running into a bit of trouble with the onmessage function in the JavaScript. It just won't fire! I put an alert into it to trigger whenever it was called and the alert never comes up. I've tried on both firefox and chrome to no avail.

As far as I can tell, my server is sending the correct message as well, when I translate it into hex, it matches perfectly with the example in section 5.7 of the documentation here:

http://datatracker.ietf.org/doc/rfc6455/?include_text=1

So I'm not entirely sure what I'm doing wrong. The handshake works fine and so does the close. Here is my code for sending a message currently:

formatted_bytes = []
formatted_bytes.append( 129 )

message_bytes = bytearray( message )

data_start = 0

if ( len( message_bytes ) <= 125 ):
    formatted_bytes.append( len( message_bytes ) )
    start = 2

for byte in message_bytes:
    formatted_bytes.append( byte )

client_socket.send( bytes( formatted_bytes ) )

The server is written in python, forgot to mention that.

kanaka
  • 70,845
  • 23
  • 144
  • 140
Pete.Mertz
  • 1,302
  • 2
  • 18
  • 34
  • I think we'll need to see the server side code. I don't know much about this, but I think it may be because of a missing Web Socket Handshake. – Some Guy Sep 06 '12 at 16:17
  • Try opening your javascript developer console and see if you get any errors/warnings. Also, are you certain the handshake is happening correctly (i.e. does the onopen handler fire?) Do you have an onclose handler to make sure that something isn't going wrong right after connection? – kanaka Sep 06 '12 at 16:49
  • @Amaan this is the server side code. The handshake works properly and the onopen event is fired correctly in the javascript. – Pete.Mertz Sep 06 '12 at 18:12
  • @kanaka When I open the javascript developer console nothing comes up except for the headers from the handshake (both sides) and the first message sent by the client. So it would seem that the javascript isn't throwing an error, it's just not seeing or registering any part of the message! EDIT: the headers appear in the "network" section of the Developer Tools in chrome, I get no messages in errors or logs in the javascript window. – Pete.Mertz Sep 06 '12 at 18:13
  • I've gotten the browser to throw an error by manually sending the trigger message in the console, e.g. saying "ws.send( "a test message" )" and then waiting for the response. When the server responds, the browser throws the error "The connection to ws://localhost:8080/ was interrupted while the page was loading." and then the connection closes from the client side. I'm not exactly sure what's going on here. – Pete.Mertz Sep 06 '12 at 19:21
  • The complete server code or alternately a wireshark/tcpdump capture of the data on the wire would really help here. – kanaka Sep 06 '12 at 22:53

1 Answers1

4

This is the same answer as for your other question: Compressed bit must be 0 when sending a message to websocket client

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