websocketprovider module¶
This class implements a websocket client. |
|
- class WebsocketClient(provider, handle=None)[source]¶
Bases:
object
This class implements a websocket client. See
connect
for more details.- connect(url, headers=None, on_connected=<function nop>, on_disconnected=<function nop>, on_error=<function nop>, on_data=<function nop>)[source]¶
Connect to a given url, asynchronously. The connection will be run in a separate thread managed by the websocket provider. Client callbacks are set according to whichever on_ callback parameters you pass.
Callbacks will be called on the thread of the connection, so be sure to execute_on_main_thread any long-running or gui operations in the callbacks.
If the connection succeeds, on_connected will be called. On normal termination, on_disconnected will be called. If the connection succeeds, but later fails, on_disconnected will not be called, and on_error will be called instead. If the connection fails, neither on_connected nor on_disconnected will be called, and on_error will be called instead.
If on_connected or on_data return false, the connection will be aborted.
- Parameters:
url (str) – full url with scheme, domain, optionally port, and path
headers (dict) – dictionary of string header keys to string header values
on_connected (function() -> bool) – function to call when connection succeeds
on_disconnected (function() -> void) – function to call when connection is closed normally
on_error (function(str) -> void) – function to call when connection is closed with an error
on_data (function(bytes) -> bool) – function to call when data is read from the websocket
- Returns:
if the connection has started, but not necessarily if it succeeded
- Return type:
- Example:
>>> provider = list(WebsocketProvider)[0] >>> client = provider.create_instance() >>> client.connect("ws://localhost:8080", {}) True