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

@ -1,6 +1,7 @@
0.5.3 0.5.3
----- -----
- Exit the wait loop if the client wants to disconnect - Updated wait loop to exit if the client wants to disconnect
- Fixed calling on_connect() so that it is called only once
- Set heartbeat_interval to be half of the heartbeat_timeout - Set heartbeat_interval to be half of the heartbeat_timeout
0.5.2 0.5.2

View file

@ -148,7 +148,7 @@ Credits
- `Alexandre Bourget`_ wrote gevent-socketio_, which is a socket.io server written in Python. - `Alexandre Bourget`_ wrote gevent-socketio_, which is a socket.io server written in Python.
- `Paul Kienzle`_, `Zac Lee`_, `Josh VanderLinden`_, `Ian Fitzpatrick`_, `Lucas Klein`_, `Rui Chicoria`_, `Travis Odom`_ submitted code to expand support of the socket.io protocol. - `Paul Kienzle`_, `Zac Lee`_, `Josh VanderLinden`_, `Ian Fitzpatrick`_, `Lucas Klein`_, `Rui Chicoria`_, `Travis Odom`_ submitted code to expand support of the socket.io protocol.
- `Bernard Pratz`_ and `Francis Bull`_ wrote prototypes to support xhr-polling and jsonp-polling. - `Bernard Pratz`_ and `Francis Bull`_ wrote prototypes to support xhr-polling and jsonp-polling.
- `Eric Chen`_, `Denis Zinevich`_, `Thiago Hersan`_ suggested ways to make the connection more robust. - `Eric Chen`_, `Denis Zinevich`_, `Thiago Hersan`_, `Nayef Copty`_ suggested ways to make the connection more robust.
.. _socket.io: http://socket.io .. _socket.io: http://socket.io
@ -179,3 +179,4 @@ Credits
.. _Eric Chen: https://github.com/taiyangc .. _Eric Chen: https://github.com/taiyangc
.. _Denis Zinevich: https://github.com/dzinevich .. _Denis Zinevich: https://github.com/dzinevich
.. _Thiago Hersan: https://github.com/thiagohersan .. _Thiago Hersan: https://github.com/thiagohersan
.. _Nayef Copty: https://github.com/nayefc

View file

@ -1,2 +1 @@
# America/Los_Angeles 11/17/2013 # US/Pacific 11/19/2013
_ Investigate heartbeat timeouts

View file

@ -1,4 +1,5 @@
# UTC 11/17/2013 # UTC 11/19/2013
+ Add nayefc to acknowledgments [11/19/2013]
+ Beware of scheme included in URL [11/17/2013] + Beware of scheme included in URL [11/17/2013]
+ Add test for server ack callback in namespace [11/17/2013] + Add test for server ack callback in namespace [11/17/2013]
+ Set port automatically if it is not automatically specified [11/17/2013] + Set port automatically if it is not automatically specified [11/17/2013]

View file

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

View file

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