websocket - WebSocket 13 support

Message

A single WebSocket message. Note that you’ll be seeing the subclasses of this instead; see below.

is_ping

Should never appear if you’re using a Reader (it handles pings automatically.)

is_pong

Whether this message is a response to a previously sent ping.

is_data

Whether this message is some binary data.

is_text

Whether this message is text (in UTF-8, I think?)

is_close

Whether this message is the last one sent over this channel. The connection has already been closed.

DataMessage

A message with type other than “text”. Is a subclass of bytes; contains the data.

TextMessage

A message with type “text”. Is a subclass of str; contains the data, too.

WebSocket

A special coroutine-like object that waits for the next message and returns it when used with yield from. Raises asyncio.CancelledError if the connection gets closed; the first argument of the exception, if it exists, is a Message received before closing the connection (it will have is_close set to True.) Other than that, it can send data, too:

ping payload

Send a ping. This method is a coroutine which waits until all data is sent. (Note that waiting on it is not necessary to send data; feel free to forget the yield from.)

pong payload

Respond to a ping. Done automatically. Also a coroutine.

text payload

Send some text. A coroutine.

data payload

Send some binary data. A coroutine.

close code data

Send a close message. No other messages can be sent after that. This method also cancels the task that calls it, just to drive that point home. A coroutine.

mux streams

Connect multiple WebSocket streams into a read-only one that emits data from all of these streams. Instead of just message, it returns (stream, message) pairs when yielded from. The first websocket to disconnect cancels the task.

endpoint handler

Create a request handler that establishes a WebSocket connection and calls into the handler, which should accept all the same arguments as a normal request handler would with the addition of one more positional argument, which is a WebSocket object.

aggregate n handler

Same as endpoint, only instead of a single connection the handler should accept a set of n connections. The clients may have to wait until there are enough connections to call the handler.

client path reader writer

Create a client-side half of the WebSocket stream given a standard asyncio StreamReader and StreamWriter. Note that this is a server-side framework, so client-side functionality is not really outstanding; for example, this function only supports HTTP/1.1 and only sends the minimal headers (which are hardcoded). Returns a WebSocket object.

io = yield from $ asyncio.open_connection 'websocket.host' 80
ws = yield from $ websocket.client '/websocket_node/' *: io
ws.text 'ok'