【问题标题】:Socket IO Server to ServerSocket IO 服务器到服务器
【发布时间】:2012-12-16 14:03:15
【问题描述】:

服务器是否可以使用 Socket.IO 连接到另一个服务器并被视为客户端?

让它加入房间,接收 io.sockets.in('lobby').emit()。还有更多?

第一台服务器也在监听连接/消息。

嘿,布拉德,下面是我的完整 .js 应用程序供参考:

var io = require("socket.io").listen(8099);
io.set('log level', 1);

io.sockets.on("connection", function (socket) {

    console.log('A Client has Connected to this Server');

    //Let Everyone Know I just Joined   
    socket.broadcast.to('lobby').emit("message",'UC,' + socket.id); // Send to everyone in Room but NOT me  


socket.on("message", function (data) {

//Missing code
socket2.send('message,' + data); //Forward Message to Second Server

});

socket.on("disconnect", function (data) {
    //Send Notification to Second Server
    //Need to figure out later

    //Send Notification to Everyone
    socket.broadcast.emit("message",'UD,' + socket.id ); //Send to Everyone but NOT me

    //Remove user from Session ID
    arSessionIDs.removeByValue(socket.id);      

    //Send Notification to Console
    console.log("disconnecting " + arRoster[socket.id][1]);
});

});

var io_client = require( 'socket.io-client' );
var socket2 = io_client.connect('http://192.168.0.104:8090');
socket2.on('connect', function () {
socket2.emit('C3434M,Test');
});

【问题讨论】:

标签: javascript socket.io


【解决方案1】:

是的,当然。只需在您的服务器应用程序中直接使用 Socket.IO 客户端即可。

https://github.com/LearnBoost/socket.io-client

您可以使用npm install socket.io-client 安装它。然后使用:

var socket = io.connect('http://example.com');
socket.on('connect', function () {
  // socket connected
  socket.emit('server custom event', { my: 'data' });
});

【讨论】:

  • 嘿布拉德,是否可以接收消息 socket.on('message'),然后使用 Socket IO 将该消息转发到第二个服务器?谢谢,-T
  • 是的,您可以在服务器端使用 Socket.IO 客户端,就像在客户端使用它一样。你可以对这些消息做任何你想做的事情。
  • 好吧,我建立了我的应用程序,易于复制/粘贴和运行。你能帮我填空吗?连接到第二台服务器是如何发挥作用的?谢谢,-T
  • 我真的不知道你在问什么,或者有什么空白要填写。当你在客户端对象上收到消息事件时,在你的服务器对象上发出消息和数据。此外,没有理由发送回 ACK(“收到的消息”)。这会在后台自动发生。如果您需要对 ACK 执行操作,请使用回调函数。
  • 使用第一篇文章中的代码。您究竟如何将消息发送到第二个服务器。
【解决方案2】:

我意识到这是一篇旧帖子,但我正在研究类似的东西,并决定回来并贡献一些东西,因为它让我思考。

这是一个基本的客户端 -> 服务器 1 -> 服务器 2 设置

服务器 #1

// Server 1
var io = require("socket.io").listen(8099); // This is the Server for SERVER 1
var other_server = require("socket.io-client")('http://example.com:8100'); // This is a client connecting to the SERVER 2

other_server.on("connect",function(){
    other_server.on('message',function(data){
        // We received a message from Server 2
        // We are going to forward/broadcast that message to the "Lobby" room
        io.to('lobby').emit('message',data);
    });
});

io.sockets.on("connection",function(socket){
    // Display a connected message
    console.log("User-Client Connected!");

    // Lets force this connection into the lobby room.
    socket.join('lobby');

    // Some roster/user management logic to track them
    // This would be upto you to add :)
    
    // When we receive a message...
    socket.on("message",function(data){
        // We need to just forward this message to our other guy
        // We are literally just forwarding the whole data packet
        other_server.emit("message",data);
    });
    
    socket.on("disconnect",function(data){
        // We need to notify Server 2 that the client has disconnected
        other_server.emit("message","UD,"+socket.id);
        
        // Other logic you may or may not want
        // Your other disconnect code here
    });
});

这里是服务器 #2

// Server 2
var io = require("socket.io").listen(8100);
io.sockets.on("connection",function(socket){
    // Display a connected message
    console.log("Server-Client Connected!");
    
    // When we receive a message...
    socket.on("message",function(data){
        // We got a message. I don't know, what we should do with this
    });
});

这是我们的客户,发送原始消息。

// Client
var socket = io('http://localhost');
socket.on('connect', function(){
    socket.emit("message","This is my message");
    
    socket.on('message',function(data){
        console.log("We got a message: ",data);
    });
});

我正在将此帖子设为社区 Wiki,以便有人可以根据需要对其进行改进。

代码未经测试,使用风险自负。

【讨论】:

  • 美男子!这让我登上了 socket.io 集群设置。非常感谢。
  • 让我们更进一步 - 服务器实施 mDNS 以发现网络上还有谁。所有在某个预定端口上运行的 http/s 服务器将其 http/s 服务广播到网络。当 mDNS 检测到新客户端时,它会自动尝试将其 websocket 服务器作为客户端订阅。理想情况下,所有服务器都会互相看到并订阅彼此的 websocket。如果客户端发出一个 websocket,它应该通过所有服务器工作并重新广播到这些服务器上的所有客户端。从那里你可以塑造各种有趣的服务:)
【解决方案3】:

我遇到了同样的问题,但我决定使用更简单的方法(至少对我来说)使用 redis pub/sub,而不是使用 socket.io-client,结果非常简单。

您可以在这里查看我的解决方案:https://github.com/alissonperez/scalable-socket-io-server

使用此解决方案,您可以获得所需的进程/服务器数量(使用自动扩展解决方案),您只需使用 redis 作为在服务器之间转发消息的一种方式。

【讨论】:

  • 对我来说,这是正确的答案。我意识到这个问题专门要求 socket.io,但使用 redis pub-sub 是解决 2 个不同进程/服务器之间通信问题的更好方法。 +1
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 2016-11-10
  • 2020-04-28
  • 2019-04-10
  • 1970-01-01
  • 2022-09-27
  • 2018-01-05
  • 2016-07-20
相关资源
最近更新 更多