webRTC错误未定义配置
webRTC error undefined configuration
我的client.js文件中有以下内容。。。
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,如图所示。。。
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};
当按下按钮时,start(isCaller)
被调用,然后转到这一行。。。
peerConnection = new RTCPeerConnection(peerConnectionConfig);
从而抛出错误。为什么?
这是正常的作用域问题,您在window.onload
匿名函数中声明了变量,并试图在其他函数中直接访问它,因此出现了错误。
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- webRTC错误未定义配置
- 递归 js v3 未捕获的类型错误:无法读取未定义的属性“配置”
- 余烬检查器配置未定义错误
- 描述未定义,我的配置文件无法在量角器上运行
- 未定义的颜色配置文件
- 访问配置文件中的baseURL显示未定义的值
- Meteor.user().profile返回'配置文件'未定义