-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
42 lines (35 loc) · 1.22 KB
/
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
33
34
35
36
37
38
39
40
41
42
<!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>
<h1>WebSocket Client</h1>
<div id="messages"></div>
<input type="text" id="messageInput" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
<script>
// Create a new WebSocket.
const socket = new WebSocket('wss://localhost:3000');
// Connection opened.
socket.addEventListener('open', function (event) {
console.log('Connected to WebSocket server.');
});
// Listen for messages.
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML += '<p>' + event.data + '</p>';
});
// Send a message to the WebSocket server.
function sendMessage() {
const input = document.getElementById('messageInput');
const message = input.value;
socket.send(message);
input.value = '';
}
</script>
</body>
</html>