node.js - 使用(socket.io + RedisStore)跨多个服务器进行通信

标签 node.js redis socket.io load-balancing aws-opsworks

我正在使用 Node.js 和 Socket.io 开发一款多人在线游戏。我希望有很多玩家加入游戏,所以我将其托管在 Amazon Opworks 上。

问题是服务器无法将套接字事件发送到连接到不同服务器的客户端。我正在使用 RedisStore 来管理 socket.io session 。我相信 RedisStore 和 socket.io 以无缝的方式处理了这种服务器间通信。这是对另一个问题的引用:How does socket.io send messages across multiple servers?

但事实并非如此。如果消息位于不同的服务器上,则不会传递给其他客户端;如果只有一台服务器,该应用程序可以运行,但如果我使用 Opsworks 上使用 ELB 进行负载平衡的多台服务器,该应用程序将失败。

这只是整个代码的摘录。如果有语法错误等,请忽略。

app.js

    //Redis Client Initialization
   var redis = require("redis");
   redis_client = require('redis-url').connect('redis://xxxxx');

//setting RedisStore for socket.io 

var RedisStore = require('socket.io/lib/stores/redis')
, redis  = require('socket.io/node_modules/redis')
, pub    = redis.createClient(11042,'-----')
, sub    = redis.createClient(11042,'-----')
, client = redis.createClient(11042,'-----');

// using RedisStore in combo with Socket.io to allow communications across multiple servers

io.set('store', new RedisStore({
  redis    : redis,
  redisPub : pub,
  redisSub : sub,
  redisClient : client
}));

//socket communication specific code 


io.of('/game')
.on('connection', function (socket) {

  socket.on('init' , function(data){

    var user_id = data.user_id; // collecting user_id that was sent by the client
    var socket_id = socket.id;
    redis_client.set("user_socket:"+user_id, socket_id, function(err, reply){

          //stored a referece to the socket id for that user in the redis database

    });

  });

  socket.on('send_message', function(data){

      var sender = data.sender_id;

      var reciepient = data.reciepient_id ; // id of the user to whom message is to be sent to
      redis_client.get("user_socket:"+reciepient, function(err,socket_id){

        if(socket_id){

            var socket = io.of('/game').sockets[socket_id];
            socket.emit("message", {sender : sender}); // This fails. Messages to others servers dont go through.

        }
      })

  })

});

最佳答案

您不能直接广播到其他服务器上的套接字对象。 Redis 所做的是允许您向其他服务器上的“房间”广播。值得庆幸的是,在 socket.io 1.x 中,新连接会自动加入一个以其套接字 ID 命名的房间。要解决您的问题,请更改:

    if(socket_id){

        var socket = io.of('/game').sockets[socket_id];
        socket.emit("message", {sender : sender}); // This fails. Messages to others servers dont go through.

    } 

发射到房间而不是在套接字对象上调用发射:

   if(socket_id){

        io.to(socket_id).emit("message", {sender : sender}); // This fails. Messages to others servers dont go through.

    }

你可能会有更多的运气。

关于node.js - 使用(socket.io + RedisStore)跨多个服务器进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259239/

相关文章:

ruby-on-rails - 在 sidekiq 中标记作业以便将来快速搜索和删除

javascript - 使用 gulp 和 wiredep,socket.io 不会添加到 index.html(即使它在 bower.json 中)

php - 如何在node.js中全局声明数据库连接

python - 类似 Python 2.7 + Bottle 框架的 socket.io

javascript - HTTP 错误 307 - Node.js (Express)、passport.js、mongoose.js 和 Android 应用程序中的临时重定向

javascript - 通过过滤未定义计算对象数组的平均值

ruby-on-rails-4 - Rails 4.2:sidekiq 无法连接到 Redis(Errno::ECONNREFUSED),由 foreman 启动

javascript - Node.js 多个异步函数

node.js - 呃!堆栈错误: `gyp` failed with exit code: 1

ruby-on-rails - 带 Rails 的 Redis