Skip to content

Commit 0a89cc2

Browse files
committed
Drop separate client_websocket doc page
1 parent 7d53909 commit 0a89cc2

File tree

5 files changed

+191
-200
lines changed

5 files changed

+191
-200
lines changed

docs/client.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,51 @@ If no redirects occurred or ``allow_redirects`` is set to ``False``,
559559
history will be an empty sequence.
560560

561561

562+
.. _aiohttp-client-websockets:
563+
564+
WebSockets
565+
----------
566+
567+
.. versionadded:: 0.15
568+
569+
570+
:mod:`aiohttp` works with client websockets out-of-the-box.
571+
572+
You have to use the :meth:`aiohttp.ClientSession.ws_connect` coroutine
573+
for client websocket connection. It accepts a *url* as a first
574+
parameter and returns :class:`ClientWebSocketResponse`, with that
575+
object you can communicate with websocket server using response's
576+
methods::
577+
578+
session = aiohttp.ClientSession()
579+
async with session.ws_connect('http://example.org/websocket') as ws:
580+
581+
async for msg in ws:
582+
if msg.tp == aiohttp.MsgType.text:
583+
if msg.data == 'close cmd':
584+
await ws.close()
585+
break
586+
else:
587+
ws.send_str(msg.data + '/answer')
588+
elif msg.tp == aiohttp.MsgType.closed:
589+
break
590+
elif msg.tp == aiohttp.MsgType.error:
591+
break
592+
593+
If you prefer to establish *websocket client connection* without
594+
explicit :class:`~aiohttp.ClientSession` instance please use
595+
:func:`ws_connect()`::
596+
597+
async with aiohttp.ws_connect('http://example.org/websocket') as ws:
598+
...
599+
600+
601+
You **must** use the only websocket task for both reading (e.g ``await
602+
ws.receive()`` or ``async for msg in ws:``) and writing but may have
603+
multiple writer tasks which can only send data asynchronously (by
604+
``ws.send_str('data')`` for example).
605+
606+
562607
Timeouts
563608
--------
564609

docs/client_reference.rst

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,66 @@ Usage::
522522
:return: :class:`ClientResponse` or derived from
523523

524524

525+
.. coroutinefunction:: ws_connect(url, *, protocols=(), \
526+
timeout=10.0, connector=None, auth=None,\
527+
ws_response_class=ClientWebSocketResponse,\
528+
autoclose=True, autoping=True, loop=None,\
529+
origin=None, headers=None)
530+
531+
This function creates a websocket connection, checks the response and
532+
returns a :class:`ClientWebSocketResponse` object. In case of failure
533+
it may raise a :exc:`~aiohttp.errors.WSServerHandshakeError` exception.
534+
535+
:param str url: Websocket server url
536+
537+
:param tuple protocols: Websocket protocols
538+
539+
:param float timeout: Timeout for websocket read. 10 seconds by default
540+
541+
:param obj connector: object :class:`TCPConnector`
542+
543+
:param ws_response_class: WebSocketResponse class implementation.
544+
``ClientWebSocketResponse`` by default.
545+
546+
.. versionadded:: 0.16
547+
548+
:param bool autoclose: Automatically close websocket connection
549+
on close message from server. If `autoclose` is
550+
False them close procedure has to be handled manually
551+
552+
:param bool autoping: Automatically send `pong` on `ping` message from server
553+
554+
:param aiohttp.helpers.BasicAuth auth: BasicAuth named tuple that
555+
represents HTTP Basic Authorization
556+
(optional)
557+
558+
:param loop: :ref:`event loop<asyncio-event-loop>` used
559+
for processing HTTP requests.
560+
561+
If param is ``None`` :func:`asyncio.get_event_loop`
562+
used for getting default event loop, but we strongly
563+
recommend to use explicit loops everywhere.
564+
565+
:param str origin: Origin header to send to server
566+
567+
:param headers: :class:`dict`, :class:`CIMultiDict` or
568+
:class:`CIMultiDictProxy` for providing additional
569+
headers for websocket handshake request.
570+
571+
.. versionadded:: 0.18
572+
573+
Add *auth* parameter.
574+
575+
.. versionadded:: 0.19
576+
577+
Add *origin* parameter.
578+
579+
.. versionadded:: 0.20
580+
581+
Add *headers* parameter.
582+
583+
584+
525585
Connectors
526586
----------
527587

@@ -1025,6 +1085,89 @@ Response object
10251085
``None`` if *BODY* is empty or contains white-spaces
10261086
only.
10271087

1088+
ClientWebSocketResponse
1089+
-----------------------
1090+
1091+
To connect to a websocket server :func:`aiohttp.ws_connect` or
1092+
:meth:`aiohttp.ClientSession.ws_connect` coroutines should be used, do
1093+
not create an instance of class :class:`ClientWebSocketResponse`
1094+
manually.
1095+
1096+
.. class:: ClientWebSocketResponse()
1097+
1098+
Class for handling client-side websockets.
1099+
1100+
.. attribute:: closed
1101+
1102+
Read-only property, ``True`` if :meth:`close` has been called of
1103+
:const:`~aiohttp.websocket.MSG_CLOSE` message has been received from peer.
1104+
1105+
.. attribute:: protocol
1106+
1107+
Websocket *subprotocol* chosen after :meth:`start` call.
1108+
1109+
May be ``None`` if server and client protocols are
1110+
not overlapping.
1111+
1112+
.. method:: exception()
1113+
1114+
Returns exception if any occurs or returns None.
1115+
1116+
.. method:: ping(message=b'')
1117+
1118+
Send :const:`~aiohttp.websocket.MSG_PING` to peer.
1119+
1120+
:param message: optional payload of *ping* message,
1121+
:class:`str` (converted to *UTF-8* encoded bytes)
1122+
or :class:`bytes`.
1123+
1124+
.. method:: send_str(data)
1125+
1126+
Send *data* to peer as :const:`~aiohttp.websocket.MSG_TEXT` message.
1127+
1128+
:param str data: data to send.
1129+
1130+
:raise TypeError: if data is not :class:`str`
1131+
1132+
.. method:: send_bytes(data)
1133+
1134+
Send *data* to peer as :const:`~aiohttp.websocket.MSG_BINARY` message.
1135+
1136+
:param data: data to send.
1137+
1138+
:raise TypeError: if data is not :class:`bytes`,
1139+
:class:`bytearray` or :class:`memoryview`.
1140+
1141+
.. coroutinemethod:: close(*, code=1000, message=b'')
1142+
1143+
A :ref:`coroutine<coroutine>` that initiates closing handshake by sending
1144+
:const:`~aiohttp.websocket.MSG_CLOSE` message. It waits for
1145+
close response from server. It add timeout to `close()` call just wrap
1146+
call with `asyncio.wait()` or `asyncio.wait_for()`.
1147+
1148+
:param int code: closing code
1149+
1150+
:param message: optional payload of *pong* message,
1151+
:class:`str` (converted to *UTF-8* encoded bytes)
1152+
or :class:`bytes`.
1153+
1154+
.. coroutinemethod:: receive()
1155+
1156+
A :ref:`coroutine<coroutine>` that waits upcoming *data*
1157+
message from peer and returns it.
1158+
1159+
The coroutine implicitly handles
1160+
:const:`~aiohttp.websocket.MSG_PING`,
1161+
:const:`~aiohttp.websocket.MSG_PONG` and
1162+
:const:`~aiohttp.websocket.MSG_CLOSE` without returning the
1163+
message.
1164+
1165+
It process *ping-pong game* and performs *closing handshake* internally.
1166+
1167+
:return: :class:`~aiohttp.websocket.Message`, `tp` is types of
1168+
`~aiohttp.MsgType`
1169+
1170+
10281171
Utilities
10291172
---------
10301173

0 commit comments

Comments
 (0)