Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

stream流复制之后write方法无法使用

### 问题描述

一个方法的回调中,有一个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)直接报错

image.png

打印了赋值前后对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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我不太清楚strem.write()里具体源代码是什么,但是根据你的描述和报错信息,可以大概率猜测是因为代理后this指向出了问题,你可以打断点到具体报错的代码行看看是不是这个原因。具体可以参考https://juejin.cn/post/684490...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...