### 问题描述
一个方法的回调中,有一个stream流,我想把这个流赋值到全局对象,但是赋值后无法使用了,只能在当前作用域里使用
### 相关代码
initSSH() {
const { Client } = window.require("ssh2");
const conn = new Client();
conn.on("ready", () => {
console.log("Client :: ready");
conn.shell((err, stream) => {
if (err){
this.term.write(err)
conn.end()
}
console.log("stream",stream)
// 这里把stream赋值给this.sshClientStream后 无法使用
this.sshClientStream = stream;
stream
.on("close", () => {
console.log("Stream :: close");
conn.end();
})
.on("data", (data) => {
// console.log("OUTPUT: " + data);
this.term.write(data);
this.term.scrollToBottom();
});
this.bindTermOnKey()
});
})
conn.connect({
host: "127.0.0.1",
port: 12,
username: "root",
password: "root",
});
},
bindTermOnKey(){
console.log("sshClientStream",this.sshClientStream)
this.term.onKey((e) => {
console.log(e);
console.log(e.key);
this.sshClientStream.cork();
this.sshClientStream.write(e.key);
this.sshClientStream.uncork();
});
},
### 你期待的结果是什么?实际看到的错误信息又是什么?
在其他方法中使用this.sshClientStream.write(xxx)直接报错
打印了赋值前后对stream进行对比,发现赋值前是Channel,赋之后this.sshClientStream变成了Proxy
调用write错误信息如下
internal/crypto/cipher.js:157 Uncaught TypeError: Illegal invocation
at Proxy.update (internal/crypto/cipher.js:157)
at send_ (/Users/shishuaibiao/Documents/文稿 - 时帅彪的MacBook Pro/ElectronProjects/okshell/okshell/node_modules/[email protected]@ssh2-streams/lib/ssh.js:5047)
at send (/Users/shishuaibiao/Documents/文稿 - 时帅彪的MacBook Pro/ElectronProjects/okshell/okshell/node_modules/[email protected]@ssh2-streams/lib/ssh.js:4961)
at Proxy.SSH2Stream.channelData (/Users/shishuaibiao/Documents/文稿 - 时帅彪的MacBook Pro/ElectronProjects/okshell/okshell/node_modules/[email protected]@ssh2-streams/lib/ssh.js:969)
at Proxy.Channel._write (/Users/shishuaibiao/Documents/文稿 - 时帅彪的MacBook Pro/ElectronProjects/okshell/okshell/node_modules/[email protected]@ssh2/lib/Channel.js:347)
at doWrite (_stream_writable.js:435)
at clearBuffer (_stream_writable.js:574)
at Proxy.Writable.uncork (_stream_writable.js:329)
at eval (HelloWorld.vue?fdab:108)
at e.fire (xterm.js?7243:1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…