WebSocket

Abstract websocket functionality.

class lomond.websocket.WebSocket(url, protocols=None, agent=None)

IO independent websocket functionality.

Parameters:url (str) – A websocket URL, must have a ws:// or wss:// protocol.
Params list protocols:
 A list of supported protocols (defaults to no protocols).
Params str agent:
 A user agent string to be sent in the header. The default uses the value USER_AGENT defined in lomond.constants.
build_request()

Get the websocket request (in bytes).

This method is called from the session, and should not be invoked explicitly.

close(code=None, reason=None)

Close the websocket.

Parameters:
  • code (int) – A closing code, which should probably be one of the enumerations in lomond.status.Status or a valid value as specified in https://tools.ietf.org/html/rfc6455#section-7.4
  • reason (str) – A short descriptive reason why the websocket is closing. This value is intended for the remote end to help in debugging.

Note

Closing the websocket won’t exit the main loop immediately; it will put the websocket in to a closing state while it waits for the server to echo back a close packet. No data may be sent by the application when the websocket is closing.

connect(session_class=<class 'lomond.session.WebsocketSession'>, poll=5.0, ping_rate=30.0, auto_pong=True)

Connect the websocket to a session.

Parameters:
  • session_class – An object to manage the session. This object is an extension mechanism that will allow the WebSocket to be driven by different back-ends. For now, treat it as an implementation detail and leave it as the default.
  • poll (float) – Rate (in seconds) that poll events should be generated.
  • ping_rate (float) – Rate that ping packets should be sent. Set to 0 to disable auto pings.
  • auto_pong (bool) – Enable (default) automatic response to ping events.
Returns:

An iterable of Event instances.

feed(data)

Feed with data from the socket, and yield any events.

Parameters:data (bytes) – data received over a socket.
is_closed

Flag that indicates if the websocket is closed.

is_closing

Boolean that indicates if the websocket is in a closing state. No further messages may be sent when a websocket is closing.

is_secure

Boolean that indicates if the websocket is over ssl (i.e. the wss protocol).

on_disconnect()

Called on disconnect.

on_response(response)

Called when the HTTP response has been received.

reset()

Reset the state.

send_binary(data)

Send a binary frame.

Parameters:data (bytes) – Binary data to send.
Raises:TypeError – If data is not bytes.
send_ping(data='')

Send a ping packet.

Parameters:

data (bytes) – Data to send in the ping message (must be <= 125 bytes).

Raises:
  • TypeError – If data is not bytes.
  • ValueError – If data is > 125 bytes.
send_pong(data)

Send a pong packet.

Parameters:data (bytes) – Data to send in the ping message (must be <= 125 bytes).

A pong may be sent in response to a ping, or unsolicited to keep the connection alive.

Raises:
  • TypeError – If data is not bytes.
  • ValueError – If data is > 125 bytes.
send_text(text)

Send a text frame.

Parameters:text (str) – Text to send.
Raises:TypeError – If data is not str (or unicode on Py2).