在React-native上将2台设备与WebRTC连接

时间:2018-10-03 10:06:13

标签: react-native webrtc

我想在react-native上连接2个设备。对于发送报价,我使用net模块来创建小型服务器。

结果是两个设备之间没有连接:/

对于第一个设备,我创建一个要约:

  createOffer() {
    const { pc } = this;

    return new Promise((resolve) => {
      pc.createOffer(offer => {
        pc.setLocalDescription(
          offer,
          () => resolve(offer),
          (error) => this.logError(`pc.setLocalDescription ${error}`)
          );
      }, (error) => this.logError(`pc.createOffer ${error}`)
      );

      pc.onicecandidate = ({ candidate }) => {
        alert(`pc.onicecanddate ${candidate}`);
      };

      pc.oniceconnectionstatechange = event => {
        alert('oniceconnectionstatechange');
      };
    });
  }

对于第二个设备,我使用此方法发送答案:

  createAnswer() {
    const { pc } = this;

    return new Promise((resolve) => {
      pc.createAnswer(offer => {
        pc.setLocalDescription(
          offer,
          () => resolve(offer),
          (error) => this.logError(`pc.setLocalDescription ${error}`)
          );
      }, (error) => this.logError(`pc.createOffer ${error}`)
      );

      pc.onicecandidate = ({ candidate }) => {
        alert(`pc.onicecanddate ${candidate}`);
      };

      pc.oniceconnectionstatechange = event => {
        alert('oniceconnectionstatechange');
      };
    });
  }

  onicecandidate({ candidate }) {
    console.log('onicecandidate', candidate);

    if (candidate) {
      alert(candidate);
    }
  }

  addIceCandidate(candidate) {
    alert(JSON.stringify(candidate));
  }

  oniceconnectionstatechange(event) {
    alert('oniceconnectionstatechange');
  }

当第二台设备发送报价时,我为第一台设备运行此方法:

  setRemoteDescription(receiverOffer) {
    const { pc } = this;
    pc.addIceCandidate(new RTCIceCandidate(receiverOffer));
  }

但是我没有任何结果。我不太明白为什么无法建立连接。我可能迈出了糟糕的一步,但我不知道是哪一步。

有人可以帮助我吗?

谢谢社区!

0 个答案:

没有答案