2017-02-13 78 views
1

好日子如何配置插座IO和插座io的客戶

我需要大量的PC,以及連接到主服務器,通過爲單位的服務器

hierarchy

我的東西,但我沒有全部完成

主服務器

socketIo = require("socket.io"), 
ioServer = socketIo(server), 
ioServer.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... 
     console.log(data); 
     console.log(data.from + " is connected with ip " + data.ip); 
     socket.emit('message', { 'from': '10.19.17.101', 'answer':'I already added you '+data.from }); 
    }); 
}); 

個服務器單位

socketIo = require("socket.io"), 
ioServer = socketIo(server), 
ioClient = require("socket.io-client")('http://10.19.17.101:7700') 
ioClient.on('connect', function(){ 
     ioClient.on('message',function(data){ 
      console.log(data.from + " answered: " + data.answer); 
      ioServer.to('pxe4').emit('message',data); 
     }); 
     ioClient.emit('message',{ 'from': 'pxe4', 'ip':'10.19.16.84' }); 
}); 

ioServer.sockets.on("connection",function(socket){ 
    // Display a connected message 
    console.log("User-Client Connected!"); 
    // When we receive a message... 
    socket.on("message",function(data){ 
     // We got a message... I dunno what we should do with this... 
     console.log(data); 
     console.log(data.from + " is connected with ip " + data.ip); 
     socket.emit('message', { 'from': '10.19.16.84', 'answer':'I already added you '+data.from }); 
     ioClient.emit("message",data); 
    }); 
    socket.on("disconnect",function(data){ 
     // We need to notify Server 2 that the client has disconnected 
     ioClient.emit("message","UD,"+socket.id); 
     // Other logic you may or may not want 
     // Your other disconnect code here 
    }); 

}); 

單位

ioClient = require("socket.io-client")('http://10.19.16.84:7770'), 
ioClient.on('connect', function(){ 
     ioClient.on('message',function(data){ 
     // We received a message from Server 2 
     // We are going to forward/broadcast that message to the "Lobby" room 
     console.log(data.from + " answered: " + data.answer); 
     }); 
     ioClient.emit('message',forsend); 
}); 

我在想,如果在這個時候我可以把從主服務器的一些信息到特定的單位?

如果有人能幫助我,我會很感激。

回答

1

從主服務器或服務器單元上的每個客戶端連接時,您將收回包含socketid的套接字對象。您必須將這些套接字標識保存在某些數據存儲中,以便快速訪問服務器信息。當必須向特定套接字發送數據時,必須從數據存儲中查詢該特定套接字併發出數據。在斷開連接時,你必須從數據存儲中取出特定的插座

+0

你能給我一個代碼示例嗎? –

+0

確定給我一些時間我會用代碼更新我的答案 –