@@ -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+
525585Connectors
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+
10281171Utilities
10291172---------
10301173
0 commit comments