-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
190 lines (167 loc) · 4.6 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
/**
* --------------------------
* Global UI elements
* --------------------------
*/
const vidLocal = document.getElementById('vidLocal');
const vidRemote = document.getElementById('vidRemote');
const displayChat = document.getElementById('displayChat');
const enterChat = document.getElementById('enterChat');
const btnStart = document.getElementById('btnStart');
const logErrors = err => {console.error(err)};
const ui = {
appendChatMessage: (name, text) => {
let time = new Date().toString('H:i:s');
displayChat.innerHTML = `<p><strong>${name}</strong> <small>${time}</small><br>${text}</p>` + displayChat.innerHTML;
},
enableChat: () => {
enterChat.disabled = false;
enterChat.onkeyup = e => {
// if enter is pressed
if (e.keyCode === 13) {
dataChannel.send(enterChat.value);
ui.appendChatMessage('Du', enterChat.value);
enterChat.value = '';
}
}
}
};
/**
* --------------------------
* Local Video
* --------------------------
*/
let localStream;
let remoteStream;
/**
* --------------------------
* Signaling
* --------------------------
*/
const socket = io.connect('http://localhost:8888');
let connection;
let dataChannel;
// Send a message to the signaling server
const sendMessage = message => {
message = Object.assign({to: 'default'}, message);
console.log(`>>> SENT: ${message.type}`, message);
socket.emit('message', message);
};
// Receive a message from the signaling server
const receiveMessage = (message) => {
// Ignore messages that were sent by this client
if (message.from === socket.id) {
return;
}
console.log(`<<< RECEIVED: ${message.type} from ${message.from}:`, message);
receiveSignalingMessage(message);
};
socket.on('message', receiveMessage);
const receiveSignalingMessage = (message) => {
switch (message.type) {
case 'init':
ui.appendChatMessage('SYSTEM', `Peer joined.`);
createConnection();
startConnection(false);
break;
case 'offer':
connection
.setRemoteDescription(new RTCSessionDescription(message.descr))
.then(() => connection.createAnswer())
.then(answer => connection.setLocalDescription(new RTCSessionDescription(answer)))
.then(() => sendMessage({type: 'answer', descr: connection.localDescription}))
.catch(logErrors);
break;
case 'answer':
connection.setRemoteDescription(new RTCSessionDescription(message.descr));
break;
case 'candidate':
connection.addIceCandidate(new RTCIceCandidate({
candidate: message.candidate
}));
break;
default:
logErrors(`Unknown message type: "${message.type}"`);
break;
}
};
/**
* --------------------------
* WebRTC
* --------------------------
*/
const createConnection = () => {
connection = new RTCPeerConnection();
connection.addStream(localStream);
};
const startConnection = (isCreator) => {
console.log('Starting RTC connection as ' + (isCreator ? 'creator' : 'client'));
connection.onicecandidate = e => {
if (e.candidate) {
sendMessage({
type: 'candidate',
candidate: e.candidate.candidate
});
}
};
connection.onaddstream = e => {
vidRemote.srcObject = e.stream;
remoteStream = e.stream;
};
if (isCreator) {
dataChannel = connection.createDataChannel('chat');
onDataChannelCreated(dataChannel);
connection
.createOffer()
.then(offer => connection.setLocalDescription(new RTCSessionDescription(offer)))
.then(() => sendMessage({type: 'offer', descr: connection.localDescription}))
.catch(logErrors);
} else {
connection.ondatachannel = event => {
dataChannel = event.channel;
onDataChannelCreated(dataChannel);
};
}
};
function onDataChannelCreated(channel) {
channel.onopen = () => {
ui.enableChat();
};
channel.onmessage = message => {
ui.appendChatMessage('Peer', message.data);
}
}
/**
* --------------------------
* Button Handlers
* --------------------------
*/
btnStart.onclick = e => {
navigator.mediaDevices
.getUserMedia({
audio: true,
video: true
})
.then(stream => {
vidLocal.srcObject = stream;
localStream = stream;
// Join a default room
socket.emit('join', 'default', (message, {clients}) => {
// If clients.length is 0, we just created the room and need to wait for a peer
let clientNames = Object.keys(clients);
if (clientNames.length === 0) {
console.log('First in room, waiting for peers.');
ui.appendChatMessage('SYSTEM', 'Created room "default". Waiting for users.');
} else {
sendMessage({
type: 'init'
});
createConnection();
startConnection(true);
ui.appendChatMessage('SYSTEM', 'Joined room "default".');
}
});
})
.catch(logErrors);
btnStart.disabled = true;
};