Javascript 关闭和 WebRTC 回调问题
Javascript Closure and WebRTC callback issues
我 运行 遇到了这个问题,我正在创建闭包并使用调试器单步执行,变量 connectingClientId 在闭包回调 (localOfferCreated) 中正确设置。当 createOffer 调用回调时,connectedClientId 未定义。怎么会这样呢?为了这个我整晚都在用头撞墙。
function publishStream(handShakeInitiator, connectingClientId) {
var localOfferCreated = offerClosure(connectingClientId);
var localIceCandidate = iceClosure(connectingClientId);
peerConnections[connectingClientId] = new RTCPeerConnection(peerConnectionConfig);
peerConnections[connectingClientId].onicecandidate = localIceCandidate;
peerConnections[connectingClientId].addStream(localStream);
if (handShakeInitiator) {
peerConnections[connectingClientId].createOffer(localOfferCreated, createOfferError, offerOptions);
}
}
function offerClosure(id) {
var connectingClientId = id;
function offerCreated(description) {
peerConnections[connectingClientId].setLocalDescription(description, function (connectingClientId) {
webSocket.send(JSON.stringify({
'control': signalConstants.sendToClient,
'cid': connectingClientId,
'sdp': description
}));
}, function () {
console.log('Error setting description.');
});
};
return offerCreated;
}
从调试器中注意这些:
connectingClientId 已设置 -
调用时未设置 connectingClientId -
我在这里错过了什么?
来自RTCPeerConnection.setLocalDescription
successCallback
Is a Function without parameter which will be called
when the description has been successfully set. At this point, one can
send the offer to a remote server that can forward it to a remote
client
您正在通过将其作为内部函数参数来重新定义 connectingClientID
。请记住,命名函数参数是一个隐式变量声明,正如文档所说,它将是未定义的,因为成功回调不提供任何参数。 JavaScript 函数可以访问它们的外部范围,所以你的匿名函数不需要传递这个 arg,它可以简单地引用它创建一个闭包。
function offerCreated(description) {
peerConnections[connectingClientId].setLocalDescription(description, function() {
webSocket.send(JSON.stringify({
control: signalConstants.sendToClient,
cid: connectingClientId,
sdp: description
}));
}, function () {
console.log('Error setting description.');
});
};
我 运行 遇到了这个问题,我正在创建闭包并使用调试器单步执行,变量 connectingClientId 在闭包回调 (localOfferCreated) 中正确设置。当 createOffer 调用回调时,connectedClientId 未定义。怎么会这样呢?为了这个我整晚都在用头撞墙。
function publishStream(handShakeInitiator, connectingClientId) {
var localOfferCreated = offerClosure(connectingClientId);
var localIceCandidate = iceClosure(connectingClientId);
peerConnections[connectingClientId] = new RTCPeerConnection(peerConnectionConfig);
peerConnections[connectingClientId].onicecandidate = localIceCandidate;
peerConnections[connectingClientId].addStream(localStream);
if (handShakeInitiator) {
peerConnections[connectingClientId].createOffer(localOfferCreated, createOfferError, offerOptions);
}
}
function offerClosure(id) {
var connectingClientId = id;
function offerCreated(description) {
peerConnections[connectingClientId].setLocalDescription(description, function (connectingClientId) {
webSocket.send(JSON.stringify({
'control': signalConstants.sendToClient,
'cid': connectingClientId,
'sdp': description
}));
}, function () {
console.log('Error setting description.');
});
};
return offerCreated;
}
从调试器中注意这些:
connectingClientId 已设置 -
调用时未设置 connectingClientId -
我在这里错过了什么?
来自RTCPeerConnection.setLocalDescription
successCallback
Is a Function without parameter which will be called when the description has been successfully set. At this point, one can send the offer to a remote server that can forward it to a remote client
您正在通过将其作为内部函数参数来重新定义 connectingClientID
。请记住,命名函数参数是一个隐式变量声明,正如文档所说,它将是未定义的,因为成功回调不提供任何参数。 JavaScript 函数可以访问它们的外部范围,所以你的匿名函数不需要传递这个 arg,它可以简单地引用它创建一个闭包。
function offerCreated(description) {
peerConnections[connectingClientId].setLocalDescription(description, function() {
webSocket.send(JSON.stringify({
control: signalConstants.sendToClient,
cid: connectingClientId,
sdp: description
}));
}, function () {
console.log('Error setting description.');
});
};