Skip to content

Loading…

Fixes bug #50 #55

Merged
merged 1 commit into from

2 participants

@kiddouk

as requested.

@mrjoes
Owner

Looks good, but will be slightly slower than current implementation due to dict operations.

@mrjoes mrjoes merged commit 586c44e into mrjoes:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 17, 2012
  1. @kiddouk

    Fixes #50

    kiddouk committed
Showing with 30 additions and 20 deletions.
  1. +7 −3 tornadio2/conn.py
  2. +23 −17 tornadio2/proto.py
View
10 tornadio2/conn.py
@@ -209,7 +209,7 @@ def on_close(self):
"""Default on_close handler."""
pass
- def send(self, message, callback=None):
+ def send(self, message, callback=None, force_json=False):
"""Send message to the client.
`message`
@@ -218,6 +218,10 @@ def send(self, message, callback=None):
Optional callback. If passed, callback will be called
when client received sent message and sent acknowledgment
back.
+ `force_json`
+ Optional argument. If set to True (and message is a string)
+ then the message type will be JSON (Type 4 in socket_io protocol).
+ This is what you want, when you send already json encoded strings.
"""
if self.is_closed:
return
@@ -225,9 +229,9 @@ def send(self, message, callback=None):
if callback is not None:
msg = proto.message(self.endpoint,
message,
- self.queue_ack(callback, message))
+ self.queue_ack(callback, message), force_json)
else:
- msg = proto.message(self.endpoint, message)
+ msg = proto.message(self.endpoint, message, force_json=force_json)
self.session.send_message(msg)
View
40 tornadio2/proto.py
@@ -79,7 +79,7 @@ def heartbeat():
return u'2::'
-def message(endpoint, msg, message_id=None):
+def message(endpoint, msg, message_id=None, force_json=False):
"""Generate message packet.
`endpoint`
@@ -89,24 +89,30 @@ def message(endpoint, msg, message_id=None):
If object or dictionary, will json encode and send as is.
`message_id`
Optional message id for ACK
+ `force json`
+ Disregard msg type and send the message with JSON type. Usefull for already
+ JSON encoded strings.
"""
- if (not isinstance(msg, (unicode, str)) and
- isinstance(msg, (object, dict))):
- if msg is not None:
- return u'4:%s:%s:%s' % (
- message_id or '',
- endpoint or '',
- json.dumps(msg, **json_decimal_args)
- )
- else:
- # TODO: Log something?
- return u''
+ if msg is None:
+ # TODO: Log something ?
+ return u''
+
+ packed_message_tpl = u"%(kind)s:%(message_id)s:%(endpoint)s:%(msg)s"
+ packed_data = {'endpoint': endpoint or u'',
+ 'message_id': message_id or u''}
+
+ # Trying to send a dict over the wire ?
+ if not isinstance(msg, (unicode, str)) and isinstance(msg, (dict, object)):
+ packed_data.update({'kind': JSON,
+ 'msg': json.dumps(msg, **json_decimal_args)})
+
+ # for all other classes, including objects. Call str(obj)
+ # and respect forced JSON if requested
else:
- return u'3:%s:%s:%s' % (
- message_id or u'',
- endpoint or u'',
- msg if isinstance(msg, unicode) else str(msg).decode('utf-8')
- )
+ packed_data.update({'kind': MESSAGE if not force_json else JSON,
+ 'msg': msg if isinstance(msg, unicode) else str(msg).decode('utf-8')})
+
+ return packed_message_tpl % packed_data
def event(endpoint, name, message_id, *args, **kwargs):
Something went wrong with that request. Please try again.