Commit 09919006 authored by Taddeüs Kroes's avatar Taddeüs Kroes

WebSocket.receive_forever() now handles exceptions without crashing

parent 822bfe72
......@@ -87,6 +87,9 @@ class Client(WebSocket):
def onclose(self, code, reason):
self.server.remove_client(self, code, reason)
def onexception(self, e):
logging.error(format_exc(e))
def __str__(self):
return '<Client at %s:%d>' % self.address
......
......@@ -128,15 +128,18 @@ class WebSocket(object):
of multiple data frames, but this is not visible for onmessage().
Control messages (or control frames) are handled automatically.
"""
try:
while True:
while True:
try:
self.onmessage(self, self.receive_message())
if self.received_close_params is not None:
self.handle_close(*self.received_close_params)
break
except SocketClosed:
self.onclose(None, '')
except SocketClosed:
self.onclose(None, '')
break
except Exception as e:
self.onexception(e)
def run_threaded(self, daemon=True):
"""
......@@ -221,3 +224,9 @@ class WebSocket(object):
Called when the socket is closed by either end point.
"""
pass
def onexception(self, e):
"""
Handle a raised exception.
"""
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment