-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-ws.html
More file actions
70 lines (61 loc) · 1.43 KB
/
test-ws.html
File metadata and controls
70 lines (61 loc) · 1.43 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
62
63
64
65
66
67
68
69
70
<!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>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
#output {
height: 200px;
overflow-y: auto;
border: 1px solid #ccc;
margin-bottom: 10px;
padding: 10px;
}
#input {
width: 100%;
padding: 5px;
}
button {
padding: 5px;
}
</style>
</head>
<body>
<div>
<div id="output"></div>
<input type="text" id="input" placeholder="Type your message">
<button onclick="sendMessage()">Send</button>
</div>
<script>
const socket = new WebSocket('ws://localhost:3000');
const output = document.getElementById('output');
const input = document.getElementById('input');
socket.onopen = () => {
log('WebSocket connection opened');
};
socket.onmessage = (event) => {
log(`Received: ${event.data}`);
};
socket.onclose = () => {
log('WebSocket connection closed');
};
function sendMessage() {
const message = input.value.trim();
if (message !== '') {
socket.send(message);
log(`You said: ${message}`);
input.value = '';
}
}
function log(message) {
output.innerHTML += `<p>${message}</p>`;
output.scrollTop = output.scrollHeight;
}
</script>
</body>
</html>