【问题标题】:NodeJS Socket.io Server<-->Server communicationNodeJS Socket.io 服务器<-->服务器通信
【发布时间】:2016-08-20 20:01:12
【问题描述】:

我正在尝试使用 socket.io 在 NodeJS(集群架构,单独的虚拟机)中建立服务器到服务器的通信。 我尝试使用这里发布的内容http://socket.io/docs/using-multiple-nodes/

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

所以我假设(可能是错误的)在执行io.emit("message", "some example message") 时我可以通过以下方式监听它:

 io.on('connection', function(socket){
  console.log("io.on");
  socket.on('message', function(msg){
    console.log('message: ' + msg);
  });
});

当运行一台服务器 (node_app) 并在另一台服务器上发送事件时,我看到调试消息:

socket.io-parser encoding packet {"type":2,"data":["message","some example message"],"nsp":"/"} +6s
  socket.io-parser encoded {"type":2,"data":["message","some example message"],"nsp":"/"} as 2["message","some example message"] +0ms

我想实现每个node_app之间的通信(用于缓存失效等):

【问题讨论】:

    标签: node.js redis socket.io scalability socket.io-redis


    【解决方案1】:

    对于那些正在寻找类似案例的解决方案的人: 我发现这篇文章http://code.tutsplus.com/tutorials/multi-instance-nodejs-app-in-paas-using-redis-pubsub--cms-22239 基本上回答了我的问题。 通过 Redis 发布/订阅机制,我实现了我想要的。 :)

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2020-04-09
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2014-03-06
      • 2013-07-01
      • 2016-04-19
      • 2016-01-01
      相关资源
      最近更新 更多