在peer.js上的peer之间无法关闭连接 [英] Cant close connection beetween peer on peerjs

查看:53
本文介绍了在peer.js上的peer之间无法关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作视频聊天应用程序,它可与peerjs一起使用.现在,我想添加一些功能,例如,如果连接被用户破坏,则另一个连接到他的用户会收到通知.

i'm trying to make video chat app, and it works with peerjs. Now i wanna add some functions like if connection is destroy by user, the other user that connected to him got notification.

这是我呼叫其他用户的代码

here's my code for calling other user

 call(userId: string, partnerId: string) {
    const conn = this.peer.connect(partnerId);
    conn.on('open', () => {
      conn.on('data', (data) => {
        console.log(data);
      })
      conn.send(userId);
    })
    if (this.peer.destroyed) {
      this.createPeer(this.userId);
    }

    if (this.myEl.classList.contains('disableCall')) {
      console.log('Show');
      this.myEl.classList.add('showCall');
      this.partnerEl.classList.add('showCall');
      this.myEl.classList.remove('disableCall');
      this.partnerEl.classList.remove('disableCall');
    }
    this.hideCallLogin = false;
    const call = this.peer.call(partnerId, this.myStream);
    this.mediaConnection = call;
    call.on('stream', (stream) => {
      this.partnerEl.srcObject = stream;
      this.hideCall = true;
      this.hideCallLogin = false;
      if (call.peerConnection.connectionState == 'connecting') {
        this.swapVideo('my-video');
      }
    });
    call.on('close', () => {
      console.log('Di hentikan oleh penerima');
      this.mediaConnection.close();
      this.myEl.classList.remove('showCall');
      this.partnerEl.classList.remove('showCall');
      this.myEl.classList.add('disableCall');
      this.partnerEl.classList.add('disableCall');
      this.hideCall = false;
      this.hideCallLogin = false;
    })
  }

这是其他用户的回答

wait() {
    this.peer.on('call', (call) => {
      this.mediaConnection = call;
      //change this to a modal for confirm a call
      var acceptsCall = confirm(partner + " is calling, do you want to accept it ?");
      if (acceptsCall) {
        call.answer(this.myStream); // Answer the call with an A/V stream.
        call.on('stream', (stream) => {
          if (this.myEl.classList.contains('disableCall')) {
            console.log('Show');
            this.partnerEl.classList.add('showCall');
            this.myEl.classList.add('showCall');
            this.partnerEl.classList.remove('disableCall');
            this.myEl.classList.remove('disableCall');
          }
          this.partnerEl.srcObject = stream;
          this.status = 'Connected';
          this.hideCallLogin = false;
          this.swapVideo('my-video');
        });
        call.on('close', () => {
          console.log('Di hentikan oleh penelpon');
          this.myEl.classList.remove('showCall');
          this.partnerEl.classList.remove('showCall');
          this.myEl.classList.add('disableCall');
          this.partnerEl.classList.add('disableCall');
          console.log(this.status);
          this.hideCall = false;
          this.hideCallLogin = false;
        })
      }
    });
    //getting partner id
    let partner = '';
    this.peer.on('connection', (conn) => {
      conn.on('open', () => {
        conn.on('data', (data) => {
          partner = data;
        })
      })
    })

  }

我想要的是,如果1个同伴挂断并自动关闭其视频,则同伴断开连接或销毁,任何人都知道该怎么做?

what i want is, peers disconnect or destroy if 1 peer hangup and automatically close their video, anyone know how to do that?

推荐答案

wait() {
  this.peer.on('call', (call) => {
    this.mediaConnection = call; // Save reference to the `mediaConnection` object

    var acceptsCall = confirm("Videocall incoming, do you want to accept it ?");
    if (acceptsCall) {
      call.answer(this.myStream); // Answer the call with an A/V stream.
      call.on('stream', (stream) => {
        this.partnerEl.srcObject = stream;
        this.status = 'Connected';
        console.log(this.status);
      });
    }
  });
}

然后,当您要挂断电话时,应该可以调用 this.mediaConnection.close()

Then when you want to hang up you should be able to call this.mediaConnection.close()

要使其在调用方端起作用,您还必须在其中存储mediaConnection.

To have it working on the caller end you have to store mediaConnection there as well.

call(partnerId: string) {
  this.partnerId = partnerId;
  console.log(this.peer.destroyed);

  if (this.peer.destroyed) {
    this.createPeer(this.userId);
  }
  
  const call = this.peer.call(partnerId, this.myStream);
  this.mediaConnection = call; // Save reference to the `mediaConnection` object

  call.on('stream', (stream) => {
    this.partnerEl.srcObject = stream;
    this.hideCall = true;
    this.hideCallLogin = false;
  });
}

这篇关于在peer.js上的peer之间无法关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆