Skip to content

Commit

Permalink
Ready for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
LodeKennes committed May 12, 2020
1 parent cd2a8c0 commit 5d1eb5b
Showing 1 changed file with 3 additions and 75 deletions.
78 changes: 3 additions & 75 deletions client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,13 @@
.build();

hubConnection.on("ClientJoined", async function(id, count, clients) {
for (const socketId of clients) {
if (rtcPeerConnections[socketId] !== undefined || socketId === hubConnection.connectionId) continue;

const connection = new RTCPeerConnection(peerConnectionConfig);
rtcPeerConnections[socketId] = {
connection
};

connection.onicecandidate = async function (event) {
if (event.candidate === null) return;
await hubConnection.send("Signal", socketId, JSON.stringify({ 'ice': event.candidate }));
};

connection.onaddstream = async function (event) {
for (const key in rtcPeerConnections) {
if (key === socketId) {
rtcPeerConnections[key].stream = event.stream;
break;
}
}

const container = document.getElementById("other-videos");
const newVideo = document.createElement("video");
newVideo.setAttribute('muted', false);
newVideo.setAttribute('autoplay', true);
newVideo.setAttribute('id', 'video' + socketId);
newVideo.srcObject = event.stream;
container.appendChild(newVideo);
};

connection.addStream(stream);
}

if (count >= 2) {
const connection = rtcPeerConnections[id]?.connection;

if (connection !== undefined) {
const offer = await connection.createOffer();
await connection.setLocalDescription(offer);
await hubConnection.send("Signal", id, JSON.stringify({'sdp': connection.localDescription}));
}
}
// 2. Client joined
});

hubConnection.on("Signal", async function(from, message) {
const signal = JSON.parse(message);

if (from === hubConnection.connectionId) return;

const connection = rtcPeerConnections[from].connection;

if (signal.sdp) {
await connection.setRemoteDescription(new RTCSessionDescription(signal.sdp));

if (signal.sdp.type === 'offer') {
const answer = await connection.createAnswer();
await connection.setLocalDescription(answer);
await hubConnection.send("Signal", from, JSON.stringify({'sdp': connection.localDescription}));
}
}

if (signal.ice) {
await connection.addIceCandidate(new RTCIceCandidate(signal.ice));
}
// 3. Signal received
});

hubConnection.on("ClientLeft", async function(id) {
Expand All @@ -97,22 +40,7 @@
const setup = document.getElementById("setup");
setup.parentNode.removeChild(setup);

await new Promise((resolve, reject) => {
navigator.getUserMedia({
video: true,
audio: true
}, (mediaStream) => {
// Success
stream = mediaStream;
document.getElementById("own-video").srcObject = stream;
resolve();
},
(e) => {
// Error
console.log(e);
reject(e);
})
});
// 1. Get user media

await hubConnection.start();

Expand Down

0 comments on commit 5d1eb5b

Please sign in to comment.