Handling socketIo disconnection with Python socketIO_client (not server)

1,304 views
Skip to first unread message

alonn

unread,
Apr 28, 2014, 5:22:47 AM4/28/14
to pywe...@googlegroups.com

Cross posting from SO( you can grab the points there if you know the answer) - maybe someone here would know the answer

I wrote this simple python socketIO client - to connect to a socket supporting server.

the class (using socketIO_client lib:

class QuoteClient(object): def init(self, quote_url): self.provider_url = quote_url.rstrip('/') self._connect()

def start(self):
    self.connection.on('quote', self.on_update)
    self.connection.on('error', self.on_error)
    self.connection.on('disconnect', self.on_disconnect)
    self.connection.emit('subscribe', self._get_target_list(), self.on_subscribe)

    self.connection.wait()

def on_update(self, update_data):

    logger.debug('update received %s ' % update_data)


def on_error(self):
    logger.warning('socket error')
    self._connect()

def on_disconnect(self):
    logger.warning('socket disconnect')
    self._connect()

def on_subscribe(self, payload):
    for key, value in payload.items():
        logger.info('subscribed to %s' % self.provider_url)
        self.on_update(update_data)

def _connect(self):
    logger.info('connecting to socket')
    self.connection = SocketIO(self.provider_url)


def _get_target_list(self):
    return [s[0] for s in Target.objects.values_list('key')]

run like that:

q = SocketClient(socket_url)
q.start() 

All works fine. while it lasts..

My question: is how can Should I handle disconnection... To test the on_disconnect handler I tried stopping my socket_server but the client didn't show any of the expected logging. or otherwise. and when the socket_server came back up. No new data was transferred (socket was probably lost) and I had to reset the client.

I'll be glad for help and guidance with this problem.. Thanks!


Shai Berger

unread,
Apr 28, 2014, 5:37:06 AM4/28/14
to pywe...@googlegroups.com
I don't know how to do it in this specific library, but you need to set
SO_KEEPALIVE on the socket.

HTH,
Shai.

On Monday 28 April 2014 12:22:47 alonn wrote:
> Cross posting from
> SO<http://stackoverflow.com/questions/23329095/python-socketio-client-how-
> to-handle-disconnect-from-server>( you can grab the points there if you
> know the answer) - maybe someone here would know the answer
>
> I wrote this simple python socketIO client - to connect to a socket
> supporting server.
>
> the class (using socketIO_client
> lib<https://github.com/invisibleroads/socketIO-client>
>
>
> class QuoteClient(object): def *init*(self, quote_url): self.provider_url =
> *My question:* is how can Should I handle disconnection... To test the
Reply all
Reply to author
Forward
0 new messages