本文介绍了如何在 reactJs 中做 webRTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 webRTC 技术包含到我现有的 reactJs 应用程序中

Am trying to include a webRTC tech to my already existing reactJs App

问题是 react 无法识别 webRTC API

the problem is that react is not recognizing the webRTC API's

  Line 185:19:  'webkitRTCPeerConnection' is not defined  no-undef
  Line 191:1:   'rtcPeerConn' is not defined              no-undef
  Line 212:3:   'rtcPeerConn' is not defined              no-undef
  Line 214:62:  'rtcPeerConn' is not defined              no-undef

这个函数在一个功能性的反应组件中

this function is inside a functional react component

 function startSignaling(){
  displayMessage("start signaling...");
rtcPeerConn = new webkitRTCPeerConnection(configuration)
//send ice candidate to other peer
      rtcPeerConn.onicecandidate  = function(evt){
          if(evt.candidate){
              io.emit("signal",{"type":"ice candidate","message":JSON.stringify({'candidate':evt.candidate}),room:signal_room})
              displayMessage("completed that ice candidate");
          }
      }
rtcPeerConn.onnegotiationneeded = function(){
  displayMessage("on negotiationnneded");
  rtcPeerConn.createOffer(sendLocalDesc,logerror);

}
rtcPeerConn.onaddstream = (evt,err)=>{
  displayMessage("creating  the other stream");
  if(err){
      displayMessage(err)
  }
  success2(evt.stream);
}

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

      navigator.mediaDevices.getUserMedia({video:true,audio:true}).then(stream=>{
          success(stream);
        //  rtcPeerConn.addStream(stream);
      }).catch(err=>{
          logerror(err);
          });

}

推荐答案

问题不在于 React 无法识别 WebRTC API,而是您没有定义 rtcPeerConn 变量.此外,此 API 不需要 webkitRTCPeerConnection(供应商前缀),请改用 RTCPeerConnection.

The problem is not with React not recognizing the WebRTC APIs, you didn't define the rtcPeerConn variable. Also, webkitRTCPeerConnection (vendor prefixes) are not required for this API, use RTCPeerConnection instead.

rtcPeerConn = new webkitRTCPeerConnection(configuration) 行替换为:

let rtcPeerConn = new RTCPeerConnection(configuration);

这篇关于如何在 reactJs 中做 webRTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:15