|
@@ -20,9 +20,9 @@
|
|
|
window.addEventListener('load', () => {
|
|
|
|
|
|
const config = {
|
|
|
- iceServers: [{
|
|
|
- urls: 'stun:stun.l.google.com:19302', // change to your STUN server
|
|
|
- }],
|
|
|
+ iceServers : [ {
|
|
|
+ urls : 'stun:stun.l.google.com:19302', // change to your STUN server
|
|
|
+ } ],
|
|
|
};
|
|
|
|
|
|
const localId = randomId(4);
|
|
@@ -41,14 +41,13 @@ _localId.textContent = localId;
|
|
|
|
|
|
console.log('Connecting to signaling...');
|
|
|
openSignaling(url)
|
|
|
- .then((ws) => {
|
|
|
- console.log('WebSocket connected, signaling ready');
|
|
|
- offerId.disabled = false;
|
|
|
- offerBtn.disabled = false;
|
|
|
- offerBtn.onclick = () => offerPeerConnection(ws, offerId.value);
|
|
|
- })
|
|
|
- .catch((err) => console.error(err));
|
|
|
-
|
|
|
+ .then((ws) => {
|
|
|
+ console.log('WebSocket connected, signaling ready');
|
|
|
+ offerId.disabled = false;
|
|
|
+ offerBtn.disabled = false;
|
|
|
+ offerBtn.onclick = () => offerPeerConnection(ws, offerId.value);
|
|
|
+ })
|
|
|
+ .catch((err) => console.error(err));
|
|
|
|
|
|
function openSignaling(url) {
|
|
|
return new Promise((resolve, reject) => {
|
|
@@ -57,50 +56,51 @@ function openSignaling(url) {
|
|
|
ws.onerror = () => reject(new Error('WebSocket error'));
|
|
|
ws.onclose = () => console.error('WebSocket disconnected');
|
|
|
ws.onmessage = (e) => {
|
|
|
- if(typeof(e.data) != 'string') return;
|
|
|
+ if (typeof (e.data) != 'string')
|
|
|
+ return;
|
|
|
const message = JSON.parse(e.data);
|
|
|
console.log(message);
|
|
|
- const { id, type } = message;
|
|
|
+ const {id, type} = message;
|
|
|
|
|
|
let pc = peerConnectionMap[id];
|
|
|
- if(!pc) {
|
|
|
- if(type != 'offer') return;
|
|
|
+ if (!pc) {
|
|
|
+ if (type != 'offer')
|
|
|
+ return;
|
|
|
|
|
|
- // Create PeerConnection for answer
|
|
|
- console.log(`Answering to ${id}`);
|
|
|
- pc = createPeerConnection(ws, id);
|
|
|
- }
|
|
|
+ // Create PeerConnection for answer
|
|
|
+ console.log(`Answering to ${id}`);
|
|
|
+ pc = createPeerConnection(ws, id);
|
|
|
+ }
|
|
|
|
|
|
- switch(type) {
|
|
|
+ switch (type) {
|
|
|
case 'offer':
|
|
|
case 'answer':
|
|
|
- pc.setRemoteDescription({
|
|
|
- sdp: message.description,
|
|
|
- type: message.type,
|
|
|
- })
|
|
|
- .then(() => {
|
|
|
- if(type == 'offer') {
|
|
|
- // Send answer
|
|
|
+ pc.setRemoteDescription({
|
|
|
+ sdp : message.description,
|
|
|
+ type : message.type,
|
|
|
+ }).then(() => {
|
|
|
+ if (type == 'offer') {
|
|
|
+ // Send answer
|
|
|
sendLocalDescription(ws, id, pc, 'answer');
|
|
|
}
|
|
|
- });
|
|
|
- break;
|
|
|
-
|
|
|
- case 'candidate':
|
|
|
- pc.addIceCandidate({
|
|
|
- candidate: message.candidate,
|
|
|
- sdpMid: message.mid,
|
|
|
- });
|
|
|
- break;
|
|
|
- }
|
|
|
+ });
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'candidate':
|
|
|
+ pc.addIceCandidate({
|
|
|
+ candidate : message.candidate,
|
|
|
+ sdpMid : message.mid,
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function offerPeerConnection(ws, id) {
|
|
|
- // Create PeerConnection
|
|
|
- console.log(`Offering to ${id}`);
|
|
|
- pc = createPeerConnection(ws, id);
|
|
|
+ // Create PeerConnection
|
|
|
+ console.log(`Offering to ${id}`);
|
|
|
+ pc = createPeerConnection(ws, id);
|
|
|
|
|
|
// Create DataChannel
|
|
|
const label = "test";
|
|
@@ -128,7 +128,7 @@ function createPeerConnection(ws, id) {
|
|
|
console.log(`"DataChannel from ${id} received with label "${dc.label}"`);
|
|
|
setupDataChannel(dc, id);
|
|
|
|
|
|
- dc.send(`Hello from ${localId}`);
|
|
|
+ dc.send(`Hello from ${localId}`);
|
|
|
|
|
|
sendMsg.disabled = false;
|
|
|
sendBtn.disabled = false;
|
|
@@ -148,14 +148,13 @@ function setupDataChannel(dc, id) {
|
|
|
sendBtn.disabled = false;
|
|
|
sendBtn.onclick = () => dc.send(sendMsg.value);
|
|
|
};
|
|
|
- dc.onclose = () => {
|
|
|
- console.log(`DataChannel from ${id} closed`);
|
|
|
- };
|
|
|
- dc.onmessage = (e) => {
|
|
|
- if(typeof(e.data) != 'string') return;
|
|
|
- console.log(`Message from ${id} received: ${e.data}`);
|
|
|
+ dc.onclose = () => { console.log(`DataChannel from ${id} closed`); };
|
|
|
+ dc.onmessage = (e) => {
|
|
|
+ if (typeof (e.data) != 'string')
|
|
|
+ return;
|
|
|
+ console.log(`Message from ${id} received: ${e.data}`);
|
|
|
document.body.appendChild(document.createTextNode(e.data));
|
|
|
- };
|
|
|
+ };
|
|
|
|
|
|
dataChannelMap[id] = dc;
|
|
|
return dc;
|
|
@@ -163,24 +162,24 @@ function setupDataChannel(dc, id) {
|
|
|
|
|
|
function sendLocalDescription(ws, id, pc, type) {
|
|
|
(type == 'offer' ? pc.createOffer() : pc.createAnswer())
|
|
|
- .then((desc) => pc.setLocalDescription(desc))
|
|
|
- .then(() => {
|
|
|
- const { sdp, type } = pc.localDescription;
|
|
|
- ws.send(JSON.stringify({
|
|
|
- id,
|
|
|
- type,
|
|
|
- description: sdp,
|
|
|
- }));
|
|
|
- });
|
|
|
+ .then((desc) => pc.setLocalDescription(desc))
|
|
|
+ .then(() => {
|
|
|
+ const {sdp, type} = pc.localDescription;
|
|
|
+ ws.send(JSON.stringify({
|
|
|
+ id,
|
|
|
+ type,
|
|
|
+ description : sdp,
|
|
|
+ }));
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
function sendLocalCandidate(ws, id, cand) {
|
|
|
const {candidate, sdpMid} = cand;
|
|
|
ws.send(JSON.stringify({
|
|
|
id,
|
|
|
- type: 'candidate',
|
|
|
+ type : 'candidate',
|
|
|
candidate,
|
|
|
- mid: sdpMid,
|
|
|
+ mid : sdpMid,
|
|
|
}));
|
|
|
}
|
|
|
|
|
@@ -188,8 +187,7 @@ function sendLocalCandidate(ws, id, cand) {
|
|
|
function randomId(length) {
|
|
|
const characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
|
const pickRandom = () => characters.charAt(Math.floor(Math.random() * characters.length));
|
|
|
- return [...Array(length)].map(pickRandom).join('');
|
|
|
+ return [...Array(length) ].map(pickRandom).join('');
|
|
|
}
|
|
|
|
|
|
});
|
|
|
-
|