WebSocket握手Node.JS,Socket.IO和群集不起作用 [英] WebSocket handshake in Node.JS, Socket.IO and Clusters not working

查看:218
本文介绍了WebSocket握手Node.JS,Socket.IO和群集不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node.js,socket.io和node.js集群来聚类应用程序时遇到问题。

I having a problem with clustering my application with Node.js, socket.io and node.js clusters.

我使用socket.io-redis来共享所有员工的信息,但不工作。

I using the socket.io-redis to share the information for all workers, but is not working.

我的代码:

var cluster   = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {   
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {      
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {

    ...

         var express   = require("express");
         //Server
         var server = express();
         //Socket.io
         var http  = require('http').Server(server);
         var io    = require('socket.io')(http);
         var redis_io = require('socket.io-redis'); 
         var redis = require("redis");

         io.adapter(redis_io({host: "127.0.0.1", port: 6379 })); 

    ...
}

在客户端,我得到连接建立之前,关闭400错误或WebSocket的握手错误。

In client, i get errors in handshake like 400 error or WebSocket is closed before the connection is established.

我可以做什么来解决这个问题?

What i can do to solve this?

我使用最后一个版本的node.js和套接字。 io

Im using the last version of node.js and socket.io

谢谢!

推荐答案

我有同样的问题花了我一段时间来弄清楚。一些研究解释说,这是因为一些传输,像长时间轮询,需要进行多个请求以建立最佳的连接。在请求之间存在状态,因此如果不同的连续请求被路由到不同的群集工作者,则连接失败。

I had this same problem and it took me a while to figure it out. A bit of research explained that it is because some of the transports, like long-polling, need to make multiple requests in order to establish the optimal connection. There is state held across requests, so if different consecutive requests get routed to different cluster workers, the connection fails.

http://socket.io/docs/using-multiple-nodes/ 引用了一个自定义的集群模块称为 sticky-session ,其工作原理如下: https ://github.com/indutny/sticky-session

There is a page about it at http://socket.io/docs/using-multiple-nodes/ which cites a custom cluster module called sticky-session which works around this: https://github.com/indutny/sticky-session

我真的不想使用它,因为基本上忽略了所有的工作node.js团队一直在集群模块的TCP负载平衡上投资。

I really did not want to use it since that basically ignores all the work the node.js team has been investing in the TCP load balancing behind the cluster module.

由于Web Socket协议本身只需要一个连接,所以我可以通过强制 websocket 成为第一个也是唯一的运输。我可以这样做,因为我控制客户端和服务器。对于公共网页,这可能不是安全的,因为您必须担心浏览器兼容性。在我的情况下,客户端是一个移动应用程序。

Since the Web Socket protocol itself only needs a single connection, I was able to work around this by forcing websocket to be the first and only transport. I can do this because I control the client and server. For a public web page, this may not be safe for you to do since you have to worry about browser compatibility. In my case, the client is a mobile app.

这是我放入测试页面的JavaScript客户端代码(再次,真正的客户端是一个移动应用程序,所以我的网页真的只是帮助建立和测试):

Here is the JavaScript client code I put into my test page (again, the real client is a mobile app, so my web page here is really just an aid to help build and test):

var socket = io('http://localhost:8080/', {
  transports: [ 'websocket' ]
});

这篇关于WebSocket握手Node.JS,Socket.IO和群集不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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