[uWSGI] How to get and store a websocket session?

Roberto De Ioris roberto at unbit.it
Tue Apr 21 05:35:08 CEST 2015


>
> Hello people,
> I can do ws = environ['wsgi.websocket'] when I use gevent's wsgi server,
> but not when I use uwsgi. So how do I get and store a websocket session in
> uwsgi? 
>
> from gevent import monkey; monkey.patch_all()
> from gevent.pywsgi import WSGIServer
> import uwsgi
>
> class WebSocketApp(object):
>     def __init__(self):
>         self.my_dict = {}
>         self.id_count = 0
>
>     def __call__(self, environ, start_response):
>             uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'],
> environ.get('HTTP_ORIGIN', ''))
>
>             #works when using gevent's WSGI server but not when using
> uwsgi
>             ws = environ['wsgi.websocket']
>
>             self.my_dict[self.id_count] = ws
>             self.id_count += 1
>             while True:
>                 try:
>                     msg = uwsgi.websocket_recv()
>                     uwsgi.websocket_send(msg)
>                 except IOError, e:
>                     return ""
>
>     def send_msg(self, id, msg):
>         self.my_dict[self.id].websocket_send(msg)
>
> application = WebSocketApp()
> Any Help would be much appreciated.
> Tim
> _______________________________________________
> uWSGI mailing list
> uWSGI at lists.unbit.it
> http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi
>


They are two different (and unrelated) ways, you cannot use both.

There is no thing like a 'websocket' session in the uWSGI websocket api,
once a thread, a process or a greenlet is marked as a websocket request
(using the handshake function) you can start using send and recv functions
transparently in that process/thread/greenlet.

This is transparent even when you use uWSGI in gevent mode.

-- 
Roberto De Ioris
http://unbit.com


More information about the uWSGI mailing list