Pyjo.Transaction.WebSocket - WebSocket transaction¶
import Pyjo.Transaction.WebSocket
# Send and receive WebSocket messages
ws = Pyjo.Transaction.WebSocket.new()
ws.send('Hello World!')
@ws.on
def message(ws, msg):
print("Message: {0}".format(repr(msg)))
@ws.on
def finish(ws, code, reason):
print("WebSocket closed with status {0}".format(code))
Pyjo.Transaction.WebSocket
is a container for WebSocket transactions based
on RFC 6455.
Events¶
Pyjo.Transaction.WebSocket
inherits all events from Pyjo.Transaction
and
can emit the following new ones.
binary¶
@ws.on
def binary(ws, chunk):
...
Emitted when a complete WebSocket binary message has been received.
finish¶
@ws.on
def finish(ws, code, reason):
...
Emitted when the WebSocket connection has been closed.
frame¶
@ws.on
def frame(ws, frame):
...
Emitted when a WebSocket frame has been received.
ws.unsubscribe('frame')
@ws.on
def frame(ws, frame):
print("FIN: {0}".format(frame))
print("RSV1: {1}".format(frame))
print("RSV2: {2}".format(frame))
print("RSV3: {3}".format(frame))
print("Opcode: {4}".format(frame))
print("Payload: {5}".format(frame))
json¶
@ws.on
def json(ws, json):
...
Emitted when a complete WebSocket message has been received, all text and binary messages will be automatically JSON decoded. Note that this event only gets emitted when it has at least one subscriber.
@ws.on
def json(ws, json):
print("Message: {msg}".format(json))
message¶
@ws.on
def message(ws, msg):
...
Emitted when a complete WebSocket message has been received, text messages will be automatically decoded. Note that this event only gets emitted when it has at least one subscriber.
text¶
@ws.on
def text(ws, chunk):
...
Emitted when a complete WebSocket text message has been received.
Classes¶
-
class
Pyjo.Transaction.WebSocket.
Pyjo_Transaction_WebSocket
(**kwargs)¶ Pyjo.Transaction.WebSocket
inherits all attributes and methods fromPyjo.Transaction
and implements the following new ones.-
build_frame
(fin, rsv1, rsv2, rsv3, op, payload)¶ chunk = ws.build_frame(fin, rsv1, rsv2, rsv3, op, payload)
Build WebSocket frame.
# Binary frame with FIN bit and payload print(ws.build_frame(1, 0, 0, 0, 2, b'Hello World!')) # Text frame with payload but without FIN bit print(ws.build_frame(0, 0, 0, 0, 1, b'Hello ')) # Continuation frame with FIN bit and payload print(ws.build_frame(1, 0, 0, 0, 0, b'World!')) # Close frame with FIN bit and without payload print(ws.build_frame(1, 0, 0, 0, 8, b'')) # Ping frame with FIN bit and payload print(ws.build_frame(1, 0, 0, 0, 9, b'Test 123')) # Pong frame with FIN bit and payload print(ws.build_frame(1, 0, 0, 0, 10, b'Test 123'))
-
build_message
(**kwargs)¶ chunk = ws.build_message(binary=message) chunk = ws.build_message(text=message) chunk = ws.build_message(json={'test': [1, 2, 3]})
Build WebSocket message.
-
compressed
= None¶ boolean = ws.compressed ws.compressed = boolean
Compress messages with
permessage-deflate
extension.
-
finish
(code=None, reason=None)¶ ws = ws->finish() ws = ws->finish(1000) ws = ws->finish(1003, 'Cannot accept data!')
Close WebSocket connection gracefully.
-
masked
= None¶ boolean = ws.masked ws.masked = boolean
Mask outgoing frames with XOR cipher and a random 32-bit key.
-
max_websocket_size
= None¶ size = ws.max_websocket_size ws.max_websocket_size = 1024
Maximum WebSocket message size in bytes, defaults to the value of the
PYJO_MAX_WEBSOCKET_SIZE
environment variable or262144
(256KB).
-
parse_frame
(chunk)¶ frame = ws.parse_frame(chunk)
Parse WebSocket frame.
# Parse single frame and remove it from buffer frame = ws.parse_frame(chunk) print("FIN: {0}".format(frame)) print("RSV1: {1}".format(frame)) print("RSV2: {2}".format(frame)) print("RSV3: {3}".format(frame)) print("Opcode: {4}".format(frame)) print("Payload: {5}".format(frame))
-
send
(cb=None, **kwargs)¶ ws = ws.send(binary=message) ws = ws.send(text=message) ws = ws.send(json={'test': [1, 2, 3]}) ws = ws.send(frame=[fin, rsv1, rsv2, rsv3, op, payload]) ws = ws.send(cb, ...)
Send message or frame non-blocking via WebSocket, the optional drain callback will be invoked once all data has been written.
# Send “Ping” frame ws.send(frame=[1, 0, 0, 0, 9, b’Hello World!’])
-
-
Pyjo.Transaction.WebSocket.
object
¶ alias of
Pyjo_Transaction_WebSocket