recently exposed to websocket, the demand is very simple, using websocket to let users participate in a 1v1 Mini Game. The
environment is distributed, and each server is multi-process, and the web server uses the nodejs,websocket framework with socket.io.
problems encountered:
first of all, in a single server survey, when using nodejs multi-process (cluster), 400 errors occurred when establishing websocket, and then it was found that it was a multi-process problem, and users could not guarantee that they would visit the same process every time when they visited the server, so the background would report an error "Session ID unknown". It may be that in order to ensure that socket.io may occasionally become long polling, it is necessary to ensure that the foreground and backend must pass a session id. But every time the page is refreshed, the re-launch of websocket should have nothing to do with the websocket before the last refresh, or it is possible that socket.io introduced session id to ensure reconnection with the server to save resources. I don"t know if my understanding is correct.
has found a lot of information on the Internet, and has also seen the recommendation scheme (http://socket.io/docs/using-multiple-nodes/) on the official website of socket.io. There are two kinds:
nginx
configure ip_hash to enable users to connect to fixed processes every time. They don"t know much about nginx before.
redis
read the most established recommended solution on the Internet is also redis. I haven"t contacted redis before, and I don"t know how it works. After roughly understanding it, I found that it is a persistent storage of key-value, similar to MemCache and mongoDB. The code example given on the official website:
var io = require("socket.io")(3000);
var redis = require("socket.io-redis");
io.adapter(redis({ host: "localhost", port: 6379 }));
The example is very simple. It redirects the request to an adapter to connect to the redis service, but what is the principle? the solution of shared memory? I don"t understand it, so I don"t do it.
finally, I chose a silly scheme. When starting the websocket service in different sub-processes, I chose different ports. For example, a server with 8 cores will have 8 ports 3001-3008, so that when users connect to a fixed port, there will be no more 400error, but after users hit different ports, how to communicate with each other is still a problem. This is only a single server experiment, if it is a cluster. Communicating with each other is also a problem, and is it critical that users connect to the same port and the same server every time? In the final analysis, I still know too little about websocket. I hope to get some guidance and help here.