-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathindex.js
76 lines (69 loc) · 2.18 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
;(() => {
// expectingMessage is set to true
// if the user has just submitted a message
// and so we should scroll the next message into view when received.
let expectingMessage = false
function dial() {
const conn = new WebSocket(`ws://${location.host}/subscribe`)
conn.addEventListener('close', ev => {
appendLog(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`, true)
if (ev.code !== 1001) {
appendLog('Reconnecting in 1s', true)
setTimeout(dial, 1000)
}
})
conn.addEventListener('open', ev => {
console.info('websocket connected')
})
// This is where we handle messages received.
conn.addEventListener('message', ev => {
if (typeof ev.data !== 'string') {
console.error('unexpected message type', typeof ev.data)
return
}
const p = appendLog(ev.data)
if (expectingMessage) {
p.scrollIntoView()
expectingMessage = false
}
})
}
dial()
const messageLog = document.getElementById('message-log')
const publishForm = document.getElementById('publish-form')
const messageInput = document.getElementById('message-input')
// appendLog appends the passed text to messageLog.
function appendLog(text, error) {
const p = document.createElement('p')
// Adding a timestamp to each message makes the log easier to read.
p.innerText = `${new Date().toLocaleTimeString()}: ${text}`
if (error) {
p.style.color = 'red'
p.style.fontStyle = 'bold'
}
messageLog.append(p)
return p
}
appendLog('Submit a message to get started!')
// onsubmit publishes the message from the user when the form is submitted.
publishForm.onsubmit = async ev => {
ev.preventDefault()
const msg = messageInput.value
if (msg === '') {
return
}
messageInput.value = ''
expectingMessage = true
try {
const resp = await fetch('/publish', {
method: 'POST',
body: msg,
})
if (resp.status !== 202) {
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
} catch (err) {
appendLog(`Publish failed: ${err.message}`, true)
}
}
})()