Node.js、多线程和 Socket.io [英] Node.js, multi-threading and Socket.io

查看:41
本文介绍了Node.js、多线程和 Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Socket.io 与 本机负载平衡一起工作(集群")在 Node.js v.0.6.0 及更高版本中.

I'm looking to get Socket.io to work multi-threaded with native load balancing ("cluster") in Node.js v.0.6.0 and later.

据我了解,Socket.io 使用 Redis 来存储其内部数据.我的理解是:我们不想为每个工作人员生成一个新的 Redis 实例,而是希望强制工作人员使用与主设备相同的 Redis 实例.因此,连接数据将在所有工作人员之间共享.

From what I understand, Socket.io uses Redis to store its internal data. My understanding is this: instead of spawning a new Redis instance for every worker, we want to force the workers to use the same Redis instance as the master. Thus, connection data would be shared across all workers.

master 中是这样的:

Something like this in the master:

RedisInstance = new io.RedisStore;

我们必须以某种方式将 RedisInstance 传递给工作人员并执行以下操作:

The we must somehow pass RedisInstance to the workers and do the following:

io.set('store', RedisInstance);

受到此实现的启发,使用旧的第 3 方集群模块,我有以下非工作实现:

Inspired by this implementation using the old, 3rd party cluster module, I have the following non-working implementation:

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

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

  var sio = require('socket.io')
  , RedisStore = sio.RedisStore
  , io = sio.listen(8080, options);

  // Somehow pass this information to the workers
  io.set('store', new RedisStore);

} else {
  // Do the work here
  io.sockets.on('connection', function (socket) {
    socket.on('chat', function (data) {
      socket.broadcast.emit('chat', data);
    })
  });
}

想法?我可能完全走错了方向,有人可以指出一些想法吗?

Thoughts? I might be going completely in the wrong direction, anybody can point to some ideas?

推荐答案

实际上你的代码应该是这样的:

Actually your code should look like this:

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

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  var sio = require('socket.io')
  , RedisStore = sio.RedisStore
  , io = sio.listen(8080, options);

  // Somehow pass this information to the workers
  io.set('store', new RedisStore);

  // Do the work here
  io.sockets.on('connection', function (socket) {
    socket.on('chat', function (data) {
      socket.broadcast.emit('chat', data);
    })
  });
}

另一种选择是打开 Socket.IO 以侦听多个端口并使用 HAProxy 负载平衡之类的东西.无论如何,您知道最重要的事情:使用 RedisStore 在进程外扩展!

Another option is to open Socket.IO to listen on multiple ports and have something like HAProxy load-balance stuff. Anyway you know the most important thing: using RedisStore to scale outside a process!

资源:

http://nodejs.org/docs/latest/api/cluster.html
如何扩展 socket.io?
如何在socket.io中重用redis连接?
节点:扩展 socket.io/nowjs - 跨不同实例扩展
http://delicious.com/alessioaw/socket.io

这篇关于Node.js、多线程和 Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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