From 38e72dc304c31eeb291b31ea9aeac70cae059662 Mon Sep 17 00:00:00 2001 From: Roy Hyunjin Han Date: Wed, 20 Nov 2013 08:07:07 -0800 Subject: [PATCH] Fixed calling on_connect() so that it is called only once; fixes #31 --- CHANGES.rst | 3 ++- README.rst | 3 ++- TODO.goals | 3 +-- TODO.log | 3 ++- socketIO_client/__init__.py | 28 ++++++++++++++++------------ socketIO_client/transports.py | 19 ++++++++++++++----- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7e91a66..90ee4b2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,7 @@ 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 0.5.2 diff --git a/README.rst b/README.rst index 1e9f420..64dc5cd 100644 --- a/README.rst +++ b/README.rst @@ -148,7 +148,7 @@ Credits - `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. - `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 @@ -179,3 +179,4 @@ Credits .. _Eric Chen: https://github.com/taiyangc .. _Denis Zinevich: https://github.com/dzinevich .. _Thiago Hersan: https://github.com/thiagohersan +.. _Nayef Copty: https://github.com/nayefc diff --git a/TODO.goals b/TODO.goals index 0ffe609..cb82706 100644 --- a/TODO.goals +++ b/TODO.goals @@ -1,2 +1 @@ -# America/Los_Angeles 11/17/2013 -_ Investigate heartbeat timeouts +# US/Pacific 11/19/2013 \ No newline at end of file diff --git a/TODO.log b/TODO.log index 4da62af..70f9984 100644 --- a/TODO.log +++ b/TODO.log @@ -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] + Add test for server ack callback in namespace [11/17/2013] + Set port automatically if it is not automatically specified [11/17/2013] \ No newline at end of file diff --git a/socketIO_client/__init__.py b/socketIO_client/__init__.py index 5faa483..106accc 100644 --- a/socketIO_client/__init__.py +++ b/socketIO_client/__init__.py @@ -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] diff --git a/socketIO_client/transports.py b/socketIO_client/transports.py index b28a1ee..ed4e59c 100644 --- a/socketIO_client/transports.py +++ b/socketIO_client/transports.py @@ -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):