-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
133 lines (111 loc) · 4.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Starlette WebSocket test</title>
<style type="text/css">
#channels > div > span {
font-weight: bold;
font-size: 1.2em;
}
#channels > div > div {
margin-left: 2em;
white-space: pre;
}
#channels > div > * {
margin-right: 1em;
}
#channels {
border-bottom: 1px solid black;
}
</style>
</head>
<body>
<p>Channels:</p>
<div id="channels">
</div>
<input id="new-channel-input"/>
<button id="new-channel-button">Add Channel</button>
<script type="text/javascript">
'use strict';
const newChannelInput = document.getElementById("new-channel-input");
const newChannelButton = document.getElementById("new-channel-button");
const channelsDiv = document.getElementById("channels");
newChannelInput.onkeyup = (e) => {
if (e.key === "Enter") {
e.preventDefault();
newChannelButton.click();
}
};
newChannelButton.onclick = () => createChannel(newChannelInput.value);
const socket = new WebSocket("ws://localhost:8000/socket");
socket.addEventListener("message", messageHandler);
socket.onclose = () => alert("Server closed connection");
async function sendMessage(value, channel) {
const response = await fetch("/push/" + channel, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({text: value})
});
if (response.status !== 200) {
alert("Sending message failed");
}
}
async function createChannel(id) {
const response = await fetch("/new-channel?id=" + encodeURIComponent(id));
if (response.status !== 200) {
alert("Creating channel failed");
}
}
async function deleteChannel(id) {
const response = await fetch("/del-channel?id=" + encodeURIComponent(id));
if (response.status !== 200) {
alert("Deleting channel failed");
}
}
function messageHandler(event) {
const data = JSON.parse(event.data);
if (data.t === "new") {
const channel = data.id;
const div = document.createElement("div");
div.id = "channel-" + channel;
const span = document.createElement("span");
span.innerText = channel;
div.appendChild(span);
const delButton = document.createElement("button");
delButton.innerText = "Delete channel";
delButton.onclick = () => deleteChannel(channel);
div.appendChild(delButton);
const input = document.createElement("input");
input.placeholder = "Message";
div.appendChild(input);
const sendButton = document.createElement("button");
sendButton.innerText = "Send Message";
sendButton.onclick = () => sendMessage(input.value, channel);
input.onkeyup = (e) => {
if (e.key === "Enter") {
e.preventDefault();
sendButton.click();
}
};
div.appendChild(sendButton);
const CBLabel = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = false;
checkbox.onchange = () => socket.send((checkbox.checked ? "sub" : "pop") + channel);
CBLabel.append(checkbox, " Subscribe");
div.appendChild(CBLabel);
const innerDiv = document.createElement("div");
innerDiv.id = "channel-" + channel + "-msgs";
div.appendChild(innerDiv);
channelsDiv.appendChild(div);
} else if (data.t === "del") {
document.getElementById("channel-" + data.id).remove();
} else if (data.t === "msg") {
document.getElementById("channel-" + data.id + "-msgs").innerText += "\n" + JSON.stringify(data.msg, null, 2);
}
}
</script>
</body>
</html>