Search code examples
pythonmod-pywebsocket

Pywebsocket error


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


Solution

  • I have found out this errors in my examples:

    1st code

    It is not possible to send any message to client during handshake

    web_socket_do_extra_handshake is called during the handshake after the headers are successfully parsed and WebSocket properties (ws_location, ws_origin, and ws_resource) are added to request. A handler can reject the request by raising an exception.

    2nd code

    Probably, I have had there bad indention in my code


    The easiest way to debug your websocket handlers is to run websocket in debug mode:

    # Run standalone server with "--log-level debug"
    ./standalone.py -p 12345 --allow-draft75 -d example --log-level debug
    
    # For apache module version, add LogLevel directive to httpd.conf
    LogLevel debug
    

    I'm running websockets as apache module and I can see parsing (and other) errors in apache's error log now