-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
67 lines (60 loc) · 1.78 KB
/
index.html
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
<h1>text capture page</h1>
<textarea id="capture" rows="2" cols="20" style="background: black">
</textarea>
<script>
let lastEvent = 0
let inputEvents = 0
const t = document.getElementById('capture')
let socket
let iv
function write (data) {
socket.send(JSON.stringify(data))
}
function connect () {
document.body.style['background-color'] = 'black'
setTimeout(() => {
if (iv) clearTimeout(iv)
socket = new WebSocket('ws://localhost:9002')
socket.onopen = (event) => {
document.body.style['background-color'] = 'blue'
// XXX: for coloured state mgmt
socket.send(JSON.stringify({ type: 'mode', mode: 'web' }))
}
socket.onclose = (event) => {
document.body.style['background-color'] = 'red'
if (iv) clearTimeout(iv)
iv = setTimeout(connect, 5000)
}
socket.onerror = (event) => {
document.body.style['background-color'] = 'red'
if (iv) clearTimeout(iv)
iv = setTimeout(connect, 5000)
}
socket.onmessage = (event) => {
try {
const msg = JSON.parse(event.data.toString())
if (msg.type === 'state-ctl') {
if (msg.state === 'OFF') document.body.style['background-color'] = 'blue'
else document.body.style['background-color'] = 'green'
}
} catch (e) {}
}
}, 1000)
}
t.addEventListener('input', (ev) => {
if (typeof ev.data !== 'string') return
inputEvents++
lastEvent = Date.now()
write({ type: 'oninput' })
})
setInterval(() => {
if (Date.now() - lastEvent > 1200 && inputEvents > 0) {
const pending = t.value
write({ type: 'result', text: pending })
inputEvents = 0
t.value = ''
}
}, 500)
t.value = ''
connect()
</script>