2013-01-02 45 views
26

服務器可以使用Socket.IO連接到另一個服務器並被視爲客戶端?套接字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'); 
}); 
+0

可能重複的[Node.js客戶端爲一個socket.io服務器](https://stackoverflow.com/questions/10703513/node-js-client-for-a-socket-io-server) – StefansArya

回答

35

是的,絕對。只需直接在服務器應用程序中使用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' }); 
}); 
+0

嘿嘿Brad,是否有可能接收到一個消息socket.on('message'),然後使用Socket IO將該消息轉發給第二個服務器?謝謝,-T – Taurian

+0

是的,您可以像使用客戶端一樣使用Socket.IO客戶端服務器端。你可以做任何你想要的消息。 – Brad

+0

好吧,我把我的應用程序,很容易複製/粘貼和運行。你能幫我填補空白嗎?如何連接到第二臺服務器?謝謝,-T – Taurian

14

我意識到這是一個老帖子,但我正在做類似的東西,並決定回來,有所貢獻,因爲它讓我思考..

這裏有一個基本的客戶端 - >服務器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); 
    }); 
}); 

我正在使這篇文章成爲一個社區Wiki,以便有人可以改善這一點,如果他們覺得喜歡它。

!!該代碼尚未通過測試,請自擔風險!

+0

美麗的男人!這讓我有了一個socket.io集羣設置。非常感謝你。 –

+0

真的幫了我,謝謝! – FirstLegion