Public Member Functions | |
def | __init__ |
def | allow_draft76 |
def | async_callback |
def | close |
def | get_websocket_scheme |
def | on_close |
def | on_connection_close |
def | on_message |
def | open |
def | select_subprotocol |
def | write_message |
Public Attributes | |
open_args | |
open_kwargs | |
stream | |
ws_connection | |
Private Member Functions | |
def | _execute |
def | _not_supported |
Subclass this class to create a basic WebSocket handler. Override on_message to handle incoming messages. You can also override open and on_close to handle opened and closed connections. See http://dev.w3.org/html5/websockets/ for details on the JavaScript interface. The protocol is specified at http://tools.ietf.org/html/rfc6455. Here is an example Web Socket handler that echos back all received messages back to the client:: class EchoWebSocket(websocket.WebSocketHandler): def open(self): print "WebSocket opened" def on_message(self, message): self.write_message(u"You said: " + message) def on_close(self): print "WebSocket closed" Web Sockets are not standard HTTP connections. The "handshake" is HTTP, but after the handshake, the protocol is message-based. Consequently, most of the Tornado HTTP facilities are not available in handlers of this type. The only communication methods available to you are write_message() and close(). Likewise, your request handler class should implement open() method rather than get() or post(). If you map the handler above to "/websocket" in your application, you can invoke it in JavaScript with:: var ws = new WebSocket("ws://localhost:8888/websocket"); ws.onopen = function() { ws.send("Hello, world"); }; ws.onmessage = function (evt) { alert(evt.data); }; This script pops up an alert box that says "You said: Hello, world".
Definition at line 36 of file websocket.py.
def tornado.websocket.WebSocketHandler.__init__ | ( | self, | |
application, | |||
request, | |||
kwargs | |||
) |
Reimplemented from tornado.web.RequestHandler.
Definition at line 79 of file websocket.py.
def tornado.websocket.WebSocketHandler._execute | ( | self, | |
transforms, | |||
args, | |||
kwargs | |||
) | [private] |
Executes this request with the given output transforms.
Reimplemented from tornado.web.RequestHandler.
Definition at line 85 of file websocket.py.
def tornado.websocket.WebSocketHandler._not_supported | ( | self, | |
args, | |||
kwargs | |||
) | [private] |
Definition at line 222 of file websocket.py.
def tornado.websocket.WebSocketHandler.allow_draft76 | ( | self | ) |
Override to enable support for the older "draft76" protocol. The draft76 version of the websocket protocol is disabled by default due to security concerns, but it can be enabled by overriding this method to return True. Connections using the draft76 protocol do not support the ``binary=True`` flag to `write_message`. Support for the draft76 protocol is deprecated and will be removed in a future version of Tornado.
Definition at line 186 of file websocket.py.
def tornado.websocket.WebSocketHandler.async_callback | ( | self, | |
callback, | |||
args, | |||
kwargs | |||
) |
Wrap callbacks with this if they are used on asynchronous requests. Catches exceptions properly and closes this WebSocket if an exception is uncaught. (Note that this is usually unnecessary thanks to `tornado.stack_context`)
Reimplemented from tornado.web.RequestHandler.
Definition at line 213 of file websocket.py.
def tornado.websocket.WebSocketHandler.close | ( | self | ) |
Closes this Web Socket. Once the close handshake is successful the socket will be closed.
Definition at line 179 of file websocket.py.
Return the url scheme used for this request, either "ws" or "wss". This is normally decided by HTTPServer, but applications may wish to override this if they are using an SSL proxy that does not provide the X-Scheme header as understood by HTTPServer. Note that this is only used by the draft76 protocol.
Definition at line 201 of file websocket.py.
def tornado.websocket.WebSocketHandler.on_close | ( | self | ) |
Invoked when the WebSocket is closed.
Definition at line 175 of file websocket.py.
Called in async handlers if the client closed the connection. Override this to clean up resources associated with long-lived connections. Note that this method is called only if the connection was closed during asynchronous processing; if you need to do cleanup after every request override `on_finish` instead. Proxies may keep a connection open for a time (perhaps indefinitely) after the client has gone away, so this method may not be called promptly after the end user closes their connection.
Reimplemented from tornado.web.RequestHandler.
Definition at line 225 of file websocket.py.
def tornado.websocket.WebSocketHandler.on_message | ( | self, | |
message | |||
) |
Handle incoming messages on the WebSocket This method must be overridden.
Definition at line 168 of file websocket.py.
def tornado.websocket.WebSocketHandler.open | ( | self | ) |
Invoked when a new WebSocket is opened. The arguments to `open` are extracted from the `tornado.web.URLSpec` regular expression, just like the arguments to `tornado.web.RequestHandler.get`.
Definition at line 159 of file websocket.py.
def tornado.websocket.WebSocketHandler.select_subprotocol | ( | self, | |
subprotocols | |||
) |
Invoked when a new WebSocket requests specific subprotocols. ``subprotocols`` is a list of strings identifying the subprotocols proposed by the client. This method may be overridden to return one of those strings to select it, or ``None`` to not select a subprotocol. Failure to select a subprotocol does not automatically abort the connection, although clients may close the connection if none of their proposed subprotocols was selected.
Definition at line 146 of file websocket.py.
def tornado.websocket.WebSocketHandler.write_message | ( | self, | |
message, | |||
binary = False |
|||
) |
Sends the given message to the client of this Web Socket. The message may be either a string or a dict (which will be encoded as json). If the ``binary`` argument is false, the message will be sent as utf8; in binary mode any byte string is allowed.
Definition at line 134 of file websocket.py.
Definition at line 85 of file websocket.py.
Definition at line 85 of file websocket.py.
Definition at line 79 of file websocket.py.
Definition at line 79 of file websocket.py.