简体   繁体   中英

How to handle client losing Internet connection with socket.io

I am fairly new to Web development so bear with me.

Setup:

I am running a server on my localhost and I am also running a web application (via webpack) which represents a client. Ideally a client would be someone visiting my webapp via a mobile phone for example. Now the client creates a socket (using socket.io) and connects to the server. Then server starts sending data to the client. The client uses the data to alter the state of the webpage. The client also has some local data (accessible from the client side once the web page has loaded) to change state if it receives no data from the server.

Problem:

I want to be able to detect when the client loses internet connection (and therefore connection to my server), so that the webpage can then start using the locally available data to change state. I thought about using Detect the Internet connection is offline? to do this, but I am unsure if this is even the right approach. Is there a way to achieve this using socket.io? I am aware of socket.on('disconnect', ...) but I am unsure if this would fire if the client himself loses connection.

您可以在客户端脚本上使用 socket.on('disconnect', ...)

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