Skip to content

Commit 60735dd

Browse files
Use correct binary packet types in the msgpack packet encoder (Fixes #811)
1 parent 47c5f45 commit 60735dd

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/socketio/msgpack_packet.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44

55
class MsgPackPacket(packet.Packet):
6+
uses_binary_events = False
7+
68
def encode(self):
79
"""Encode the packet for transmission."""
810
return msgpack.dumps(self._to_dict())

src/socketio/packet.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Packet(object):
1919
# id: ASCII encoded, only if id is not None
2020
# data: JSON dump of data payload
2121

22+
uses_binary_events = True
2223
json = _json
2324

2425
def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None,
@@ -27,7 +28,9 @@ def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None,
2728
self.data = data
2829
self.namespace = namespace
2930
self.id = id
30-
if binary or (binary is None and self._data_is_binary(self.data)):
31+
if self.uses_binary_events and \
32+
(binary or (binary is None and self._data_is_binary(
33+
self.data))):
3134
if self.packet_type == EVENT:
3235
self.packet_type = BINARY_EVENT
3336
elif self.packet_type == ACK:

tests/common/test_msgpack_packet.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ def test_encode_decode_with_id(self):
2222
assert p.data == p2.data
2323
assert p.id == p2.id
2424
assert p.namespace == p2.namespace
25+
26+
def test_encode_binary_event_packet(self):
27+
p = msgpack_packet.MsgPackPacket(packet.EVENT, data={'foo': b'bar'})
28+
assert p.packet_type == packet.EVENT
29+
p2 = msgpack_packet.MsgPackPacket(encoded_packet=p.encode())
30+
assert p2.data == {'foo': b'bar'}
31+
32+
def test_encode_binary_ack_packet(self):
33+
p = msgpack_packet.MsgPackPacket(packet.ACK, data={'foo': b'bar'})
34+
assert p.packet_type == packet.ACK
35+
p2 = msgpack_packet.MsgPackPacket(encoded_packet=p.encode())
36+
assert p2.data == {'foo': b'bar'}

0 commit comments

Comments
 (0)