I'm currently facing an issue where I am encountering a problem while creating a closure and debugging through it. The variable connectingClientId is correctly set within the closure callback (localOfferCreated). However, when the callback is invoked by createOffer, the connectedClientId turns out to be undefined. It's been a frustrating night trying to figure out what could be causing this situation.
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;
}
Take a look at these insights from the debugger:
connectingClientId is set - https://i.sstatic.net/MV1MA.png
connectingClientId becomes unset upon call - https://i.sstatic.net/1Q5Ej.png
What could I possibly be overlooking here?