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
)
| 333 | ) |
| 334 | |
| 335 | def write_message( |
| 336 | self, message: Union[bytes, str, Dict[str, Any]], binary: bool = False |
| 337 | ) -> "Future[None]": |
| 338 | """Sends the given message to the client of this Web Socket. |
| 339 | |
| 340 | The message may be either a string or a dict (which will be |
| 341 | encoded as json). If the ``binary`` argument is false, the |
| 342 | message will be sent as utf8; in binary mode any byte string |
| 343 | is allowed. |
| 344 | |
| 345 | If the connection is already closed, raises `WebSocketClosedError`. |
| 346 | Returns a `.Future` which can be used for flow control. |
| 347 | |
| 348 | .. versionchanged:: 3.2 |
| 349 | `WebSocketClosedError` was added (previously a closed connection |
| 350 | would raise an `AttributeError`) |
| 351 | |
| 352 | .. versionchanged:: 4.3 |
| 353 | Returns a `.Future` which can be used for flow control. |
| 354 | |
| 355 | .. versionchanged:: 5.0 |
| 356 | Consistently raises `WebSocketClosedError`. Previously could |
| 357 | sometimes raise `.StreamClosedError`. |
| 358 | """ |
| 359 | if self.ws_connection is None or self.ws_connection.is_closing(): |
| 360 | raise WebSocketClosedError() |
| 361 | if isinstance(message, dict): |
| 362 | message = tornado.escape.json_encode(message) |
| 363 | return self.ws_connection.write_message(message, binary=binary) |
| 364 | |
| 365 | def select_subprotocol(self, subprotocols: List[str]) -> Optional[str]: |
| 366 | """Override to implement subprotocol negotiation. |