套接字IO服务器到服务器 [英] Socket IO Server to Server

查看:153
本文介绍了套接字IO服务器到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

让它加入房间,接收io.sockets。在( '大房间')。发射()。还有更多吗?

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

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

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

嘿Brad,这是我在下面的完整.js应用程序供参考:

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

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


推荐答案

是的,绝对的。只需在服务器应用程序中直接使用Socket.IO客户端。

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

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

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

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

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

这篇关于套接字IO服务器到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆