是否有可能为一台服务器连接到另一台使用Socket.IO又像客户可以治疗吗?
并将其加入房,免费获赠io.sockets.in(“大房间”)。发射()。 和更多?
第一个服务器也监听连接/消息以及。
嘿布拉德,这里是我的全部应用程序名为.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');
});
是的,一点没错。 只需直接使用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' });
});
我意识到这是一个老帖子,但我工作的类似的东西,并决定回来,有所贡献,因为它让我思考..
这是一个基本的客户端 - >服务器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://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
});
});
而这里的服务器#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...
});
});
这是我们的客户,谁发送的原始邮件。
// 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);
});
});
我在做这个职位一个社区维基从而使他人能够改善这一点,如果他们觉得喜欢它。
!!代码没有经过测试,使用需要您自担风险!
我有同样的问题,而是使用socket.io-client
,我决定使用Redis的发布/订阅使用更简单的方法(至少对我来说),其结果是非常简单的。
你可以在这里看看我的解决方案: https://github.com/alissonperez/scalable-socket-io-server
有了这个解决方案,你可以有你想要的(使用自动缩放解决方案)有多少进程/服务器,您只需使用Redis的,以此来你的服务器之间转发邮件。