3

我正在尝试使用socket.io在NodeJS(集群架构,单独的VM)中建立服务器到服务器的通信。我尝试使用这里发布的内容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(用于缓存失效等):

在此处输入图像描述

4

1 回答 1

1

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

于 2016-05-09T15:51:45.853 回答