sendrecv/js: Handle OFFER_REQUEST as part of the switch

This is clearer, and also stricter w.r.t. what sort of messages we
accept.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-examples/-/merge_requests/31>
This commit is contained in:
Nirbheek Chauhan 2021-02-09 14:27:31 +05:30
parent a508bc243d
commit ea3c0e8766

View file

@ -130,40 +130,38 @@ function onServerMessage(event) {
if (!peer_connection) if (!peer_connection)
createCall(null).then (generateOffer); createCall(null).then (generateOffer);
return; return;
case "OFFER_REQUEST":
// The peer wants us to set up and then send an offer
if (!peer_connection)
createCall(null).then (generateOffer);
return;
default: default:
if (event.data.startsWith("ERROR")) { if (event.data.startsWith("ERROR")) {
handleIncomingError(event.data); handleIncomingError(event.data);
return; return;
} }
if (event.data.startsWith("OFFER_REQUEST")) { // Handle incoming JSON SDP and ICE messages
// The peer wants us to set up and then send an offer try {
if (!peer_connection) msg = JSON.parse(event.data);
createCall(null).then (generateOffer); } catch (e) {
} if (e instanceof SyntaxError) {
else { handleIncomingError("Error parsing incoming JSON: " + event.data);
// Handle incoming JSON SDP and ICE messages
try {
msg = JSON.parse(event.data);
} catch (e) {
if (e instanceof SyntaxError) {
handleIncomingError("Error parsing incoming JSON: " + event.data);
} else {
handleIncomingError("Unknown error parsing response: " + event.data);
}
return;
}
// Incoming JSON signals the beginning of a call
if (!peer_connection)
createCall(msg);
if (msg.sdp != null) {
onIncomingSDP(msg.sdp);
} else if (msg.ice != null) {
onIncomingICE(msg.ice);
} else { } else {
handleIncomingError("Unknown incoming JSON: " + msg); handleIncomingError("Unknown error parsing response: " + event.data);
} }
return;
}
// Incoming JSON signals the beginning of a call
if (!peer_connection)
createCall(msg);
if (msg.sdp != null) {
onIncomingSDP(msg.sdp);
} else if (msg.ice != null) {
onIncomingICE(msg.ice);
} else {
handleIncomingError("Unknown incoming JSON: " + msg);
} }
} }
} }