webRTC错误未定义的配置 [英] webRTC error undefined configuration

查看:804
本文介绍了webRTC错误未定义的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的client.js文件中包含以下内容...

I have the following in my client.js file...

window.onload = function() {


  var peerConnection;
  var peerConnectionConfig = {
    'iceServers': [{
      'url': 'stun:stun.services.mozilla.com'
    }, {
      'url': 'stun:stun.l.google.com:19302'
    }]
  };

  navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
  window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
  window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;


  var video = document.getElementById("video");
  var remote = document.getElementById("remoteVideo");
  var cameraStream = "";

  serverConnection = new WebSocket('ws://127.0.0.1:3434');
  serverConnection.onmessage = gotMessageFromServer;


  serverConnection.onmessage = gotMessageFromServer;

  var constraints = {
    video: true,
    audio: false,
  };

  if (navigator.getUserMedia) {
    navigator.getUserMedia(constraints, getUserMediaSuccess, getUserMediaError);
  } else {
    alert('Your browser does not support getUserMedia API');
  }



}

function getUserMediaSuccess(stream) {
  localStream = stream;
  video.src = window.URL.createObjectURL(stream);
}

function getUserMediaError(error) {
  console.log(error);
}

function connect() {
  start(true);
}



function start(isCaller) {

  peerConnection = new RTCPeerConnection(peerConnectionConfig);
  peerConnection.onicecandidate = gotIceCandidate;
  peerConnection.onaddstream = gotRemoteStream;
  peerConnection.addStream(localStream);

  if (isCaller) {
    peerConnection.createOffer(gotDescription, createOfferError);
  }
}

function gotDescription(description) {
  console.log('got description');
  peerConnection.setLocalDescription(description, function() {
    serverConnection.send(JSON.stringify({
      'sdp': description
    }));
  }, function() {
    console.log('set description error')
  });
}

function gotIceCandidate(event) {
  if (event.candidate != null) {
    serverConnection.send(JSON.stringify({
      'ice': event.candidate
    }));
  }
}

function gotRemoteStream(event) {
  console.log("got remote stream");
  remote.src = window.URL.createObjectURL(event.stream);
}

function createOfferError(error) {
  console.log(error);
}

function gotMessageFromServer(message) {
  if (!peerConnection) start(false);


  var signal = JSON.parse(message.data);
  if (signal.sdp) {
    peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
      peerConnection.createAnswer(gotDescription, createAnswerError);
    });
  } else if (signal.ice) {
    peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
  }
}

我得到的错误是client.js:60 Uncaught ReferenceError: peerConnectionConfig is not defined

我在顶部定义了peerConnectionConfig,如图所示...

I define peerConnectionConfig at the top, as shown...

var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};

然后在按下按钮时调用start(isCaller),然后转到该行...

And start(isCaller) is called when a button is pressed, which then goes to this line...

peerConnection = new RTCPeerConnection(peerConnectionConfig);

...这将引发错误.有什么原因吗?

...which throws the error. Any reason as to why?

推荐答案

这是正常的作用域问题,您已经在window.onload匿名函数中声明了变量,并尝试在其他函数中直接访问它,因此出现错误.

it is normal scoping issue, you have declared variables in window.onload anonymous function and trying to access it directly in other functions, thus the error.

这篇关于webRTC错误未定义的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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