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,19 +395,22 @@ 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):
return { try:
'0': self._on_disconnect, return {
'1': self._on_connect, '0': self._on_disconnect,
'2': self._on_heartbeat, '1': self._on_connect,
'3': self._on_message, '2': self._on_heartbeat,
'4': self._on_json, '3': self._on_message,
'5': self._on_event, '4': self._on_json,
'6': self._on_ack, '5': self._on_event,
'7': self._on_error, '6': self._on_ack,
'8': self._on_noop, '7': self._on_error,
'': self._on_noop '8': self._on_noop,
}.get(code, self._on_noop) '': 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,22 +98,34 @@ 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):
self._log(logging.DEBUG, '[packet received] %s', packet_text) #remove packet separator
try: packet_texts = re.sub('^\xef\xbf\xbd\w+\xef\xbf\xbd', '',
packet_parts = packet_text.split(':', 3) packet_texts)
except AttributeError:
self._log(logging.WARNING, '[packet error] %s', packet_text) packets = packet_texts.split('\xef\xbf\xbd')[::2]
continue
code, packet_id, path, data = None, None, None, None for packet_text in packets:
packet_count = len(packet_parts) self._log(logging.DEBUG, '[packet received] %s', packet_text)
if 4 == packet_count: sep_count = packet_text.count('\xef\xbf\xbd')/2
code, packet_id, path, data = packet_parts
elif 3 == packet_count: try:
code, packet_id, path = packet_parts packet_parts = packet_text.split(':', 3)
elif 1 == packet_count: except AttributeError:
code = packet_parts[0] self._log(logging.WARNING, '[packet error] %s', packet_text)
yield code, packet_id, path, data continue
code, packet_id, path, data = None, None, None, None
packet_count = len(packet_parts)
if 4 == packet_count:
code, packet_id, path, data = packet_parts
elif 3 == packet_count:
code, packet_id, path = packet_parts
elif 1 == packet_count:
code = packet_parts[0]
if code and len(code) > 1:
code = code[-1]
yield code, packet_id, path, data
def _enqueue_packet(self, packet): def _enqueue_packet(self, packet):
self._packets.append(packet) self._packets.append(packet)