Node.js脚本不会退出

时间:2018-01-29 12:51:30

标签: node.js node.js-addon

我是J的noob,但是通过编写api来将文件从irc传输到我的服务器,试图让生活更轻松 经过一些挖掘,我得到了一个下载文件的模块(下面的代码基于npm xdcc),但是当我运行模块时,它在完成命令后不会退出 例如。吐出'下载完成'后,我必须按Ctrl + C退出

var irc = require('xdcc').irc;
var ProgressBar = require('progress');
var progress;


var connectIRC = function (bot, pack) {
    var user = 'desu' + Math.random().toString(36).substr(7, 3);

    var start = 0;

    console.log('Connecting...');

    var client = new irc.Client('irc.rizon.net', user, {
        channels: ['#nibl'],
        userName: user,
        realName: user
    });

    client.on('join', function(channel, nick, message) {
        if (nick !== user) return;
        console.log('Joined', channel);
        client.getXdcc(bot, 'xdcc send #' + pack, '.');
    });

    client.on('xdcc-connect', function(meta) {
        console.log('Connected: ' + meta.ip + ':' + meta.port);
        progress = new ProgressBar('Downloading... [:bar] :percent, :etas remaining', {
            incomplete: ' ',
            total: meta.length,
            width: 20
        });
    });

    var last = 0;
    client.on('xdcc-data', function(received) {
        progress.tick(received - last);
        last = received;
    });

    client.on('xdcc-end', function(received) {
        console.log('Download completed');
    });

    client.on('notice', function(from, to, message) {
        if (to == user && from == bot) {
            console.log("[notice]", message);
        }
    });
    client.on('error', function(message) {
        console.error(message);
    });
};

module.exports.connectIRC = connectIRC;

当irc机器人没有发送任何通知时,文件名也用双引号括起来 例如。 包名= Node.mkv

如果bot发送通知,则文件下载名称= Node.mkv

如果bot没有发送任何通知,则name =“Node.mkv”(包括双引号)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

如果要自动关闭Node JS应用程序,则必须调用全局进程对象的exit方法,以便:

  client.on('xdcc-end', function(received) {
        console.log('Download completed');
        process.exit()
    });