WebSocket onmessage no se activa

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:


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.

preguntado el 06 de septiembre de 12 a las 17:09

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. -

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? -

@Amaan this is the server side code. The handshake works properly and the onopen event is fired correctly in the javascript. -

@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. -

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. -

1 Respuestas

This is the same answer as for your other question: El bit comprimido debe ser 0 al enviar un mensaje al cliente websocket

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


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

pitón 3:

>>> bytes([129, 19])

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] )

Un par de otras notas:

  • 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.

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.