【发布时间】:2023-06-08 19:03:01
【问题描述】:
美好的一天
我需要通过一个单元服务器将很多电脑连接到一个主服务器
我有一些东西,但我没有全部完成
主服务器
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);
});
我想知道此时我是否可以将一些信息从主服务器发送到特定单元?
如果有人可以帮助我,我将不胜感激。
【问题讨论】:
标签: node.js unit-testing sockets command-line connection