Client-side websocket support. Takes a url and returns a Future whose result is a `WebSocketClientConnection`. ``compression_options`` is interpreted in the same way as the return value of `.WebSocketHandler.get_compression_options`. The connection supports two styles of opera
(
url: Union[str, httpclient.HTTPRequest],
callback: Optional[Callable[["Future[WebSocketClientConnection]"], None]] = None,
connect_timeout: Optional[float] = None,
on_message_callback: Optional[Callable[[Union[None, str, bytes]], None]] = None,
compression_options: Optional[Dict[str, Any]] = None,
ping_interval: Optional[float] = None,
ping_timeout: Optional[float] = None,
max_message_size: int = _default_max_message_size,
subprotocols: Optional[List[str]] = None,
)
| 1579 | |
| 1580 | |
| 1581 | def websocket_connect( |
| 1582 | url: Union[str, httpclient.HTTPRequest], |
| 1583 | callback: Optional[Callable[["Future[WebSocketClientConnection]"], None]] = None, |
| 1584 | connect_timeout: Optional[float] = None, |
| 1585 | on_message_callback: Optional[Callable[[Union[None, str, bytes]], None]] = None, |
| 1586 | compression_options: Optional[Dict[str, Any]] = None, |
| 1587 | ping_interval: Optional[float] = None, |
| 1588 | ping_timeout: Optional[float] = None, |
| 1589 | max_message_size: int = _default_max_message_size, |
| 1590 | subprotocols: Optional[List[str]] = None, |
| 1591 | ) -> "Awaitable[WebSocketClientConnection]": |
| 1592 | """Client-side websocket support. |
| 1593 | |
| 1594 | Takes a url and returns a Future whose result is a |
| 1595 | `WebSocketClientConnection`. |
| 1596 | |
| 1597 | ``compression_options`` is interpreted in the same way as the |
| 1598 | return value of `.WebSocketHandler.get_compression_options`. |
| 1599 | |
| 1600 | The connection supports two styles of operation. In the coroutine |
| 1601 | style, the application typically calls |
| 1602 | `~.WebSocketClientConnection.read_message` in a loop:: |
| 1603 | |
| 1604 | conn = yield websocket_connect(url) |
| 1605 | while True: |
| 1606 | msg = yield conn.read_message() |
| 1607 | if msg is None: break |
| 1608 | # Do something with msg |
| 1609 | |
| 1610 | In the callback style, pass an ``on_message_callback`` to |
| 1611 | ``websocket_connect``. In both styles, a message of ``None`` |
| 1612 | indicates that the connection has been closed. |
| 1613 | |
| 1614 | ``subprotocols`` may be a list of strings specifying proposed |
| 1615 | subprotocols. The selected protocol may be found on the |
| 1616 | ``selected_subprotocol`` attribute of the connection object |
| 1617 | when the connection is complete. |
| 1618 | |
| 1619 | .. versionchanged:: 3.2 |
| 1620 | Also accepts ``HTTPRequest`` objects in place of urls. |
| 1621 | |
| 1622 | .. versionchanged:: 4.1 |
| 1623 | Added ``compression_options`` and ``on_message_callback``. |
| 1624 | |
| 1625 | .. versionchanged:: 4.5 |
| 1626 | Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size`` |
| 1627 | arguments, which have the same meaning as in `WebSocketHandler`. |
| 1628 | |
| 1629 | .. versionchanged:: 5.0 |
| 1630 | The ``io_loop`` argument (deprecated since version 4.1) has been removed. |
| 1631 | |
| 1632 | .. versionchanged:: 5.1 |
| 1633 | Added the ``subprotocols`` argument. |
| 1634 | """ |
| 1635 | if isinstance(url, httpclient.HTTPRequest): |
| 1636 | assert connect_timeout is None |
| 1637 | request = url |
| 1638 | # Copy and convert the headers dict/object (see comments in |