Skip to content

Commit

Permalink
Add an explicit binary option to WebSocketHandler.write_message.
Browse files Browse the repository at this point in the history
Switching automatically based on python's bytes vs unicode types is
error-prone in python2 where e.g. json_encode returns bytes.

Closes tornadoweb#429.
  • Loading branch information
bdarnell committed Jan 8, 2012
1 parent bd03493 commit 33cd345
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 2 additions & 1 deletion maint/test/websocket/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from tornado.ioloop import IOLoop
from tornado.options import define, options, parse_command_line
from tornado.util import bytes_type
from tornado.websocket import WebSocketHandler
from tornado.web import Application

define('port', default=9000)

class EchoHandler(WebSocketHandler):
def on_message(self, message):
self.write_message(message)
self.write_message(message, binary=isinstance(message, bytes_type))

if __name__ == '__main__':
parse_command_line()
Expand Down
25 changes: 13 additions & 12 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ def _execute(self, transforms, *args, **kwargs):
self.ws_connection = WebSocketProtocol76(self)
self.ws_connection.accept_connection()

def write_message(self, message):
def write_message(self, message, binary=False):
"""Sends the given message to the client of this Web Socket."""
self.ws_connection.write_message(message)
if isinstance(message, dict):
message = tornado.escape.json_encode(message)
self.ws_connection.write_message(message, binary=binary)

def open(self, *args, **kwargs):
"""Invoked when a new WebSocket is opened."""
Expand Down Expand Up @@ -312,10 +314,11 @@ def _on_length_indicator(self, byte):
self.client_terminated = True
self.close()

def write_message(self, message):
def write_message(self, message, binary=False):
"""Sends the given message to the client of this Web Socket."""
if isinstance(message, dict):
message = tornado.escape.json_encode(message)
if binary:
raise ValueError(
"Binary messages not supported by this version of websockets")
if isinstance(message, unicode):
message = message.encode("utf-8")
assert isinstance(message, bytes_type)
Expand Down Expand Up @@ -405,15 +408,13 @@ def _write_frame(self, fin, opcode, data):
frame += data
self.stream.write(frame)

def write_message(self, message):
def write_message(self, message, binary=False):
"""Sends the given message to the client of this Web Socket."""
if isinstance(message, dict):
message = tornado.escape.json_encode(message)
if isinstance(message, unicode):
opcode = 0x1
message = message.encode("utf-8")
else:
if binary:
opcode = 0x2
else:
opcode = 0x1
message = tornado.escape.utf8(message)
assert isinstance(message, bytes_type)
self._write_frame(True, opcode, message)

Expand Down

0 comments on commit 33cd345

Please sign in to comment.