Welcome Message

42 views
Skip to first unread message

itgc...@googlemail.com

unread,
Dec 11, 2009, 10:49:28 PM12/11/09
to pywebsocket
Hello, how can i sent a Welcome Message to the clients when they are
connected?

Takeshi Yoshino

unread,
Dec 17, 2009, 11:09:47 PM12/17/09
to pyweb...@googlegroups.com
Hi,

Please use msgutil.send_message to send out Welcome message in your transfer_data handler.
Check the example code for reference.

e.g.
msgutil.send_message(request, 'Welcome to our service')
and then, the client receives 'Welcome to our service' on evt.data on onmessage event.

Regards,

Takeshi

Kubi

unread,
Feb 27, 2013, 4:31:03 AM2/27/13
to pyweb...@googlegroups.com, tyos...@chromium.org
Hello, I have tried to send welcome message by editing echo handler from examples.
Unfortunately, it doesnt work. Could you explain to me what is wrong?
I have tried it like this:

_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)
and this:

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

Why none of this works (Im getting 1006 error code).

Takeshi Yoshino

unread,
Mar 5, 2013, 10:57:18 PM3/5/13
to Kubi, pyweb...@googlegroups.com
Hi,

The first one:
ws_stream is set after completion of handshake. You cannot use it inside extra handshake handlers.

The second one:
welcome is referenced before substitution inside the function definition. Use global to import reference to the global variable "welcome" into web_socket_transfer_data by

global welcome

at the beginning of web_socket_transfer_data.

Thanks
Reply all
Reply to author
Forward
0 new messages