2017-02-13 79 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

确定给我一些时间我会用代码更新我的答案 –