Repository navigation
Expand file tree
/
Copy pathhome.jet
More file actions
193 lines (159 loc) · 6.28 KB
/
Copy pathhome.jet
File metadata and controls
193 lines (159 loc) · 6.28 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/notie/4.3.1/notie.min.css"
integrity="sha512-UrjLcAek5jbj1vwGbXkviPHtgSNVNQCedX7cBIMDdSI2iZtUcZcoTh2Sqc8R9mVcijOjFUi1IlxhfrE1uWaIog=="
crossorigin="anonymous"/>
<style>
.chatbox {
outline: 1px solid silver;
min-height: 160px;
padding: 0.5em;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<h1 class="mt-3">This is the home page</h1>
<hr>
</div>
</div>
<div class="row">
<div class="col-md-8">
<h3>Chat</h3>
<p><a href="https://github.com">Go to github</a></p>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" name="username" id="username" class="form-control" autocomplete="off">
</div>
<div class="form-group">
<label for="message">Message:</label>
<input type="text" name="message" id="message" class="form-control" autocomplete="off">
</div>
<hr>
<a href="javascript:void(0);" class="btn btn-outline-secondary" id="sendBtn">Send Message</a>
<input type="hidden" name=&qu
8000
ot;action" id="action">
<div id="status" class="mt-2 float-end">
</div>
<div id="output" class="chatbox mt-3">
</div>
</div>
<div class="col-md-4">
<h3>Who's Online</h3>
<ul id="online_users">
</ul>
</div>
</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/notie/4.3.1/notie.min.js"
integrity="sha512-NHRCwRf2LnVSlLDejCA9oS3fG3/FLSQIPCjAWl3M7tVi5wszwr6FxkjotWnQDXLE+aLKcxRrzFDNEgXj9nvkPw=="
crossorigin="anonymous"></script>
<script src="/static/reconnecting-websocket.min.js"></script>
<script>
let socket = null;
let o = document.getElementById("output");
let userField = document.getElementById("username");
let messageField = document.getElementById("message");
window. () {
console.log("Leaving");
let jsonData = {};
jsonData["action"] = "left";
socket.send(JSON.stringify(jsonData))
}
document.addEventListener("DOMContentLoaded", function () {
socket = new ReconnectingWebSocket("ws://127.0.0.1:8080/ws", null, {debug: true, reconectInterval: 3000});
const offline = `<span class="badge bg-danger">Not connected</span>`
const class="badge bg-success">Connected</span>`
let statusDiv = document.getElementById("status");
socket. => {
console.log("Successfully connected");
statusDiv.innerHTML = online;
}
socket. => {
console.log("connection closed");
statusDiv.innerHTML = offline;
}
socket. => {
console.log("there was an error");
statusDiv.innerHTML = offline;
}
socket. => {
let data = JSON.parse(msg.data);
console.log("Action is", data.action);
switch (data.action) {
case "list_users":
let ul = document.getElementById("online_users");
while (ul.firstChild) ul.removeChild(ul.firstChild);
if (data.connected_users.length > 0) {
data.connected_users.forEach(function (item) {
let li = document.createElement("li");
li.appendChild(document.createTextNode(item));
ul.appendChild(li);
})
}
break;
case "broadcast":
o.innerHTML = o.innerHTML + data.message + "<br>";
break;
}
}
userField.addEventListener("change", function () {
let jsonData = {};
jsonData["action"] = "username";
jsonData["username"] = this.value;
socket.send(JSON.stringify(jsonData));
})
messageField.addEventListener("keydown", function (event) {
if (event.code === "Enter") {
if (!socket) {
console.log("no connection");
return false
}
if ((userField.value === "") || (messageField.value === "")) {
errorMessage("Fill out username and message!");
return false;
} else {
sendMessage()
}
event.preventDefault();
event.stopPropagation();
}
})
document.getElementById("sendBtn").addEventListener("click", function () {
if ((userField.value === "") || (messageField.value === "")) {
errorMessage("Fill out username and message!");
return false;
} else {
sendMessage()
}
})
})
function sendMessage() {
let jsonData = {};
jsonData["action"] = "broadcast";
jsonData["username"] = userField.value;
jsonData["message"] = messageField.value;
socket.send(JSON.stringify(jsonData))
messageField.value = "";
}
function errorMessage(msg) {
notie.alert({
type: 'error',
text: msg,
})
}
</script>
</html>