MCPcopy
hub / github.com/tornadoweb/tornado / write_message

Method write_message

tornado/websocket.py:335–363  ·  view source on GitHub ↗

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
    )

Source from the content-addressed store, hash-verified

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.

Callers 15

write_messageMethod · 0.45
on_messageMethod · 0.45
openMethod · 0.45
on_messageMethod · 0.45
openMethod · 0.45
on_messageMethod · 0.45
on_messageMethod · 0.45
openMethod · 0.45
on_messageMethod · 0.45
openMethod · 0.45
test_websocket_genMethod · 0.45

Calls 2

is_closingMethod · 0.45

Tested by 15

on_messageMethod · 0.36
openMethod · 0.36
on_messageMethod · 0.36
openMethod · 0.36
on_messageMethod · 0.36
on_messageMethod · 0.36
openMethod · 0.36
on_messageMethod · 0.36
openMethod · 0.36
test_websocket_genMethod · 0.36
test_binary_messageMethod · 0.36