This code snippet:
pc.createOffer().then(setDescription).catch(errorHandler);
function setDescription(description) {
console.log('Received description', description);
pc.setLocalDescription(description).then(function() {
console.log("Sending SDP");
socket.emit('signal', {'sdp': description});
}).catch(errorHandler);
}
Next, you need to handle the received SDP:
function gotMessageFromServer(signal) {
console.log('Received message from server', signal);
if(signal.sdp) {
console.log("Setting remote description: ", signal);
pc.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
// Only create answers in response to offers
if(signal.sdp.type === 'offer') {
console.log("Sending answer");
pc.createAnswer().then(setDescription).catch(errorHandler);
}
}).catch(errorHandler);
} else if(signal.ice) {
console.log('Received remote ICE candidate: ', signal);
pc.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
}
}