-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
61 lines (56 loc) · 1.74 KB
/
index.html
File metadata and controls
61 lines (56 loc) · 1.74 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chat Application</title>
<style>
body {
font-family: Arial, sans-serif;
}
#chat {
border: 1px solid #ccc;
height: 300px;
overflow-y: scroll;
margin-bottom: 10px;
padding: 10px;
}
#message {
width: 80%;
padding: 10px;
}
#send {
padding: 10px;
}
</style>
</head>
<body>
<div id="chat"></div>
<input type="text" id="message" placeholder="Type a message..." />
<button id="send">Send</button>
<script>
const token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1hZGhhdiIsImlhdCI6MTcxOTM4NTkwNSwiZXhwIjoxNzE5Mzg5NTA1fQ.-fOw2PgRBud9snhMAO4pk3RvT9J6yEy44MmRnTMX6EQ";
const socket = new WebSocket(`ws://localhost:8000?token=${token}`);
socket.addEventListener("open", (event) => {
console.log("Connected to the server.");
});
socket.addEventListener("message", (event) => {
const chat = document.getElementById("chat");
const message = document.createElement("div");
message.textContent = event.data;
chat.appendChild(message);
chat.scrollTop = chat.scrollHeight;
});
socket.addEventListener("close", (event) => {
console.log("Disconnected from the server.");
});
document.getElementById("send").addEventListener("click", () => {
const messageInput = document.getElementById("message");
const message = messageInput.value;
socket.send(message);
messageInput.value = "";
});
</script>
</body>
</html>