我有两个NodeJS文件:
global.clients = [];
const net = require('net');
const ki = require('./keyboard_interface.js');
net.createServer(function (socket)
{
// Put this new client in the list
clients.push(socket);
});
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line)
{
if (line == 'c')
{
console.log('----Connected Clients----');
clients.forEach(function (socket)
{
console.log(socket.name);
});
}
});
我已经验证了clients数组中是否填充了内容,我可以从keyboard_interface.js
访问clients变量,但是为什么它为空?
答案 0 :(得分:0)
使用global.clients
访问keyboard_interface.js
中的数组