【发布时间】:2016-02-10 09:23:31
【问题描述】:
我遇到了这个问题,我正在创建一个闭包并单步执行调试器,变量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;
}
从调试器中注意这些:
我在这里错过了什么?
【问题讨论】:
标签: javascript callback closures webrtc