简体   繁体   English

如何在pty.js中删除.on()函数

[英]How to remove .on() function in pty.js

I'm using pty.js in node, and I go: 我在节点中使用pty.js ,然后我去了:

if (!global.terminal)
global.terminal = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], {
            name: 'xterm-256color',
            cols: 80,
            rows: 28
        });

 var term1 = global.terminal;

 term1.on('data', function(data) { socket.emit('output1', data); console.log('on data: ' + data); });

Now, I don't ever term1.end() because I don't want the terminal session to close, even when the user closes the browser. 现在,我永远都不会term1.end(),因为我不希望终端会话关闭,即使用户关闭浏览器也是如此。 So I keep global.terminal. 所以我保持global.terminal。 However, I noticed whenever a new socket connects (Ie I refresh the browser) then the .on method gets called twice, thrice, or however many times I refreshed. 但是,我注意到只要连接了新的套接字(即刷新浏览器),. on方法就会被调用两次,三次或刷新多次。 because it keeps the .on of the last connection. 因为它保持最后连接的.on。

Basically a way to clear the .on event and start a brand new one. 基本上是一种清除.on事件并启动全新事件的方法

So what I'm looking for is a way to clear the .on event before creating a new one. 因此,我正在寻找一种在创建新事件之前清除.on事件的方法。 I'm not sure if this is a javascript/node thing or a pty.js thing: 我不确定这是javascript / node还是pty.js:

But something like: 但是类似:

 term1.on('data', null); // <-- doesn't work, clear any existing '.on' functions
 term1.on('data', function(data) { socket.emit('output1', data); console.log('on data: ' + data); });

Removing an EventEmitter's listeners can be done with the method removeAllListeners(eventName) . 可以使用removeAllListeners(eventName)方法来删除EventEmitter的侦听器。 Which in your case would look like this: 您的情况如下所示:

term1.removeAllListeners('data');

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

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