"Sec-WebSocket-Version: 8\r\n\r\n"))
self.stream.close()
- else:
- self.ws_connection = WebSocketProtocol76(self)
- self.ws_connection.accept_connection()
+ def write_message(self, message, binary=False):
+ """Sends the given message to the client of this Web Socket.
- def write_message(self, message):
- """Sends the given message to the client of this Web Socket."""
- self.ws_connection.write_message(message)
+ 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.
+ """
+ if isinstance(message, dict):
+ message = tornado.escape.json_encode(message)
+ self.ws_connection.write_message(message, binary=binary)
+ def validate_subprotocol(self, subprotocols):
+ """Invoked when a new WebSocket requests specific subprotocols."""
+ return None
+
def open(self, *args, **kwargs):
"""Invoked when a new WebSocket is opened."""
pass
logging.debug("Malformed WebSocket request received")
self._abort()
return
- scheme = "wss" if self.request.protocol == "https" else "ws"
+ scheme = self.handler.get_websocket_scheme()
+
+ subprotocols = self.request.headers.get("Sec-WebSocket-Protocol", None)
+ if subprotocols:
+ subprotocol = self.handler.validate_subprotocol(subprotocols)
+ if not subprotocol:
+ logging.debug("Subprotocol rejected by handler.")
+ self._abort()
+ return
+ subprotocol = "Sec-WebSocket-Protocol: %s\r\n" % subprotocol
+ else:
+ subprotocol = ''
+
# Write the initial headers before attempting to read the challenge.
# This is necessary when using proxies (such as HAProxy), which
# need to see the Upgrade headers before passing through the