简体   繁体   中英

How to handle socket.io broken connection in Flask?

I have a very simple Python (Flask socket.io) application which works as a server and another app written in AngularJS which is a client.

In order to handle connected and disconnected client I use respectlivy:

@socketio.on('connect')
def on_connect():
    print("Client connected")

@socketio.on('disconnect')
def on_disconnect():
    print("Client disconnected")

When Client connects to my app I get information about it, in case if client disconnect (for example because of problems with a network) I don't get any information.

What is the proper way to handle the situation in which client disconnects unexpectedly?

There are two types of connections: using long-pooling or WebSocket.

When you use WebSocket clients knows instantly that server was disconnected. In the case of long-polling, there is need to set ping_interval and pint_timeout parameters (I also find information about heartbeat_interval and heartbeat_timeout but I don't know how they are related to ping_*).

From the server perspective: it doesn't know that client was disconnected and the only way to get that information is to set ping_interval and ping_timeout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM