-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
42 lines (35 loc) · 1.04 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fun With SSE</title>
</head>
<body>
<form id="the-form">
<input type="text" placeholder="Type A Message" name="message">
</form>
<section id="chat">
</section>
<script>
window.addEventListener("DOMContentLoaded", function() {
var form = document.querySelector("#the-form"),
messageField = form.querySelector("[name=message]"),
chat = document.querySelector("#chat")
form.addEventListener("submit", function(e) {
e.preventDefault()
var req = new XMLHttpRequest()
req.open("POST", "/messages")
req.send(messageField.value)
form.reset()
})
var eventSource = new EventSource("/events")
eventSource.addEventListener("message", function(e) {
console.log(e.data)
var line = document.createElement("div")
line.innerText = e.data
chat.insertBefore(line)
})
})
</script>
</body>
</html>