Sends the given message to the client of this Web Socket. The message may be either a string or a dict (which will be encoded as json). If the ``binary`` argument is false, the message will be sent as utf8; in binary mode any byte string is allowed. If the
(
self, message: Union[bytes, str, Dict[str, Any]], binary: bool = False
)
| 309 | ) |
| 310 | |
| 311 | def write_message( |
| 312 | self, message: Union[bytes, str, Dict[str, Any]], binary: bool = False |
| 313 | ) -> "Future[None]": |
| 314 | """Sends the given message to the client of this Web Socket. |
| 315 | |
| 316 | The message may be either a string or a dict (which will be |
| 317 | encoded as json). If the ``binary`` argument is false, the |
| 318 | message will be sent as utf8; in binary mode any byte string |
| 319 | is allowed. |
| 320 | |
| 321 | If the connection is already closed, raises `WebSocketClosedError`. |
| 322 | Returns a `.Future` which can be used for flow control. |
| 323 | |
| 324 | .. versionchanged:: 3.2 |
| 325 | `WebSocketClosedError` was added (previously a closed connection |
| 326 | would raise an `AttributeError`) |
| 327 | |
| 328 | .. versionchanged:: 4.3 |
| 329 | Returns a `.Future` which can be used for flow control. |
| 330 | |
| 331 | .. versionchanged:: 5.0 |
| 332 | Consistently raises `WebSocketClosedError`. Previously could |
| 333 | sometimes raise `.StreamClosedError`. |
| 334 | """ |
| 335 | if self.ws_connection is None or self.ws_connection.is_closing(): |
| 336 | raise WebSocketClosedError() |
| 337 | if isinstance(message, dict): |
| 338 | message = tornado.escape.json_encode(message) |
| 339 | return self.ws_connection.write_message(message, binary=binary) |
| 340 | |
| 341 | def select_subprotocol(self, subprotocols: List[str]) -> Optional[str]: |
| 342 | """Override to implement subprotocol negotiation. |