node js:无法在MacOSx / Linux上的ssh2连接中捕获“错误”

时间:2015-12-02 15:07:16

标签: javascript linux node.js macos libssh2

this.connection = new Connection(); //ssh2 connection
        async.waterfall([function(callback) {
            // some initialization code to connect
            }
        }], function(err, opts) {
            if (err) {
                callback({
                    message: 'Error during connecting to the device...',
                    detail: err
                }, false);
            } else {
                console.log('Connecting to ... ' + JSON.stringify(self.info));
                self.connection.on('ready', function() {
                    console.info('Connected... opening a shell...');
                    callback(null, true);
                }).on('error', function(err) {
                    console.error('Error during connecting to the device: ' + err);
                    callback({
                        message: 'Error during connecting to the device...',
                        detail: err
                    }, false);
                }).on('end', function() {
                    console.info("Connection ended...");
                }).on('close', function(hadError) {
                    console.info("Connection closed..." + (hadError ? " with error" : ""));
                }).connect(opts);
            }
        });

参考上面的代码,我连接到使用ssh2连接的设备,一切正常......我正在获取“准备好”,“关闭”等控制台日志,但无法获取控制台日志当发生'错误'时。

我可以在win7 32位上捕获'error'event,但不能在MacOSx(10.9.5)或Linux(Ubuntu 12)上捕获。当我强行结束与设备的连接时(例如从我的系统中拔出lan电缆),会触发on'error'事件。

这是对Mac / Linux w.r.t ssh2模块的一些限制,还是我在捕获错误的方式上做错了。

任何指针都会非常有用。

node js version v0.10.29 ssh2版本v0.4.8

1 个答案:

答案 0 :(得分:1)

在github中提出了这个问题 https://github.com/mscdex/ssh2/issues/364

解决方案是使用keepaliveInterval,它的默认值为0.因此,为了保持我的应用程序的最佳值,它可以使用它。

相关问题