Documentation for pulsar 0.9.2. For development docs, go here.
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 echos 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()
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 the
WebSocket
middleware after a successful handshake.
Override on_message()
to handle incoming string messages and
on_bytes()
to handle incoming bytes
messages.
You can also override on_open()
and on_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
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
the handle
attribute, an instance of WS
.
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
parser_factory
(version=None, kind=0, extensions=None, protocols=None, pyparser=False)Create a new FrameParser
instance.
Parameters: |
|
---|
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
) or None
.
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
websocket parser
.
Parameters: |
|
---|