-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
46 lines (40 loc) · 1.66 KB
/
index.html
File metadata and controls
46 lines (40 loc) · 1.66 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Client</title>
</head>
<body>
<input type="text" id="channel" placeholder="Enter Channel">
<input type="text" id="message" placeholder="Enter Message">
<button id="subscribe">Subscribe</button>
<button id="unsubscribe">Unsubscribe</button>
<button id="send">Send Message</button>
<div id="messages"></div>
<script>
const ws = new WebSocket('ws://localhost:8080');
const messagesContainer = document.getElementById('messages');
ws.onopen = () => {
console.log('Connected to WebSocket server');
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
const messageElement = document.createElement('div');
messageElement.textContent = `Channel: ${ JSON.stringify(data.channel, null, 2)}, Message: ${ JSON.stringify(data.message, null, 2)}`;
messagesContainer.appendChild(messageElement);
};
document.getElementById('subscribe').onclick = () => {
const channel = document.getElementById('channel').value;
ws.send(JSON.stringify({ action: 'subscribe', channel }));
};
document.getElementById('unsubscribe').onclick = () => {
ws.send(JSON.stringify({ action: 'unsubscribe' }));
};
document.getElementById('send').onclick = () => {
const message = document.getElementById('message').value;
ws.send(JSON.stringify({ action: 'send-message', message }));
};
</script>
</body>
</html>