-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
32 lines (28 loc) · 938 Bytes
/
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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Example</title>
</head>
<body>
<h1>WebSocket Example</h1>
<div>
<input type="text" id="messageInput" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
</div>
<div id="output"></div>
<script>
const userId = prompt("Enter your user ID (e.g., user1, user2):"); // Prompt the user for their user ID
const ws = new WebSocket(`ws://localhost:8000/ws/${userId}`);
ws.onmessage = function (event) {
const outputDiv = document.getElementById("output");
outputDiv.innerHTML += `<p>Received: ${event.data}</p>`;
};
function sendMessage() {
const messageInput = document.getElementById("messageInput");
const message = messageInput.value;
ws.send(message);
messageInput.value = "";
}
</script>
</body>
</html>