Go to the latest version of this document.

# ``twisted`.`web`.`websocket`.`WebSocketProtocol``

class documentation

`class WebSocketProtocol(typing.Protocol): (source)`

View In Hierarchy

An object that conforms to `WebSocketProtocol` can receive all the events from a websocket connection.

Note

While this is sort of like a `byte-stream protocol`, the interface is distinct in a few ways; in particular, we have discrete `negotiationStarted` and `negotiationFinished` methods, representing those events in the websocket handshaking process, and no `connectionMade`; similarly, since websockets can natively support both text and bytes messages, rather than fragmentable segments of a byte stream, we have `textMessageReceived` and `bytesMessageReceived` but no `dataReceived`. Finally, this is a `typing.Protocol` and not a `zope.interface.Interface`, since it does not predate the `typing` module.

Method | `bytesMessageReceived` | A bytes message was received from the peer.
Method | `connectionLost` | The websocket connection was lost.
Method | `negotiationFinished` | Negotiation is complete: a bidirectional websocket channel is now fully established.
Method | `negotiationStarted` | An underlying transport (e.g.: a TCP connection) has been established; negotiation of the websocket transport has begun.
Method | `pongReceived` | A Pong message was received.
Method | `textMessageReceived` | A text message was received from the peer.

def bytesMessageReceived(self, data: `bytes`): (source)

A bytes message was received from the peer.

def connectionLost(self, reason: `Failure`): (source)

The websocket connection was lost.

def negotiationFinished(self): (source)

Negotiation is complete: a bidirectional websocket channel is now fully established.

def negotiationStarted(self, transport: `WebSocketTransport`): (source)

An underlying transport (e.g.: a TCP connection) has been established; negotiation of the websocket transport has begun.

def pongReceived(self, payload: `bytes`): (source)

A Pong message was received.

Note

Per the standard:

        A Pong frame sent in response to a Ping frame must have identical
        "Application data" as found in the message body of the Ping frame
        being replied to.

        If an endpoint receives a Ping frame and has not yet sent Pong
        frame(s) in response to previous Ping frame(s), the endpoint MAY
        elect to send a Pong frame for only the most recently processed
        Ping frame.

Given that some Pong frames may be dropped, this event should only be used in concert with the transport's `.ping` method for its intended purpose, to measure latency and connection durability, not to transport application data.

def textMessageReceived(self, message: `str`): (source)

A text message was received from the peer.