【发布时间】:2015-07-09 19:39:06
【问题描述】:
我需要两个不同的 socket.io 服务器相互通信。我不能使用socket.io-client,因为它不区分browser to server 连接和server to server 连接。所以我尝试使用socket.io-redis。
一个是 express-socket.io 服务器,另一个是独立的 socket.io 服务器。两者都已配置为使用socket.io-redis 适配器。我没有看到从 Server2 在 Server1 收到的消息。也没有错误。
服务器 1:
var express = require('express');
var app = express();
var server = app.listen(8000,function () {
console.log('server listening at port 8000');
});
var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
io.on('message',function (message) {
console.log(message);
});
io.on('connection',function (socket) {
console.log('connection');
socket.on('message',function (message) {
console.log(message);
});
});
服务器2:
var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
io.emit('message','Hi');
【问题讨论】:
-
如果事件写入正确,您是否检查了redis存储?
-
@cdagli 你能看看stackoverflow.com/questions/36874287/… 我从你的用户描述中看到你有一些socket.io的经验:)。 Koval 是否在右下方,我无法使用 socket.io-redis 与服务器通信?
标签: node.js websocket socket.io socket.io-redis