Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep alive in flask-socketio?

there is a error in my flask project. The socketio client often disconnects to my flask-socketio server. They will reconnect some minutes after. I want to keep the connect always alive, how can i do? How can I to fix this bug?

Traceback (most recent call last):


File "F:\Python27\lib\site-packages\gevent\pywsgi.py", line 846, in handle_one
_response
    self.run_application()
  File "F:\Python27\lib\site-packages\geventwebsocket\handler.py", line 76, in r
un_application
    self.run_websocket()
  File "F:\Python27\lib\site-packages\geventwebsocket\handler.py", line 52, in r
un_websocket
    self.application(self.environ, lambda s, h, e=None: [])
  File "F:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "F:\Python27\lib\site-packages\flask_socketio\__init__.py", line 37, in _
_call__
    start_response)
  File "F:\Python27\lib\site-packages\engineio\middleware.py", line 47, in __cal
l__
    return self.engineio_app.handle_request(environ, start_response)
  File "F:\Python27\lib\site-packages\socketio\server.py", line 303, in handle_r
equest
    return self.eio.handle_request(environ, start_response)
  File "F:\Python27\lib\site-packages\engineio\server.py", line 226, in handle_r
equest
    environ, start_response)
  File "F:\Python27\lib\site-packages\engineio\socket.py", line 75, in handle_ge
t_request
    start_response)
  File "F:\Python27\lib\site-packages\engineio\socket.py", line 110, in _upgrade
_websocket
    return ws(environ, start_response)
  File "F:\Python27\lib\site-packages\engineio\async_gevent.py", line 43, in __c
all__
    return self.app(self)
  File "F:\Python27\lib\site-packages\engineio\socket.py", line 171, in _websock
et_handler
    self.receive(pkt)
  File "F:\Python27\lib\site-packages\engineio\socket.py", line 45, in receive
    self.server._trigger_event('message', self.sid, pkt.data)
  File "F:\Python27\lib\site-packages\engineio\server.py", line 307, in _trigger
_event
    return self.handlers[event](*args)
  File "F:\Python27\lib\site-packages\socketio\server.py", line 423, in _handle_
eio_message
    self._handle_event(sid, pkt.namespace, pkt.id, pkt.data)
  File "F:\Python27\lib\site-packages\socketio\server.py", line 364, in _handle_
event
    r = self._trigger_event(data[0], namespace, sid, *data[1:])
  File "F:\Python27\lib\site-packages\socketio\server.py", line 391, in _trigger
_event
    return self.handlers[namespace][event](*args)
  File "F:\Python27\lib\site-packages\flask_socketio\__init__.py", line 147, in
_handler
    app = self.server.environ[sid]['flask.app']
KeyError: 'e99ae44429294ef1af9b9012c6cd747c'
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
 'HTTP_ACCEPT_LANGUAGE': 'zh-CN,zh;q=0.8,en;q=0.6',
 'HTTP_CACHE_CONTROL': 'no-cache',
 'HTTP_CONNECTION': 'Upgrade',
 'HTTP_COOKIE': 'io=e99ae44429294ef1af9b9012c6cd747c; session=.eJyrVkrNTczMUbJSS
q_KyUzMS8_NzEvPKM10SM4vKtDLSy1JTSxO1UvOz1XSUUorzcnJS8xNBSqOKTWzsDAEkmYGqTGlFmkWl
kD5vMzkbKg8qmFKtQBXYyPB.CV-98A.aLaOqWHgFZILSx3w1JJ7V9RFUaE',
 'HTTP_HOST': '127.0.0.1:5001',
 'HTTP_ORIGIN': 'http://127.0.0.1:5001',
 'HTTP_PRAGMA': 'no-cache',
 'HTTP_SEC_WEBSOCKET_EXTENSIONS': 'permessage-deflate; client_max_window_bits',
 'HTTP_SEC_WEBSOCKET_KEY': '7dsbtNi0XUWxPY3rXHn4MA==',
 'HTTP_SEC_WEBSOCKET_VERSION': '13',
 'HTTP_UPGRADE': 'websocket',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHT
ML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
 'PATH_INFO': '/socket.io/',
 'QUERY_STRING': 'EIO=3&transport=websocket&sid=e99ae44429294ef1af9b9012c6cd747c
',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '2318',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'GIH-D-9660.game.ntes',
 'SERVER_PORT': '5001',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.1 Python/2.7',
 'flask.app': <Flask 'views'>,
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x0213D0D0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x034255A8>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0),
 'wsgi.websocket': None,
 'wsgi.websocket_version': '13'} failed with KeyError
like image 937
huiji_Leung Avatar asked Nov 27 '25 13:11

huiji_Leung


1 Answers

Please enable Engine.IO logging on the server (add argument engineio_logger=True to your constructor), so that we can find out exactly what the server did.

My guess is that the logs will show that the server times out waiting for the ping packet from the client, so it assumes the client went away and closes the connection. The Socket.IO protocol requires clients to send these special ping packets every so often, if that does not happen then the server closes the connection.

Based on your error, it appears the server timed out waiting for a ping packet and closed the connection, but then later the client does send a ping packet, when it is already too late. The error message in this situation can be improved, however, so I will log a bug for that.

If the connection in your set up isn't reliable, you can increase the timeout, by setting argument ping_timeout in the SocketIO constructor. The default is 60 seconds.

like image 118
Miguel Avatar answered Nov 29 '25 01:11

Miguel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!