[英]How do I send message to another socket.io server using socket.io-redis?
I need to have two different socket.io servers communicate with each other. 我需要有两个不同的socket.io服务器相互通信。 I cannot use
socket.io-client
since it does not differentiate between browser to server
connections and server to server
connections. 我不能使用
socket.io-client
因为它不区分browser to server
连接和server to server
连接。 So I am trying to use socket.io-redis
. 所以我试图使用
socket.io-redis
。
One is an express-socket.io server and another is a standalone socket.io server. 一个是express-socket.io服务器,另一个是独立的socket.io服务器。 Both have been configured to use
socket.io-redis
adapter. 两者都已配置为使用
socket.io-redis
适配器。 I do not see message received at Server1 from Server2. 我没有看到Server2从Server2收到消息。 Also there are no errors.
也没有错误。
Server1 : Server1:
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); }); });
Server2: 服务器2:
var io = require('socket.io')(3000); var redis = require('socket.io-redis'); io.adapter(redis({ host: 'localhost', port: 6379 })); io.emit('message','Hi');
socket.io-redis will not allow You to capture events from one server on another. socket.io-redis不允许您从一个服务器捕获另一个服务器上的事件。 How it works is it will simply "push" messages emited by one server to other connected servers, so those can emit those messages to.
它是如何工作的,它只是将一台服务器发出的消息“推送”到其他连接的服务器,因此可以将这些消息发送到。
If instance A will recieve an event (ie. io.on('connect')
), You will not be able to capture that event on instance B. You will however be able to emit a message to all clients connected to all instances, simply by calling 如果实例A将收到一个事件(即
io.on('connect')
),您将无法在实例B上捕获该事件。但是,您将能够向连接到所有实例的所有客户端发送消息,只需打电话
socket.on('message', function(socket){
socket.emit('hello', 'New message');
}
This way You will broadcast the message to all clients, either connected to instance A or B (including Yourself for that matter). 这样您就可以将消息广播到所有客户端,这些客户端连接到实例A或B(包括您自己)。 This approach allows You to scale out Your application to other instances on one machine (utilize more than one thread) or to other servers.
此方法允许您将应用程序扩展到一台计算机上的其他实例(使用多个线程)或其他服务器。
If You need Your servers to "talk" to each other, You can utilize Your existing transport layer - Express server. 如果您需要服务器彼此“交谈”,您可以利用现有的传输层 - Express服务器。 You could create handlers for different type of requests, for example:
您可以为不同类型的请求创建处理程序,例如:
app.get('/api/clientCount', function(req, res){
res.send(io.engine.clientsCount);
});
This way You can exchange information between socket.io instances, change their state, make all instances report usage, etc. Thing to remember - authenticate requests; 这种方式您可以在socket.io实例之间交换信息,更改其状态,使所有实例报告使用情况等。要记住 - 验证请求; You don't want to get those calls from unauthorised users :)
您不希望未经授权的用户接到这些电话:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.