]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Support sending ping frames, trigger callback on pong frames.
authorJoachim Bauch <mail@joachim-bauch.de>
Tue, 18 Sep 2012 08:56:54 +0000 (10:56 +0200)
committerBen Darnell <ben@bendarnell.com>
Thu, 20 Sep 2012 06:12:28 +0000 (23:12 -0700)
tornado/websocket.py

index 9bc1e053bc87e54de130ba63dd8deaa26b039f15..6287355188c1c891ff3fc9b1572edc4ae42bd186 100644 (file)
@@ -172,6 +172,14 @@ class WebSocketHandler(tornado.web.RequestHandler):
         """
         raise NotImplementedError
 
+    def ping(self, data):
+        """Send ping frame to the remote end."""
+        self.ws_connection.write_ping(data)
+
+    def on_pong(self, data):
+        """Invoked when the response to a ping frame is received."""
+        pass
+
     def on_close(self):
         """Invoked when the WebSocket is closed."""
         pass
@@ -420,6 +428,10 @@ class WebSocketProtocol76(WebSocketProtocol):
         assert isinstance(message, bytes_type)
         self.stream.write(b("\x00") + message + b("\xff"))
 
+    def write_ping(self, data):
+        """Send ping frame."""
+        raise ValueError("Ping messages not supported by this version of websockets")
+
     def close(self):
         """Closes the WebSocket connection."""
         if not self.server_terminated:
@@ -525,6 +537,11 @@ class WebSocketProtocol13(WebSocketProtocol):
         assert isinstance(message, bytes_type)
         self._write_frame(True, opcode, message)
 
+    def write_ping(self, data):
+        """Send ping frame."""
+        assert isinstance(data, bytes_type)
+        self._write_frame(True, 0x9, data)
+
     def _receive_frame(self):
         self.stream.read_bytes(2, self._on_frame_start)
 
@@ -632,7 +649,7 @@ class WebSocketProtocol13(WebSocketProtocol):
             self._write_frame(True, 0xA, data)
         elif opcode == 0xA:
             # Pong
-            pass
+            self.async_callback(self.handler.on_pong)(data)
         else:
             self._abort()