how to do webRTC in reactJs

与世无争的帅哥 提交于 2020-08-10 19:29:14

问题


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

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);
          });

}

回答1:


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.

Replace the rtcPeerConn = new webkitRTCPeerConnection(configuration) line with:

let rtcPeerConn = new RTCPeerConnection(configuration);


来源:https://stackoverflow.com/questions/60003235/how-to-do-webrtc-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!