Problem discovery
Recently, I was writing a demo of a small game of war. The main way to play it is to let two people mine each other to defeat the opponent. The server side of the game is a forwarding script based on Socket.io, while the client side uses the Socket provided by the BestHttp plug-in IO connection library. The preview of the whole game is as follows:
![U3d war bomber game preview](//up-free-imgs.azimiao.com/wp-content/uploads/2018/05/bomber1-r.jpg)
After the game is running, the client is disconnected and reconnected frequently, which has affected the normal operation of the game. For example, when sending a mine placement event, our party or the opposite party is offline, resulting in the event sending failure, and the client cannot respond normally. Refer to the following figure for the frequency of disconnection and reconnection:
![Disconnection reconnection](//up-free-imgs.azimiao.com/wp-content/uploads/2018/05/bomber2.jpg)
Try to solve
2021.12.30 @Brave Game Controller
Add in the comment area:
BestHttp does not start the heartbeat thread of ws by default, and it needs to be set manually webSocket.StartPingThread = true
;
As the version used in writing this article is too old to apply to the current situation, please refer to the above comments to try to solve it.
I checked some materials and found that the WebSocket protocol supports TCP long connection. It defines Ping and Pong events to make heartbeat, so as to maintain long connection. According to my guess, Socket.io should encapsulate these events and automatically maintain long connections. Why is the duration of the above connections so short?
First of all, I guess it's a protocol problem. Socket.io will automatically select protocols according to the environment. The protocols it supports are as follows:
- WebSocket
- Adobe Flash Socket
- AJAX long polling
- AJAX multipart streaming
- Forever Iframe
- JSONP Polling
So I changed the links to ws://, but it didn't work. Later, I tried to modify the connection settings of the client, such as reconnection after disconnection and reconnection after delay, but it didn't work.
Finally, I focused on the server script. Since the WebSocket protocol uses Ping and Pong for heartbeat, is there a possibility that the client's heartbeat packets are sent at too long an interval, and the server thinks it has timed out and actively disconnects? So I modified the code as follows:
Original server script
let server = require("http").createServer(); let io = require("socket.io")(server);
Modified server script
let server = require("http").createServer(); let io = require("socket.io")(server,{'pingTimeout':6666666,'pingInterval':6666666});
After the modification is completed, save and re run the script. Then open two game clients to fight. The frequent disconnection and reconnection problem of the game can be solved.
guess
Since I don't know much about computer networks, and this plug-in is also a closed source charging plug-in, I can only guess that this plug-in has some minor problems in the C # implementation of Socket.io. Maybe the interval or processing method of sending heartbeat is inconsistent with the default configuration of the server API? Don't you understand?
PS: Setting a large heartbeat interval in the server script is just a way to slow down, but it is enough for small exercises written casually.