简体   繁体   English

NodeJS中的全局变量

[英]Global vars in NodeJS

I have two NodeJS Files: 我有两个NodeJS文件:

main.js: main.js:

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);
});

keyboard_interface.js: keyboard_interface.js:

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);
                });
        }
});

I have verified that the clients array is filled with content, I can access the clients variable from keyboard_interface.js , but why is it empty? 我已经验证了clients数组充满了内容,可以从keyboard_interface.js访问clients变量,但是为什么它为空?

使用global.clients访问keyboard_interface.js的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM