node js:无法在 MacOSx/Linux 上的 ssh2 连接中捕获'error'


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位上捕获"错误"事件,但在MacOSx(10.9.5(或Linux(Ubuntu 12(上不能捕获。当我强制终止与设备的连接时,将触发on"error"事件(例如从我的系统中拔出lan电缆(。

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

任何指针都会非常有帮助。

节点 js 版本 v0.10.29SSH2 版本 v0.4.8

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

解决方案是使用keepaliveInterval,它的默认值为0。所以为了保持我的应用程序的最佳值而玩了一圈,它奏效了。

最新更新