WebSockets¶
The pulsar.apps.ws
contains WSGI middleware for
handling the WebSocket protocol.
Web sockets allow for bidirectional communication between the browser
and server. Pulsar implementation uses the WSGI middleware
WebSocket
for the handshake and a class derived from
WS
handler for the communication part.
This is a Web Socket handler which echoes all received messages back to the client:
from pulsar.apps import wsgi, ws
class EchoWS(ws.WS):
def on_message(self, websocket, message):
websocket.write(message)
To create a valid WebSocket
middleware initialise as follow:
wm = ws.WebSocket('/bla', EchoWS())
app = wsgi.WsgiHandler(middleware=(..., wm))
wsgi.WSGIServer(callable=app).start()
API¶
WebSocket handler¶
-
class
pulsar.apps.ws.
WS
[source]¶ A web socket handler for both servers and clients.
It implements the asynchronous message passing for a
WebSocketProtocol
. On the server, the communication is started by theWebSocket
middleware after a successful handshake.Override
on_message()
to handle incoming string messages andon_bytes()
to handle incomingbytes
messages.You can also override
on_open()
andon_close()
to perform specific tasks when the websocket is opened or closed.These methods accept as first parameter the
WebSocketProtocol
created during the handshake.-
on_open
(websocket)[source]¶ Invoked when a new
websocket
is opened.A web socket is opened straight after the upgrade headers are sent (servers) or received (clients).
-
on_message
(websocket, message)[source]¶ Handles incoming messages on the WebSocket.
This method should be overwritten
-
WebSocket¶
-
class
pulsar.apps.ws.websocket.
WebSocket
(route, handle, parser_factory=None, **kwargs)[source]¶ A specialised
Router
for a websocket handshake.Once the handshake is successful, the protocol consumer is upgraded to
WebSocketProtocol
and messages are handled by thehandle
attribute, an instance ofWS
.See http://tools.ietf.org/html/rfc6455 for the websocket server protocol and http://www.w3.org/TR/websockets/ for details on the JavaScript interface.
-
parser_factory
¶ A factory of websocket frame parsers
-
protocol_class
¶ alias of
WebSocketProtocol
-
parser_factory
(version=None, kind=0, extensions=None, protocols=None, pyparser=False) Create a new
FrameParser
instance.Parameters: - version – protocol version, the default is 13
- kind – the kind of parser, and integer between 0 and 3 (check the
FrameParser
documentation for details) - extensions – not used at the moment
- protocols – not used at the moment
- pyparser – if
True
(defaultFalse
) uses the python frame parser implementation rather than the much faster cython implementation.
-
WebSocket protocol¶
-
class
pulsar.apps.ws.websocket.
WebSocketProtocol
(handshake, handler, parser, loop=None)[source]¶ A
ProtocolConsumer
for websocket servers and clients.-
handshake
¶ The original handshake response/request.
-
parser
¶ A websocket
FrameParser
.
-
close_reason
¶ A tuple of (
code
,reason
) orNone
.Available when a close frame is received.
-
write
(message, opcode=None, encode=True, **kw)[source]¶ Write a new
message
into the wire.It uses the
encode()
method of the websocketparser
.Parameters: - message – message to send, must be a string or bytes
- opcode – optional
opcode
, if not supplied it is set to 1 ifmessage
is a string, otherwise2
when the message are bytes.
-