Reader¶
You should not make an instance of the WebSocketReader class yourself, rather you should only make use of it through a
callback registerd with message()
>>> @client.message
>>> async def on_message(reader: WebSocketReader):
... # Read from the reader here...
... print(await reader.get())
-
class
websocket.stream.reader.
WebSocketReader
(kind, client, loop)[source]¶ Variables: data_type – The type of data frame the client sent us, this is the default kind for get()
.-
at_eof
()¶ Returns: True iff there is no more data to read AND we have been fed end of file.
-
empty
()¶ Returns: True iff there is no more data to read.
-
feed_eof
()¶ Feed the buffer with end of file
-
get
(kind=None)[source]¶ Reads all of the bytes from the stream.
Parameters: kind ( DataType
) – Specifies the type of data returned, default isdata_type
Returns: bytes
if kind is DataType.BINARY,str
if kind is DataType.TEXT
-
read
(n=-1, chunksize=None)¶ Read data from the buffer. Reads until eof or n bytes.
Parameters: n – The amount of data to read. Returns: A bytearray
with the data
-
read_into
(buffer, n, offset=0)¶ Read data from the buffer into a bytearray. Reads until eof or n bytes.
n must not exceed the buffer limit, or else it will block forever.
Parameters: - buffer – The bytearray to write the data into.
- n – The amount of data to read.
- offset – The index into buffer to write to.
-
read_into_exactly
(buffer, n, offset=0)¶ Read data from the buffer into a bytearray. Reads n bytes or throws an exception if reading eof.
n must not exceed the buffer limit, or else it will block forever.
Parameters: - buffer – The bytearray to write the data into.
- n – The amount of data to read.
-
set_exception
(exc)¶ Set an exception to raise at next read.
-
write
(data)¶ Write some data to the buffer, length must not exceed the buffer limit, or else it will block forever.
Parameters: data (iterable of bytes) – The data to write
-