Fixed calling on_connect() so that it is called only once; fixes #31

This commit is contained in:
Roy Hyunjin Han 2013-11-20 08:07:07 -08:00
commit 38e72dc304
6 changed files with 37 additions and 22 deletions

View file

@ -148,7 +148,6 @@ class SocketIO(object):
if path:
self._transport.connect(path)
namespace = Namespace(self._transport, path)
namespace.on_connect()
self._namespace_by_path[path] = namespace
return namespace
@ -173,11 +172,7 @@ class SocketIO(object):
for elapsed_time in warning_screen:
try:
try:
for packet in self._transport.recv_packet():
try:
self._process_packet(packet)
except PacketError as e:
_log.warn('[packet error] %s', e)
self._process_events()
except TimeoutError:
pass
if self._stop_waiting(for_callbacks):
@ -193,6 +188,21 @@ class SocketIO(object):
except KeyboardInterrupt:
pass
def _process_events(self):
for packet in self._transport.recv_packet():
try:
self._process_packet(packet)
except PacketError as e:
_log.warn('[packet error] %s', e)
def _process_packet(self, packet):
logging.debug('xxx')
logging.debug(packet)
code, packet_id, path, data = packet
namespace = self.get_namespace(path)
delegate = self._get_delegate(code)
delegate(packet, namespace._find_event_callback)
def _stop_waiting(self, for_callbacks):
# Use __transport to make sure that we do not reconnect inadvertently
if for_callbacks and not self.__transport.has_ack_callback:
@ -264,12 +274,6 @@ class SocketIO(object):
heartbeat_time = elapsed_time
self._transport.send_heartbeat()
def _process_packet(self, packet):
code, packet_id, path, data = packet
namespace = self.get_namespace(path)
delegate = self._get_delegate(code)
delegate(packet, namespace._find_event_callback)
def get_namespace(self, path=''):
try:
return self._namespace_by_path[path]

View file

@ -23,6 +23,7 @@ class _AbstractTransport(object):
self._packet_id = 0
self._callback_by_packet_id = {}
self._wants_to_disconnect = False
self._packets = []
def disconnect(self, path=''):
if not path:
@ -71,7 +72,11 @@ class _AbstractTransport(object):
_log.debug('[packet sent] %s', packet_text)
def recv_packet(self):
code, packet_id, path, data = None, None, None, None
try:
while self._packets:
yield self._packets.pop(0)
except IndexError:
pass
for packet_text in self.recv():
_log.debug('[packet received] %s', packet_text)
try:
@ -79,6 +84,7 @@ class _AbstractTransport(object):
except AttributeError:
_log.warn('[packet error] %s', packet_text)
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
@ -88,6 +94,9 @@ class _AbstractTransport(object):
code = packet_parts[0]
yield code, packet_id, path, data
def _enqueue_packet(self, packet):
self._packets.append(packet)
def set_ack_callback(self, callback):
'Set callback to be called after server sends an acknowledgment'
self._packet_id += 1
@ -162,8 +171,8 @@ class _XHR_PollingTransport(_AbstractTransport):
self._connected = True
self._http_session = _prepare_http_session(kw)
# Create connection
for packet_text in self.recv_packet():
pass
for packet in self.recv_packet():
self._enqueue_packet(packet)
@property
def connected(self):
@ -215,8 +224,8 @@ class _JSONP_PollingTransport(_AbstractTransport):
self._http_session = _prepare_http_session(kw)
self._id = 0
# Create connection
for packet_text in self.recv_packet():
pass
for packet in self.recv_packet():
self._enqueue_packet(packet)
@property
def connected(self):