Skip to content

blackbull.client.websocket

blackbull.client.websocket

HTTP/1.1 WebSocket client. Client frames are masked; incoming server frames must not be. The returned session owns post-handshake reads.

WebSocketClient

Bases: _TransportOwner

Async WebSocket client.

Use as an async context manager::

async with WebSocketClient('localhost', 8000) as c:
    ws = await c.connect('/path', subprotocols=[b'chat'])
    await ws.send_text('hello')
    msg = await ws.receive()
    await ws.close()

The transport is held open between connect() and __aexit__; only one concurrent session per WebSocketClient is supported.

connect(path, *, subprotocols=(), response_timeout=5.0) async

Upgrade to WebSocket, closing the connection on failure.

response_timeout=None disables the handshake response deadline.

WebSocketSession

Bases: _WebSocketSession, _TransportClose

Frame-level WebSocket session over an established connection.

Always masks outgoing frames (RFC 6455 ยง5.1). Reads use WebSocketRecipient(require_masked=False) because servers MUST NOT mask their outgoing frames.

__aenter__() async

Return the already-connected session; the handshake ran before this.

ping(data=b'') async

Send a PING carrying data; the peer's PONG arrives on the read path.

receive() async

Read one ASGI websocket.* event from the connection.

Returns one of
  • {'type': 'websocket.receive', 'text': str, 'bytes': None}
  • {'type': 'websocket.receive', 'text': None, 'bytes': bytes}
  • {'type': 'websocket.disconnect', 'code': int}

Server-initiated PING frames are auto-PONGed by the underlying WebSocketRecipient (with masking, since this session is the client). Server PONG frames are silently dropped.

send_bytes(data) async

Send data as a binary message.

send_text(text) async

Send text as a UTF-8 text message.