-
Notifications
You must be signed in to change notification settings - Fork 17
/
web.js
169 lines (142 loc) Β· 3.94 KB
/
web.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
/// <reference path="../messages.d.ts" />
/**
* Hides the given element by setting `display: none`.
* @param {HTMLElement} element The element to hide
*/
function hideElement(element) {
element.style.display = "none";
}
/**
* Shows the given element by resetting the display CSS property.
* @param {HTMLElement} element The element to show
*/
function showElement(element) {
element.style.display = "";
}
const callButton = document.getElementById("call-button");
const videoContainer = document.getElementById("video-container");
/**
* Hides both local and remote video, but shows the "call" button.
*/
function hideVideoCall() {
hideElement(videoContainer);
showElement(callButton);
}
/**
* Shows both local and remote video, and hides the "call" button.
*/
function showVideoCall() {
hideElement(callButton);
showElement(videoContainer);
}
/** @type {string} */
let otherPerson;
const username = prompt("What's your name?", `user${Math.floor(Math.random() * 100)}`);
const socketUrl = `ws://${location.host}/ws`;
const socket = new WebSocket(socketUrl);
/**
* Sends the message over the socket.
* @param {WebSocketMessage} message The message to send
*/
function sendMessageToSignallingServer(message) {
const json = JSON.stringify(message);
socket.send(json);
}
// log in directly after the socket was opened
socket.addEventListener("open", () => {
console.log("websocket connected");
sendMessageToSignallingServer({
channel: "login",
name: username,
});
});
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data.toString());
handleMessage(message);
});
/**
* Processes the incoming message.
* @param {WebSocketMessage} message The incoming message
*/
async function handleMessage(message) {
switch (message.channel) {
case "start_call":
console.log(`receiving call from ${message.with}`);
otherPerson = message.otherPerson;
showVideoCall();
const offer = await webrtc.createOffer();
await webrtc.setLocalDescription(offer);
sendMessageToSignallingServer({
channel: "webrtc_offer",
offer,
otherPerson,
});
break;
case "webrtc_ice_candidate":
console.log("received ice candidate");
await webrtc.addIceCandidate(message.candidate);
break;
case "webrtc_offer":
console.log("received webrtc offer");
await webrtc.setRemoteDescription(message.offer);
const answer = await webrtc.createAnswer();
await webrtc.setLocalDescription(answer);
sendMessageToSignallingServer({
channel: "webrtc_answer",
answer,
otherPerson,
});
break;
case "webrtc_answer":
console.log("received webrtc answer");
await webrtc.setRemoteDescription(message.answer);
break;
default:
console.log("unknown message", message);
break;
}
}
const webrtc = new RTCPeerConnection({
iceServers: [
{
urls: [
"stun:stun.stunprotocol.org",
],
},
],
});
webrtc.addEventListener("icecandidate", (event) => {
if (!event.candidate) {
return;
}
sendMessageToSignallingServer({
channel: "webrtc_ice_candidate",
candidate: event.candidate,
otherPerson,
});
});
webrtc.addEventListener("track", (event) => {
/** @type {HTMLVideoElement} */
const remoteVideo = document.getElementById("remote-video");
remoteVideo.srcObject = event.streams[0];
});
navigator
.mediaDevices
.getUserMedia({ video: true })
.then((localStream) => {
/** @type {HTMLVideoElement} */
const localVideo = document.getElementById("local-video");
localVideo.srcObject = localStream;
for (const track of localStream.getTracks()) {
webrtc.addTrack(track, localStream);
}
});
callButton.addEventListener("click", async () => {
otherPerson = prompt("Who you gonna call?");
showVideoCall();
sendMessageToSignallingServer({
channel: "start_call",
otherPerson,
});
});
hideVideoCall();