detect client disconnection on server side

127 views
Skip to first unread message

Jojo

unread,
Dec 22, 2010, 5:25:38 AM12/22/10
to pywebsocket
hi guys!
how can i detect a client disconnection in my handler?

thank you and good work!

Takeshi Yoshino

unread,
Dec 24, 2010, 1:58:26 AM12/24/10
to pyweb...@googlegroups.com
Hi Jojo,

We've made some change about error/disconnection handling recently.

On the latest revision of the trunk,
- on unexpected TCP disconnection : receive_message throws msgutil.ConnectionTerminatedException, send_message throws socket.error with "[Errno 32] Broken pipe" (Linux) / "[Errno 10053] An established connection was aborted ..." (Windows)
- on disconnection due to receiving an invalid frame : receive_message throws msgutil.InvalidFrameException
- on disconnection due to receiving closing handshake : receive_message returns None

InvalidFrameException is sub-class of ConnectionTerminatedException, so you can simply catch ConnectionTerminatedException for handling unexpected disconnection and check None for clean closure.

On the latest release 0.5.2,
- on unexpected TCP disconnection : receive_message throws msgutil.MsgutilException, send_message throws socket.error
- on disconnection due to receiving closing handshake (HyBi 76 and later) : receive_message throws msgutil.ConnectionTerminatedException

Note that in this release, ConnectionTerminatedException can be thrown in some other cases, too.
- receive_message is called after receiving closing handshake
- send_message is called after sending closing handshake (i.e. after calling msgutil.close_connection(request))
On the latest revision of the trunk, these error cases are handled by catching msgutil.BadOperationException instead.

We'll keep making this kind of refactoring on exception definitions to clarify and tell the cause of error/disconnection to handler. Please watch msgutil.py for update on exception definition.

Best


Takeshi
Reply all
Reply to author
Forward
0 new messages