-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (149 loc) · 4.45 KB
/
main.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
"use strict";
import {Host, connect} from './webrtc.js';
const createButton = document.getElementById('create-button');
const joinButton = document.getElementById('join-button');
const nameForm = document.getElementById('name-form');
const roomForm = document.getElementById('room-form');
const info = document.getElementById('info');
const shareButton = document.getElementById('share-button');
const qrButton = document.getElementById('qr-button');
const cancelButton = document.getElementById('cancel-button');
const messages = document.getElementById('messages');
const messageForm = document.getElementById('message-form');
const dialog = document.querySelector('dialog');
const dialogMsg = dialog.querySelector('p');
const hostChannels = new Map();
let myChannel;
let code;
let myName;
let modalPromise = Promise.resolve();
async function showModal(message) {
return modalPromise = modalPromise.then(() => new Promise(resolve => {
dialogMsg.textContent = message;
dialog.addEventListener('close', resolve, {once: true});
dialog.showModal();
}));
}
function setName() {
return new Promise(resolve => {
nameForm.onsubmit = event => {
event.preventDefault();
nameForm.hidden = true;
myName = nameForm.elements['name'].value;
resolve();
};
nameForm.hidden = false;
nameForm.elements['name'].focus();
});
}
function getCode() {
return new Promise(resolve => {
roomForm.onsubmit = event => {
event.preventDefault();
roomForm.hidden = true;
resolve(roomForm.elements['code'].value);
};
roomForm.hidden = false;
roomForm.elements['code'].focus();
});
}
function addMessage(msg) {
for (let channel of hostChannels.values())
channel.send(msg);
let li = document.createElement("li");
li.textContent = msg;
messages.appendChild(li);
messageForm.scrollIntoView(false);
}
function sendMessage(msg) {
if (myChannel)
myChannel.send(msg);
else
addMessage(`${myName}: ${msg}`);
}
function enableMessages() {
messageForm.onsubmit = event => {
event.preventDefault();
sendMessage(messageForm.elements['message'].value);
messageForm.elements['message'].value = '';
}
messageForm.hidden = false;
messageForm.elements['message'].focus();
}
function connectURL() {
return new URL('#' + code, location.href);
}
shareButton.onclick = () => {
let url = connectURL();
if (navigator.share)
navigator.share({url});
else
navigator.clipboard.writeText(url).then(() => showModal("Link copied to clipboard"));
};
qrButton.onclick = () => {
let url = "https://luiscastro193.github.io/qr-generator/#" + encodeURIComponent(connectURL());
window.open(url);
}
cancelButton.onclick = () => location.reload();
async function setAsHost() {
createButton.disabled = true;
joinButton.disabled = true;
await setName();
code = Math.trunc(Math.random() * 10000).toString().padStart(4, '0');
info.textContent = `Hosting room ${code}`;
shareButton.hidden = false;
qrButton.hidden = false;
enableMessages();
const host = new Host(code);
while (true) {
let [user, channel] = await host.nextChannel();
if (user == myName)
user += " 2";
let oldChannel = hostChannels.get(user);
if (oldChannel) {
oldChannel.close();
await new Promise(resolve => setTimeout(resolve));
}
addMessage(`${user} has connected`);
hostChannels.set(user, channel);
channel.addEventListener('message', event => addMessage(`${user}: ${event.data}`));
channel.addEventListener('close', () => {
hostChannels.delete(user);
addMessage(`${user} has disconnected`);
});
}
}
async function connectToRoom() {
createButton.disabled = true;
joinButton.disabled = true;
if (!code) code = await getCode();
await setName();
info.textContent = `Connecting to room ${code}...`;
cancelButton.hidden = false;
try {
myChannel = await connect(code, myName);
}
catch (e) {
info.textContent = `Connection to room ${code} failed`;
throw e;
}
myChannel.addEventListener('message', event => addMessage(event.data));
info.textContent = `Connected to room ${code}`;
cancelButton.hidden = true;
myChannel.addEventListener('close', () => {
let msg = `Host has disconnected. Chat ended.`;
addMessage(msg);
info.textContent = msg;
});
enableMessages();
}
createButton.onclick = setAsHost;
joinButton.onclick = connectToRoom;
createButton.disabled = false;
joinButton.disabled = false;
if (location.hash) {
code = location.hash.slice(1);
history.replaceState(null, '', ' ');
connectToRoom();
}
window.onhashchange = () => location.reload();