diff --git a/socketio/packet.py b/socketio/packet.py index 2834aee8..dd9f8596 100644 --- a/socketio/packet.py +++ b/socketio/packet.py @@ -12,6 +12,16 @@ class Packet(object): """Socket.IO packet.""" + # the format of the Socket.IO packet is as follows: + # + # type: 1 byte, values 0-6 + # num_attachments: ASCII encoded, only if num_attachments != 0 + # '-': only if num_attachments != 0 + # namespace: only if namespace != '/' + # ',': only if namespace and one of id and data are defined in this packet + # id: ASCII encoded, only if id is not None + # data: JSON dump of data payload + json = _json def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None, @@ -79,9 +89,8 @@ def decode(self, encoded_packet): self.data = None ep = ep[1:] dash = (ep + '-').find('-') - comma = (ep + ',').find(',') attachment_count = 0 - if dash < comma: + if ep[0:dash].isdigit(): attachment_count = int(ep[0:dash]) ep = ep[dash + 1:] if ep and ep[0:1] == '/': diff --git a/tests/test_packet.py b/tests/test_packet.py index 039c2a5d..c89ef940 100644 --- a/tests/test_packet.py +++ b/tests/test_packet.py @@ -106,6 +106,17 @@ def test_decode_namespace_no_data(self): self.assertEqual(pkt.namespace, '/bar') self.assertEqual(pkt.encode(), '2/bar') + def test_encode_namespace_with_hyphens(self): + pkt = packet.Packet(packet_type=packet.EVENT, + data=[six.text_type('foo')], namespace='/b-a-r') + self.assertEqual(pkt.namespace, '/b-a-r') + self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]') + + def test_decode_namespace_with_hyphens(self): + pkt = packet.Packet(encoded_packet='2/b-a-r,["foo"]') + self.assertEqual(pkt.namespace, '/b-a-r') + self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]') + def test_encode_id(self): pkt = packet.Packet(packet_type=packet.EVENT, data=[six.text_type('foo')], id=123)