Skip to content

Instantly share code, notes, and snippets.

@rsatrio
Created June 13, 2022 03:19
Show Gist options
  • Save rsatrio/a9a9438415bd59ce1a17fe9664d1872c to your computer and use it in GitHub Desktop.
Save rsatrio/a9a9438415bd59ce1a17fe9664d1872c to your computer and use it in GitHub Desktop.
ws1.onmessage = event => {
var data1 = JSON.parse(event.data);
var data2 = null;
if (data1.userId == userId2 || data1.userId.length < 2) {
return;
}
else if (data1.type == 'NewMember') {
handleNewMemberAndOffer(data1);
}
else if (data1.type == "UserId") {
setUserId(data1.data);
userId2 = data1.data;
document.getElementById('status-offline').style.display = 'none';
document.getElementById('status-online').style.display = 'block';
document.getElementById('toSend').removeAttribute('disabled');
document.getElementById('btnSend').removeAttribute('disabled');
document.getElementById('btnLogin').style.display = 'none';
document.getElementById('myUsername').setAttribute('readOnly', 'true');
let myStatus = document.getElementById('myStatus');
myStatus.className = 'status green';
document.getElementById('h3-myStatus').innerText = 'online';
document.getElementById('h3-myStatus').appendChild(myStatus);
toast.success("Connected to Signalling Server. Please click the Show Login/Chat button");
newMember(userId2);
}
else if (data1.type == "Offer") {
data2 = JSON.parse(data1.data);
handleNewMemberAndOffer(data1);
}
else if (data1.type == "Ice") {
data2 = JSON.parse(data1.data);
if (data2) {
let peer1 = rtcPeers.get(data1.userId);
if (peer1) {
console.log("Tambah Ice Candidate");
peer1.addIceCandidate(new RTCIceCandidate(data2)).catch(error => {
console.log("Error ICE:" + error);
});
}
}
}
else if (data1.type == "Answer") {
data2 = JSON.parse(data1.data);
let peer1 = rtcPeers.get(data1.userId);
peer1.setRemoteDescription(new RTCSessionDescription(data2));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment