Fixed multiple packets being sent at once causing a json error.

This commit is contained in:
Joey Payne 2016-01-27 15:32:52 -07:00
commit c80a2db665
2 changed files with 46 additions and 30 deletions

View file

@ -275,7 +275,7 @@ class SocketIO(object):
def _process_packet(self, packet): def _process_packet(self, packet):
code, packet_id, path, data = packet code, packet_id, path, data = packet
namespace = self.get_namespace(path or '') namespace = self.get_namespace(path or '')
delegate = self._get_delegate(code) delegate = self._get_delegate(code, packet)
delegate(packet, namespace._find_event_callback) delegate(packet, namespace._find_event_callback)
def _stop_waiting(self, for_callbacks): def _stop_waiting(self, for_callbacks):
@ -395,7 +395,8 @@ class SocketIO(object):
except KeyError: except KeyError:
raise PacketError('unhandled namespace path (%s)' % path) raise PacketError('unhandled namespace path (%s)' % path)
def _get_delegate(self, code): def _get_delegate(self, code, packet):
try:
return { return {
'0': self._on_disconnect, '0': self._on_disconnect,
'1': self._on_connect, '1': self._on_connect,
@ -407,7 +408,9 @@ class SocketIO(object):
'7': self._on_error, '7': self._on_error,
'8': self._on_noop, '8': self._on_noop,
'': self._on_noop '': self._on_noop
}.get(code, self._on_noop) }[code]
except KeyError:
raise PacketError('unexpected code ({}): {}'.format([code], packet))
def _on_disconnect(self, packet, find_event_callback): def _on_disconnect(self, packet, find_event_callback):
find_event_callback('disconnect')() find_event_callback('disconnect')()

View file

@ -8,6 +8,7 @@ import socket
import sys import sys
import time import time
import websocket import websocket
import re
from .exceptions import ConnectionError, TimeoutError from .exceptions import ConnectionError, TimeoutError
from .symmetries import _get_text from .symmetries import _get_text
@ -97,8 +98,17 @@ class _AbstractTransport(object):
yield self._packets.pop(0) yield self._packets.pop(0)
except IndexError: except IndexError:
pass pass
for packet_text in self.recv(timeout=timeout): for packet_texts in self.recv(timeout=timeout):
#remove packet separator
packet_texts = re.sub('^\xef\xbf\xbd\w+\xef\xbf\xbd', '',
packet_texts)
packets = packet_texts.split('\xef\xbf\xbd')[::2]
for packet_text in packets:
self._log(logging.DEBUG, '[packet received] %s', packet_text) self._log(logging.DEBUG, '[packet received] %s', packet_text)
sep_count = packet_text.count('\xef\xbf\xbd')/2
try: try:
packet_parts = packet_text.split(':', 3) packet_parts = packet_text.split(':', 3)
except AttributeError: except AttributeError:
@ -112,6 +122,9 @@ class _AbstractTransport(object):
code, packet_id, path = packet_parts code, packet_id, path = packet_parts
elif 1 == packet_count: elif 1 == packet_count:
code = packet_parts[0] code = packet_parts[0]
if code and len(code) > 1:
code = code[-1]
yield code, packet_id, path, data yield code, packet_id, path, data
def _enqueue_packet(self, packet): def _enqueue_packet(self, packet):