class documentation
Synchronous WebSocket client for MicroPython.
- Usage:
- ws = WebSocket.connect("wss://echo.websocket.org") ws.send("Hello!") response = ws.recv() ws.close()
| Class Method | connect |
Connect to a WebSocket server. |
| Method | __del__ |
Destructor to clean up resources. |
| Method | __enter__ |
Enter the context manager, returning this WebSocket. |
| Method | __exit__ |
Exit the context manager, closing the WebSocket. |
| Method | __init__ |
Initialize the WebSocket. |
| Method | close |
Close the websocket gracefully. |
| Method | is |
Set the socket to blocking or non-blocking mode. |
| Method | ping |
Send a ping frame. |
| Method | pong |
Send a pong frame. |
| Method | recv |
Receive data from the websocket. |
| Method | send |
Send data to the websocket. |
| Class Variable | is |
Undocumented |
| Property | error |
Get the last error message, if any. |
| Property | is |
Check if the socket is in blocking mode. |
| Property | is |
Check if the WebSocket is connected. |
| Property | is |
Check if the WebSocket is open. |
| Method | _close |
Internal close - cleanup resources. |
| Method | _read |
Read a frame from the socket. |
| Method | _write |
Write a frame to the socket. |
| Instance Variable | _blocking |
Undocumented |
| Instance Variable | _error |
Undocumented |
| Instance Variable | _open |
Undocumented |
| Instance Variable | _sock |
Undocumented |
| Instance Variable | _underlying |
Undocumented |
Connect to a WebSocket server.
| Parameters | |
uri:str | WebSocket URL (ws:// or wss://). |
headers:dict | Optional additional headers for the handshake. Defaults to None. |
timeout:float | Connection timeout in seconds. Defaults to 10.0. |
| Returns | |
WebSocket | The connected WebSocket instance. |
Exit the context manager, closing the WebSocket.
| Parameters | |
exctype | Exception type, if any. |
exc:Exception | Exception instance, if any. |
tb:traceback | Traceback, if any. |
Initialize the WebSocket.
| Parameters | |
sock:socket | Connected socket (possibly SSL-wrapped). |
underlyingsocket | Underlying raw socket for timeout control. Defaults to None. |
Close the websocket gracefully.
| Parameters | |
code:int | Close status code. Defaults to WS_CLOSE_OK. |
reason:str | Optional close reason. Defaults to "". |
Set the socket to blocking or non-blocking mode.
| Parameters | |
flag:bool | True for blocking mode, False for non-blocking. |
Send a ping frame.
| Parameters | |
data:bytes | Optional ping payload (max 125 bytes). Defaults to b"". |
| Returns | |
bool | True if sent successfully. |
Send a pong frame.
| Parameters | |
data:bytes | Pong payload. Defaults to b"". |
| Returns | |
bool | True if sent successfully. |
Receive data from the websocket.
| Returns | |
str or bytes or None | Data as str (text) or bytes (binary), empty string if no data, or None if the connection is closed. |
Send data to the websocket.
| Parameters | |
data:str or bytes | Data to send. |
| Returns | |
bool | True if sent successfully. |
Read a frame from the socket.
See https://tools.ietf.org/html/rfc6455#section-5.2 for details.
| Parameters | |
maxint | Maximum frame size. Defaults to None. |
| Returns | |
tuple | (fin, opcode, data). |