From: Artur Stawiarski Date: Wed, 1 Feb 2017 14:24:25 +0000 (+0000) Subject: Added option for triggering callbacks when PING frame is received X-Git-Tag: v4.5.0~30^2~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1938%2Fhead;p=thirdparty%2Ftornado.git Added option for triggering callbacks when PING frame is received In order to implement proper application-level handling of disconnections a developer may want to hook up into the standard "ping" mechanism of the websocket protocol. This is important for systems where both the client and the server need to keep track of disconnections, but based on client-intiated pings only. --- diff --git a/tornado/websocket.py b/tornado/websocket.py index 6e1220b3e..1d89b73d8 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -264,6 +264,10 @@ class WebSocketHandler(tornado.web.RequestHandler): """Invoked when the response to a ping frame is received.""" pass + def on_ping(self, data): + """Invoked when the a ping frame is received.""" + pass + def on_close(self): """Invoked when the WebSocket is closed. @@ -852,6 +856,7 @@ class WebSocketProtocol13(WebSocketProtocol): elif opcode == 0x9: # Ping self._write_frame(True, 0xA, data) + self._run_callback(self.handler.on_ping, data) elif opcode == 0xA: # Pong self._run_callback(self.handler.on_pong, data) @@ -1016,6 +1021,9 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection): def on_pong(self, data): pass + def on_ping(self, data): + pass + def get_websocket_protocol(self): return WebSocketProtocol13(self, mask_outgoing=True, compression_options=self.compression_options)