-
Notifications
You must be signed in to change notification settings - Fork 39
/
script.js
188 lines (165 loc) · 5.76 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
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
const possibleEmojis = [
'🐀','🐁','🐭','🐹','🐂','🐃','🐄','🐮','🐅','🐆','🐯','🐇','🐐','🐑','🐏','🐴',
'🐎','🐱','🐈','🐰','🐓','🐔','🐤','🐣','🐥','🐦','🐧','🐘','🐩','🐕','🐷','🐖',
'🐗','🐫','🐪','🐶','🐺','🐻','🐨','🐼','🐵','🙈','🙉','🙊','🐒','🐉','🐲','🐊',
'🐍','🐢','🐸','🐋','🐳','🐬','🐙','🐟','🐠','🐡','🐚','🐌','🐛','🐜','🐝','🐞',
];
function randomEmoji() {
var randomIndex = Math.floor(Math.random() * possibleEmojis.length);
return possibleEmojis[randomIndex];
}
const emoji = randomEmoji();
const name = prompt("What's your name?");
// Generate random chat hash if needed
if (!location.hash) {
location.hash = Math.floor(Math.random() * 0xFFFFFF).toString(16);
}
const chatHash = location.hash.substring(1);
// TODO: Replace with your own channel ID
const drone = new ScaleDrone('yiS12Ts5RdNhebyM');
// Scaledrone room name needs to be prefixed with 'observable-'
const roomName = 'observable-' + chatHash;
// Scaledrone room used for signaling
let room;
const configuration = {
iceServers: [{
url: 'stun:stun.l.google.com:19302'
}]
};
// RTCPeerConnection
let pc;
// RTCDataChannel
let dataChannel;
// Wait for Scaledrone signalling server to connect
drone.on('open', error => {
if (error) {
return console.error(error);
}
room = drone.subscribe(roomName);
room.on('open', error => {
if (error) {
return console.error(error);
}
console.log('Connected to signaling server');
});
// 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 => {
if (members.length >= 3) {
return alert('The room is full');
}
// 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 sendSignalingMessage(message) {
drone.publish({
room: roomName,
message
});
}
function startWebRTC(isOfferer) {
console.log('Starting WebRTC in as', isOfferer ? 'offerer' : 'waiter');
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) {
sendSignalingMessage({'candidate': event.candidate});
}
};
if (isOfferer) {
// If user is offerer let them create a negotiation offer and set up the data channel
pc.onnegotiationneeded = () => {
pc.createOffer(localDescCreated, error => console.error(error));
}
dataChannel = pc.createDataChannel('chat');
setupDataChannel();
} else {
// If user is not the offerer let wait for a data channel
pc.ondatachannel = event => {
dataChannel = event.channel;
setupDataChannel();
}
}
startListentingToSignals();
}
function startListentingToSignals() {
// 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) {
// This is called after receiving an offer or answer from another peer
pc.setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
console.log('pc.remoteDescription.type', pc.remoteDescription.type);
// When receiving an offer lets answer it
if (pc.remoteDescription.type === 'offer') {
console.log('Answering offer');
pc.createAnswer(localDescCreated, error => console.error(error));
}
}, error => console.error(error));
} else if (message.candidate) {
// Add the new ICE candidate to our connections remote description
pc.addIceCandidate(new RTCIceCandidate(message.candidate));
}
});
}
function localDescCreated(desc) {
pc.setLocalDescription(
desc,
() => sendSignalingMessage({'sdp': pc.localDescription}),
error => console.error(error)
);
}
// Hook up data channel event handlers
function setupDataChannel() {
checkDataChannelState();
dataChannel.onopen = checkDataChannelState;
dataChannel.onclose = checkDataChannelState;
dataChannel.onmessage = event =>
insertMessageToDOM(JSON.parse(event.data), false)
}
function checkDataChannelState() {
console.log('WebRTC channel state is:', dataChannel.readyState);
if (dataChannel.readyState === 'open') {
insertMessageToDOM({content: 'WebRTC data channel is now open'});
}
}
function insertMessageToDOM(options, isFromMe) {
const template = document.querySelector('template[data-template="message"]');
const nameEl = template.content.querySelector('.message__name');
if (options.emoji || options.name) {
nameEl.innerText = options.emoji + ' ' + options.name;
}
template.content.querySelector('.message__bubble').innerText = options.content;
const clone = document.importNode(template.content, true);
const messageEl = clone.querySelector('.message');
if (isFromMe) {
messageEl.classList.add('message--mine');
} else {
messageEl.classList.add('message--theirs');
}
const messagesEl = document.querySelector('.messages');
messagesEl.appendChild(clone);
// Scroll to bottom
messagesEl.scrollTop = messagesEl.scrollHeight - messagesEl.clientHeight;
}
const form = document.querySelector('form');
form.addEventListener('submit', () => {
const input = document.querySelector('input[type="text"]');
const value = input.value;
input.value = '';
const data = {
name,
content: value,
emoji,
};
dataChannel.send(JSON.stringify(data));
insertMessageToDOM(data, true);
});
insertMessageToDOM({content: 'Chat URL is ' + location.href});