views:

154

answers:

2

Hi folks,

I'm using long polling for a chat with gevent. I'm using Event.wait() when waiting for new messages to be posted on the chat.


I would like to handle the occasion a client disconnects with some functionality:

e.g. Return "client has disconnected" as a message for other chat users


Is this possible? =)

+1  A: 

This is a total stab in the dark as I've never used gevent but wouldn't a client disconnect simply be when the socket is closed. So maybe something like this would work:

if not Event.wait():
    # Client has disconnected, do your magic here!
    return Chat({'status': 'client x has disconnected'})
Daveyjoe
@Dave you might have struck a ninja with that stab, let me check! =D Thanks for that!
RadiantHex
+1  A: 

This depends on which WSGI server you use. AFAIK gevent.wsgi will not notify your handler in any way when the client closes the connection, because libevent-http does not do that. However, with gevent.pywsgi it should be possible. You'll probably need to start an additional greenlet to monitor the socket condition and somehow notify the greenlet that runs the handler, e.g. by killing it. I could be missing an easier way to do this though.

Denis Bilenko
@denis: Thank you so much for your thought, I really appreciate it. This is something I would REALLY love to know! =) #gevent on freenode seems pretty silent these days... Thanks for your reply denis!
RadiantHex
I wonder, would it be a terrible idea to raise some exception in the WSGI application asynchronously if a client has disconnected?
Denis Bilenko