-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (131 loc) · 3.44 KB
/
index.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
let room = window.location.pathname;
let local_uid = new Date().getTime(); // TODO use mac address or something + chrome tab
//config
let delay = 0;
let bell = false;
let peers = {};
console.log('connecting to wrmhole at:');
console.log(`wss://wrm.blottn.ie${room}`);
// create WS
let ws = new WebSocket("wss://wrm.blottn.ie" + room);
ws.addEventListener('open', function (event) {
send({
kind: 'join',
data: {
uid: local_uid,
}
});
});
const send = (obj) => {
if (ws.readyState != 1) {
ws = new WebSocket("wss://wrm.blottn.ie" + room);
}
while (ws.readyState != 1) {}
ws.send(JSON.stringify(obj));
}
ws.addEventListener('message', async function (event) {
let text = await event.data.text();
let {kind, data} = JSON.parse(text);
let {uid} = data;
if (kind === 'join' && !(uid in peers)) {
send({
kind: 'join',
data: {
uid: local_uid,
}
});
createBox(uid);
}
if (kind === 'msg') {
let {delay, content} = data;
if (!(uid in peers)) {
createBox(uid);
}
setTimeout(() => {
document.getElementById(`uid-${uid}`).value = content;
}, delay);
}
peers[uid] = {
last: new Date().getTime(),
missed: 0,
}
});
// Heartbeats
const heartbeatWindow = 3000;
const heartbeatAllowedMisses = 2;
const heartbeatInterval = 1000;
let heartbeat = () => {
send({
kind: 'heartbeat',
data: {
uid: local_uid,
}
});
let now = new Date().getTime();
Object.keys(peers).map(peer => {
if (now - peers[peer].last > heartbeatWindow) {
console.log(`${peer} missed heartbeat`);
peers[peer].missed += 1;
}
if (peers[peer].missed > heartbeatAllowedMisses) {
delete peers[peer];
// yuck lol
// TODO change to id-ing the root box object;
let box = document.getElementById(`uid-${peer}`).parentElement.parentElement;
box.parentElement.removeChild(box);
}
});
setTimeout(heartbeat, heartbeatInterval);
}
setTimeout(heartbeat, heartbeatInterval);
// Setup event listeners
window.addEventListener('load', () => {
document.getElementById('true-title').textContent = genName(local_uid);
let input = document.getElementById('inputspace');
input.addEventListener('input', (e) => {
send({
kind: 'msg',
data: {
delay,
uid: local_uid,
content: e.target.value,
}
});
});
document.getElementById('delay')
.addEventListener('input', (e) => {
delay = parseInt(e.target.value);
});
document.getElementById('bell')
.addEventListener('change', (e) => {
bell = !bell;
});
});
function createBox(uid) {
let template = document.getElementById('template');
let box = template.cloneNode(true);
box.className = "box client";
box.querySelector('#title').textContent = genName(uid);
let input = box.querySelector('#inputspace')
input.id = `uid-${uid}`;
input.value = '';
box.querySelector(`#uid-${uid}`).setAttribute('readonly','');
box.querySelector(`#uid-${uid}`).className = "uneditable";
let container = document.getElementById('boxes');
container.appendChild(box);
}
function genName(id) {
return wordnet_english_adjective_words[id % wordnet_english_adjective_words.length];
}
function burger() {
let menu = document.querySelector(".menu");
if (menu.className.includes('menu-gone')) {
menu.className = 'menu';
} else {
menu.className = 'menu menu-gone';
}
}
function ringBell() {
const audio = new Audio();
audio.play();
}