简体   繁体   中英

Node.js Socket.IO Chat Application Cluster Server

Actually i have try to create chat application using socket.io For single server it is working fine. but when i am try to create using cluster server not working for me it error like client not handshaken client should reconnect, socket.io in cluster web socket invalid

those error are continuously giving in console

Can someone help me to proceed for cluster server for socket.IO

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var sio = require('socket.io');
var server;

 if (cluster.isMaster) {


 for (var i = 0; i < numCPUs; i++) {
     cluster.fork();
 }

 cluster.on('online', function(worker) {
   console.log('A worker with #' + worker.id);
 });

 cluster.on('listening', function(worker, address) {
  console.log('A worker is now connected to ' + address.address + ':' + address.port);
 });


  cluster.on('exit', function(worker, code, signal) {
  console.log('worker ' + worker.process.pid + ' died');
   });
 }
else {
  server =  http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('hello world\n');
  }).listen(8000);

 _socketServer();
}

var _socketServer = function() {
 io = sio.listen(server);
 io.set("log level", 1);

  io.sockets.on("connection", function (socket) {
 });
 });

I am getting error like this

client not handshaken client should reconnect, socket.io in cluster

Advance thanks.

This issue happens when you use Socket.io with the Cluster module - It occurs when a client socket is forwarded to a different worker than the one it started the handshake with - What that means is that one worker handles the first half of the handshake and a different worker handles the second half (so in effect, neither worker handles the full handshake - So the handshake is invalid). You should use the sticky-session module to fix this issue: https://github.com/indutny/sticky-session

Alternatively, if you want a solution that just runs on multiple cores out of the box, you could try SocketCluster: http://socketcluster.io/ - Disclaimer: I'm the main author. Here's the GitHub page: https://github.com/topcloud/socketcluster

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