-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (50 loc) · 1.22 KB
/
script.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
/**
@type {WebSocket}
**/
let ws;
let retryCount = 0;
const msgInput = document.getElementById("msg");
const sendBtn = document.getElementById("send");
const msgBox = document.getElementById("msg-box");
const addMessage = (msg) => {
msgBox.innerHTML += `<p style="margin:0px; padding: 5px 10px;border:1px solid black;">${msg}</p>`;
}
if (!ws) {
while (retryCount < 5 && !ws) {
ws = new WebSocket("ws://127.0.0.1:8080");
retryCount++;
}
retryCount = 0;
console.log("web socket not created");
}
console.log("web socket:", ws);
ws.onopen = (e) => {
console.log("socket opened:", e)
}
ws.onmessage = (e) => {
// xss me daddy
addMessage("server: " + e.data);
console.log("socket messaged:", e)
}
ws.onerror = (e) => {
console.log("socket errored:", e)
}
ws.onclose = (e) => {
console.log("socket closed:", e)
}
msgInput.addEventListener("keyup", (e) => {
e.preventDefault();
if (e.key === "Enter") {
if (msgInput.value === "") return;
ws.send(msgInput.value);
addMessage("me: " + msgInput.value);
msgInput.value = "";
}
})
sendBtn.addEventListener("click", (e) => {
e.preventDefault();
if (msgInput.value === "") return;
ws.send(msgInput.value);
addMessage("me: " + msgInput.value);
msgInput.value = "";
})