-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
262 lines (235 loc) · 8.61 KB
/
app.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*jshint esversion: 8 */
(async () => {
/**
* Firebase setup, replace all ###### in the firebaseConfig to your own project
* settings value
*/
var firebaseConfig = {
apiKey: "######",
authDomain: "######",
databaseURL: "######",
projectId: "######",
storageBucket: "######",
messagingSenderId: "######",
appId: "######",
};
if (isDevelopment()) {
firebaseConfig = {
apiKey: "######",
authDomain: "######",
databaseURL: "http://localhost:9000/?ns=######",
projectId: "######",
storageBucket: "######",
messagingSenderId: "######",
appId: "######",
};
}
firebase.initializeApp(firebaseConfig);
const firestore = firebase.firestore();
if (isDevelopment()) {
firestore.settings({ host: "localhost:8080", ssl: false });
}
function isDevelopment() {
if (
location.hostname === "localhost" ||
location.hostname === "127.0.0.1" ||
!location.hostname
) {
return true;
} else return false;
}
//END FIREBASE SETTUP
const rtcconfig = { iceServers: [{ urls: "stun:stun.1.google.com:19302" }] };
let pc = null;
let dc = null;
let roomID = document.querySelector("#roomID");
let offerID = document.querySelector("#offerID");
const RTCcomm = document.getElementById("RTCcomm");
const chat = document.getElementById("chat");
const createRoomBtn = document.querySelector("#createRoom");
const joinRoomBtn = document.querySelector("#joinRoom");
const log = (msg) => (RTCcomm.innerHTML += `<br>${msg}`);
/**
*Initialises the button events that handle, joining, creating and leaving
a peer connection room, this function also initialises the peer connection
with the provided rtc configurations. Note, you can use this function for any
additioanl events you need to have started before interacting with the app.
*/
function init() {
createRoomBtn.addEventListener("click", createRoom);
joinRoomBtn.addEventListener("click", joinRoom);
pc = new RTCPeerConnection(rtcconfig);
initDataChannel();
}
/**
* This function initialises the data channel on the peer connection in order to
* exchange messages
*/
function initDataChannel() {
dc = pc.createDataChannel("chat", { negotiated: true, id: 0 });
pc.oniceconnectionstatechange = (e) => {
log(pc.iceConnectionState);
if (pc.iceConnectionState === "disconnected") attemptReconnection();
};
dc.onopen = () => {
console.log("chat open");
chat.select();
chat.disabled = false;
};
dc.onclose = () => {
console.log("chat closed");
};
dc.onmessage = (e) => log(`> ${e.data}`);
chat.onkeypress = function (e) {
localStorage.setItem("T1", "on");
if (e.keyCode != 13) return;
dc.send(this.value);
log(this.value);
saveMessage(this.value); //Purely optional and can be removed if not needed
this.value = "";
};
}
async function createRoom() {
try {
console.log("Creating room");
createRoomBtn.disabled = true;
joinRoomBtn.disabled = true;
const db = firebase.firestore();
const rooms = db.collection("rooms");
const room = await rooms.add({}); //adds a new(empty) room to the rooms collection, this will be used to get the room ID for later use
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
pc.onicecandidate = async ({ candidate }) => {
if (candidate) return;
const offerCollection = await rooms.doc(room.id).collection("offer");
const offerDocument = await offerCollection.add({
offer: { type: offer.type, sdp: offer.sdp },
});
const answerCollection = await rooms.doc(room.id).collection("answer");
const answerDocument = await answerCollection.add({});
//Adds a refernce in the offer and answer document that link them to each other
await offerDocument.set({ answer: answerDocument.id }, { merge: true });
await answerDocument.set({ offer: offerDocument.id }, { merge: true });
//sets the textarea values to the room and offer ID
roomID.value = room.id;
offerID.value = offerDocument.id;
/**
* Real-time event listener that checks to see if a change occurs in the answer document.
* if a change accures it sets the peer connections remote discription to the new sdp
* this allows for reconnection
*/
answerDocument.onSnapshot(async (snapshot) => {
const data = snapshot.data().answer;
if (data && data.sdp)
pc.setRemoteDescription({ type: "answer", sdp: data.sdp });
});
console.log("Room created");
};
} catch (error) {
console.log(`There was an error creating the room ${error}`);
createRoomBtn.disabled = false;
joinRoomBtn.disabled = false;
}
}
async function joinRoom() {
console.log("Joining room");
try {
if (pc.signalingState != "stable") {
console.log(`The connection is not stable, try again`);
createRoomBtn.disabled = false;
joinRoomBtn.disabled = false;
return;
}
createRoomBtn.disabled = true;
joinRoomBtn.disabled = true;
const db = firebase.firestore();
const roomDocument = db.collection("rooms").doc(roomID.value);
const roomSnapshot = await roomDocument.get();
const offerDocument = roomDocument.collection("offer").doc(offerID.value);
const offerSnapshot = await offerDocument.get();
const answerDocument = roomDocument
.collection("answer")
.doc(offerSnapshot.data().answer);
const answerSnapshot = await answerDocument.get();
if (
!roomSnapshot.exists ||
!offerSnapshot.exists ||
!answerSnapshot.exists
) {
console.log(`The room or room data isn't available`);
createRoomBtn.disabled = false;
joinRoomBtn.disabled = false;
return;
}
const offer = offerSnapshot.data().offer;
await pc.setRemoteDescription({ type: "offer", sdp: offer.sdp });
const answer = await pc.createAnswer();
pc.setLocalDescription(answer);
pc.onicecandidate = async ({ candidate }) => {
if (candidate) return;
await answerDocument.update({
answer: { type: answer.type, sdp: pc.localDescription.sdp },
});
/**
* NOTE: this event listener is for a case where the host disconnected, forcing the user who
* joined to become a new host.
*
* Real-time event listener that checks to see if a change occurs in the answer document.
* if a change accures it sets the peer connections remote discription to the new sdp
* this allows for reconnection
*/
answerDocument.onSnapshot(async (snapshot) => {
const data = snapshot.data().answer;
if (!pc.currentRemoteDescription && data && data.sdp) {
pc.setRemoteDescription({ type: "answer", sdp: data.sdp });
}
});
};
} catch (error) {
console.log(`There was an error creating the room ${error}`);
createRoomBtn.disabled = false;
joinRoomBtn.disabled = false;
}
}
/**
* This simply save the chat messages to the current room each time a message is sent from the user
*/
async function saveMessage(msg) {
const db = firebase.firestore();
const roomDocument = db.collection("rooms").doc(roomID.value);
await roomDocument.update({
messages: firebase.firestore.FieldValue.arrayUnion(msg),
});
}
/**
* This function is triggered whenever the remote peer is disconnected.
* If the remote peer was disconnected, it will attempt to re-establish
* a peer connection, if successful the four way handshake is restarted
* and the peer that is still connected becomes the host awaiting a answer.
*
*/
async function attemptReconnection() {
try {
console.log("Attempting to reconnect");
const db = firebase.firestore();
const rooms = db.collection("rooms");
pc = new RTCPeerConnection(rtcconfig);
initDataChannel();
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
pc.onicecandidate = async ({ candidate }) => {
if (candidate) return;
await rooms
.doc(roomID.value)
.collection("offer")
.doc(offerID.value)
.update({ offer: { type: offer.type, sdp: offer.sdp } });
};
} catch (error) {
console.log(`There was an error in an attempt to reconnect: ${error}`);
createRoomBtn.disabled = false;
joinRoomBtn.disabled = false;
}
}
init();
})();