Javascript 套接字 IO 服务器到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14118076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:46:24  来源:igfitidea点击:

Socket IO Server to Server

javascriptsocket.io

提问by Taurian

Is it possible for a server to connect to another using Socket.IO and be treated like a client?

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

And have it join rooms, recieve io.sockets.in('lobby').emit(). And more?

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

The first server is also listening for connections/messages as well.

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

Hey Brad, here's my full .js app below for reference:

嘿布拉德,这是我的完整 .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');
});

回答by Brad

Yes, absolutely. Just use the Socket.IO client in your server application directly.

是的,一点没错。直接在您的服务器应用程序中使用 Socket.IO 客户端。

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

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

You can install it with npm install socket.io-client. Then to use:

您可以使用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' });
});

回答by Destreyf

I realize this is an old post, but i was working on something similar and decided to come back and contribute something as it got me thinking..

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

Here's a basic Client -> Server 1 -> Server 2 setup

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

Server #1

服务器 #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://domain.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
    });
});

And here's Server #2

这是服务器 #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 dunno what we should do with this...
    });
});

This is our Client, who sends the original message.

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

// 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);
    });
});

I'm making this post a Community Wiki so that someone can improve this if they feel like it.

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

!!THE CODE HAS NOT BEEN TESTED, USE AT YOUR OWN RISK!!

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

回答by Alisson R. Perez

I had the same problem, but instead to use socket.io-clientI decided to use a more simple approach (at least for me) using redis pub/sub, the result is pretty simple.

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

You can take a look at my solution here: https://github.com/alissonperez/scalable-socket-io-server

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

With this solution you can have how much process/servers you want (using auto-scaling solution), you just use redis as a way to forward your messages between your servers.

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