@@ -172,7 +172,7 @@ async def __aiter__(self) -> AsyncIterator[Data]:
172
172
except ConnectionClosedOK :
173
173
return
174
174
175
- async def recv (self ) -> Data :
175
+ async def recv (self , decode : bool | None = None ) -> Data :
176
176
"""
177
177
Receive the next message.
178
178
@@ -192,21 +192,34 @@ async def recv(self) -> Data:
192
192
When the message is fragmented, :meth:`recv` waits until all fragments
193
193
are received, reassembles them, and returns the whole message.
194
194
195
+ Args:
196
+ decode: Set this flag to override the default behavior of returning
197
+ :class:`str` or :class:`bytes`. See below for details.
198
+
195
199
Returns:
196
200
A string (:class:`str`) for a Text_ frame or a bytestring
197
201
(:class:`bytes`) for a Binary_ frame.
198
202
199
203
.. _Text: https://www.rfc-editor.org/rfc/rfc6455.html#section-5.6
200
204
.. _Binary: https://www.rfc-editor.org/rfc/rfc6455.html#section-5.6
201
205
206
+ You may override this behavior with the ``decode`` argument:
207
+
208
+ * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames
209
+ and return a bytestring (:class:`bytes`). This may be useful to
210
+ optimize performance when decoding isn't needed.
211
+ * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
212
+ and return a string (:class:`str`). This is useful for servers
213
+ that send binary frames instead of text frames.
214
+
202
215
Raises:
203
216
ConnectionClosed: When the connection is closed.
204
217
RuntimeError: If two coroutines call :meth:`recv` or
205
218
:meth:`recv_streaming` concurrently.
206
219
207
220
"""
208
221
try :
209
- return await self .recv_messages .get ()
222
+ return await self .recv_messages .get (decode )
210
223
except EOFError :
211
224
raise self .protocol .close_exc from self .recv_exc
212
225
except RuntimeError :
@@ -215,7 +228,7 @@ async def recv(self) -> Data:
215
228
"is already running recv or recv_streaming"
216
229
) from None
217
230
218
- async def recv_streaming (self ) -> AsyncIterator [Data ]:
231
+ async def recv_streaming (self , decode : bool | None = None ) -> AsyncIterator [Data ]:
219
232
"""
220
233
Receive the next message frame by frame.
221
234
@@ -232,21 +245,34 @@ async def recv_streaming(self) -> AsyncIterator[Data]:
232
245
iterator in a partially consumed state, making the connection unusable.
233
246
Instead, you should close the connection with :meth:`close`.
234
247
248
+ Args:
249
+ decode: Set this flag to override the default behavior of returning
250
+ :class:`str` or :class:`bytes`. See below for details.
251
+
235
252
Returns:
236
253
An iterator of strings (:class:`str`) for a Text_ frame or
237
254
bytestrings (:class:`bytes`) for a Binary_ frame.
238
255
239
256
.. _Text: https://www.rfc-editor.org/rfc/rfc6455.html#section-5.6
240
257
.. _Binary: https://www.rfc-editor.org/rfc/rfc6455.html#section-5.6
241
258
259
+ You may override this behavior with the ``decode`` argument:
260
+
261
+ * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames
262
+ and return bytestrings (:class:`bytes`). This may be useful to
263
+ optimize performance when decoding isn't needed.
264
+ * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
265
+ and return strings (:class:`str`). This is useful for servers
266
+ that send binary frames instead of text frames.
267
+
242
268
Raises:
243
269
ConnectionClosed: When the connection is closed.
244
270
RuntimeError: If two coroutines call :meth:`recv` or
245
271
:meth:`recv_streaming` concurrently.
246
272
247
273
"""
248
274
try :
249
- async for frame in self .recv_messages .get_iter ():
275
+ async for frame in self .recv_messages .get_iter (decode ):
250
276
yield frame
251
277
except EOFError :
252
278
raise self .protocol .close_exc from self .recv_exc
0 commit comments