From: Joachim Bauch Date: Tue, 18 Sep 2012 08:56:54 +0000 (+0200) Subject: Support sending ping frames, trigger callback on pong frames. X-Git-Tag: v3.0.0~272^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21552e6c3610f3c506592383ce6561a45a861ce1;p=thirdparty%2Ftornado.git Support sending ping frames, trigger callback on pong frames. --- diff --git a/tornado/websocket.py b/tornado/websocket.py index 9bc1e053b..628735518 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -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()