简体   繁体   English

如何使用socket.io-redis将消息发送到另一个socket.io服务器?

[英]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.

相关问题 如何从socket.io-redis发送私人消息(emit) - How to send private message (emit) from socket.io-redis 如何在具有多个节点和 socket.io-redis 的 socket.io 中检查套接字是否处于活动状态(已连接) - How to check socket is alive (connected) in socket.io with multiple nodes and socket.io-redis Node.js和socket.io-redis(socket.io 1.0.6):如何重新订阅? - nodejs and socket.io-redis (socket.io 1.0.6): how to redis subscribe? 使用 pm2 的集群模块与 socket.io 和 socket.io-redis - Using pm2's cluster module with socket.io and socket.io-redis Socket.io:如何使用Socket.io-redis适配器计算房间中的客户端 - Socket.io: How to count clients in a room with Socket.io-redis adapter 如何使用socket.io将TCP消息发送到远程服务器 - how to send tcp message to remote server using socket.io 如何通过 socket.io 将图像发送到服务器? - How do I send image to server via socket.io? socket.io with socket.io-redis:获取房间中的所有套接字 object - socket.io with socket.io-redis: get all socket object in a room Socket.io-redis 在作为适配器添加到 Socket.io 实例时出错 - Socket.io-redis gives error when added as adapter to Socket.io instance 使用socket.io / socket.io-redis / rediscloud / node.js在Heroku上的2个测功机出错 - Errors going to 2 dynos on Heroku with socket.io / socket.io-redis / rediscloud / node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM