]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
write_message method of WebSocketClientConnection now accepts dict as input
authorkriskros341 <krzysztofczuba884@gmail.com>
Wed, 27 Jan 2021 13:24:17 +0000 (14:24 +0100)
committerkriskros341 <krzysztofczuba884@gmail.com>
Wed, 27 Jan 2021 13:24:17 +0000 (14:24 +0100)
tornado/websocket.py

index d2c6a427aacad25ac56248ddbabd588af03ec85f..33d7a94d45b4b8f9b820df7fa27bc7338af4cb5c 100644 (file)
@@ -1072,13 +1072,15 @@ class WebSocketProtocol13(WebSocketProtocol):
         return self.stream.write(frame)
 
     def write_message(
-        self, message: Union[str, bytes], binary: bool = False
+        self, message: Union[str, bytes, dict], binary: bool = False
     ) -> "Future[None]":
         """Sends the given message to the client of this Web Socket."""
         if binary:
             opcode = 0x2
         else:
             opcode = 0x1
+        if isinstance(message, dict):
+            message = tornado.escape.json_encode(message)
         message = tornado.escape.utf8(message)
         assert isinstance(message, bytes)
         self._message_bytes_out += len(message)
@@ -1493,7 +1495,7 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection):
         future_set_result_unless_cancelled(self.connect_future, self)
 
     def write_message(
-        self, message: Union[str, bytes], binary: bool = False
+        self, message: Union[str, bytes, dict], binary: bool = False
     ) -> "Future[None]":
         """Sends a message to the WebSocket server.