I'm new to python and websockets and would like to learn more about how to use pywebsocket.
Currently, Im editing examples from pywebsocket package, unfortunately, I'm unable to understand my error in following code:
_GOODBYE_MESSAGE = u'Goodbye'
def web_socket_do_extra_handshake(request):
request.ws_stream.send_message("Welcome", binary=False) # Error line
pass # Always accept.
def web_socket_transfer_data(request):
while True:
line = request.ws_stream.receive_message()
if line is None:
return
if isinstance(line, unicode):
request.ws_stream.send_message("Sending", binary=False)
request.ws_stream.send_message(line, binary=False)
if line == _GOODBYE_MESSAGE:
return
else:
request.ws_stream.send_message(line, binary=True)
I wanted to send "Welcome" message back to client when connection is initialized. But when I send any message from Handshake I got my websocket closed with code 1006
Why is that so?
I thought the problem may be that, it is not possible to send any data during handshake, so I tried it this way:
_GOODBYE_MESSAGE = u'Goodbye'
welcome = True
def web_socket_do_extra_handshake(request):
pass # Always accept.
def web_socket_transfer_data(request):
while True:
if welcome:
welcome = False
request.ws_stream.send_message("Welcome !", binary=False)
line = request.ws_stream.receive_message()
if line is None:
return
if isinstance(line, unicode):
request.ws_stream.send_message("Sending", binary=False)
request.ws_stream.send_message(line, binary=False)
if line == _GOODBYE_MESSAGE:
return
else:
request.ws_stream.send_message(line, binary=True)
I wasn't successful in neither first or second case, could you explain what am I doing wrong? Thank you
PS: Im running mod_pywebsocket as module of Apache (not standalone), my python version is 2.6.6; I have tried to run both of my scripts using $python script_wsh.py
to check for syntax errors