-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
137 lines (122 loc) · 4.14 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Generate random room name if needed
if (!location.hash) {
location.hash = Math.floor(Math.random() * 0xffffff).toString(16);
}
const roomHash = location.hash.substring(1);
// TODO: Replace with your own channel ID
const drone = new ScaleDrone("kfjNFRRUlW1FgBep");
// Room name needs to be prefixed with 'observable-'
const roomName = "observable-" + roomHash;
const configuration = {
iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
};
let room;
let pc;
function onSuccess() { }
function onError(error) {
console.error(error);
}
drone.on("open", error => {
if (error) {
return console.error(error);
}
room = drone.subscribe(roomName);
room.on("open", error => {
if (error) {
onError(error);
}
});
// We're connected to the room and received an array of 'members'
// connected to the room (including us). Signaling server is ready.
room.on("members", members => {
console.log("MEMBERS", members);
// If we are the second user to connect to the room we will be creating the offer
const isOfferer = members.length === 2;
startWebRTC(isOfferer);
});
});
// Send signaling data via Scaledrone
function sendMessage(message) {
drone.publish({
room: roomName,
message
});
}
function startWebRTC(isOfferer) {
pc = new RTCPeerConnection(configuration);
// 'onicecandidate' notifies us whenever an ICE agent needs to deliver a
// message to the other peer through the signaling server
pc.onicecandidate = event => {
if (event.candidate) {
sendMessage({ candidate: event.candidate });
}
};
// If user is offerer let the 'negotiationneeded' event create the offer
if (isOfferer) {
pc.onnegotiationneeded = () => {
pc.createOffer()
.then(localDescCreated)
.catch(onError);
};
}
// When a remote stream arrives display it in the #remoteVideo element
pc.ontrack = event => {
const stream = event.streams[0];
if (!remoteVideo.srcObject || remoteVideo.srcObject.id !== stream.id) {
remoteVideo.srcObject = stream;
}
};
navigator.mediaDevices
.getUserMedia({
audio: true,
video: true
})
.then(stream => {
// Display your local video in #localVideo element
localVideo.srcObject = stream;
// Add your stream to be sent to the conneting peer
stream.getTracks().forEach(track => pc.addTrack(track, stream));
}, onError);
// Listen to signaling data from Scaledrone
room.on("data", (message, client) => {
// Message was sent by us
if (client.id === drone.clientId) {
return;
}
if (message.sdp) {
let closeBtn = document.querySelector('#closeConnection');
closeBtn.style.display = 'block';
let progressBlock = document.querySelector('.progress-block');
progressBlock.style.display = 'none';
// This is called after receiving an offer or answer from another peer
pc.setRemoteDescription(
new RTCSessionDescription(message.sdp),
() => {
// When receiving an offer lets answer it
if (pc.remoteDescription.type === "offer") {
pc.createAnswer()
.then(localDescCreated)
.catch(onError);
}
},
onError
);
} else if (message.candidate) {
console.log("how are you");
// Add the new ICE candidate to our connections remote description
pc.addIceCandidate(
new RTCIceCandidate(message.candidate),
onSuccess,
onError
);
}
});
document.querySelector("#closeConnection").addEventListener("click", function () {
pc.close();
});
}
function localDescCreated(desc) {
pc.setLocalDescription(desc, () => sendMessage({ sdp: pc.localDescription }),
onError
);
}