Socket.io“握手”失败,出现群集和粘性会话 [英] Socket.io 'Handshake' failing with cluster and sticky-session

查看:278
本文介绍了Socket.io“握手”失败,出现群集和粘性会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使使用一个简单的示例,我也无法使Sticky-Sessions socket.io模块正常工作。按照自述文件( https://github.com/indutny/sticky-session),我只是想使此示例正常工作:

I am having problems getting the sticky-sessions socket.io module to work properly with even a simple example. Following the very minimal example given in the readme (https://github.com/indutny/sticky-session), I am just trying to get this example to work:

var cluster = require('cluster');
var sticky = require('sticky-session');
var http   = require('http');

if (cluster.isMaster) {
  for (var i = 0; i < 4; i++) {
    cluster.fork();
  }

  Object.keys(cluster.workers).forEach(function(id) {
    console.log("Worker running with ID : " + 
      cluster.workers[id].process.pid);
  });
}

if (cluster.isWorker) {
  var anotherServer = http.createServer(function(req, res) {
    res.end('hello world!');
  });
  anotherServer.listen(3000);
  console.log('http server on 3000');
}

sticky(function() {
  var io = require('socket.io')();

  var server = http.createServer(function(req, res) {
    res.end('socket.io');
  });

  io.listen(server);

  io.on('connection', function onConnect(socket) {
    console.log('someone connected.');

    socket.on('sync', sync);
    socket.on('send', send);

    function sync(id) {
      socket.join(id);
      console.log('someone joined ' + id);
    }

    function send(id, msg) {
      io.sockets.in(id).emit(msg);
      console.log('someone sent ' + msg + ' to ' + id);
    }
  });

  return server;
}).listen(3001, function() {
  console.log('socket.io server on 3001')
});

和一个简单的客户端:

var socket = require('socket.io-client')('http://localhost:3001');

socket.on('connect', function() { 
  console.log('connected')
  socket.emit('sync', 'secret') 
});

工人开始工作良好。 http服务器工作正常。但是,当客户端连接时,控制台会记录有人连接,仅此而已。客户端永远不会触发on connect事件,因此我认为升级/握手失败。如果有人能发现我在做什么错,那将有很大帮助。

The workers start up fine. The http servers work fine. But when the client connects, the console logs 'someone connected' and nothing more. The client never fires the on connect event, so I think the upgrade/handshake is failing or something. If anyone can spot what I am doing wrong that would help alot.

谢谢!

推荐答案

@jordyyy:我遇到了同样的问题谷歌搜索后,我喜欢回答。
Socket.Io握手任务在多个请求中完成,当您在粘性会话上运行时,意味着您正在根据自己的核心使用多个进程。

@jordyyy : I was facing same issue after googling I have fond answer. Socket.Io handshaking task complete in more than one request and when you will run on sticky session it means you are using multiple process according to your core.

因此,握手请求将分布在不同的进程上,并且它们无法通话。(不是IPC)(它们是子进程),大多数情况下连接将失败/丢失。(connection-断开事件频繁发生)

那是什么解决方案?解决方法是 socketio-sticky-session

So what is solution ? Solution is socketio-sticky-session

Socketio-sticky-session,管理基于IP的连接。因此,当您将由任何客户端请求时,它将维护与流程/工作人员相关的ip地址。因此,进一步的请求将转发给同一进程/工作人员,并且您的连接已正确稳定。

Socketio-sticky-session, manage connection on IP based. So when you will request by any client then it will maintain ip address with respect process/worker. So further request will be forward to same process/worker and your connection properly stabilized.

当使用redies适配器时,您实际上可以维护套接字
连接数据不包括所有过程/工人。

And When you will use redies adapter then you can actually maintain socket connection data b/w all processes/workers.

更多信息
https://github.com/elad/node-cluster-socket.io
(如果服务器支持IPv6,则需要对worker_index方法进行一些修补)

For more information https://github.com/elad/node-cluster-socket.io (you need some patch on worker_index method, if your server is supporting IPv6)

仅知识字节。 :):)

Just knowledge bytes. :) :)

还有一件事,您无需进行分叉处理。这将通过粘性会话来完成。

One more thing, you don't need to fork process. It will be done by sticky session.

这篇关于Socket.io“握手”失败,出现群集和粘性会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆