]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Added option for triggering callbacks when PING frame is received 1938/head
authorArtur Stawiarski <artur@digitgaming.com>
Wed, 1 Feb 2017 14:24:25 +0000 (14:24 +0000)
committerArtur Stawiarski <artur@digitgaming.com>
Wed, 1 Feb 2017 14:24:25 +0000 (14:24 +0000)
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.

tornado/websocket.py

index 6e1220b3ecbcf6c13a71b8e07c020aff6ea15e49..1d89b73d8e4f90d67fffa3dfbec1dc644ed0b353 100644 (file)
@@ -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)