-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.html
More file actions
55 lines (51 loc) · 1.34 KB
/
client.html
File metadata and controls
55 lines (51 loc) · 1.34 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Client</title>
<style>
body {
font-family: Arial, sans-serif;
}
#messages {
border: 1px solid #ccc;
padding: 10px;
margin-top: 20px;
height: 300px;
overflow-y: scroll;
}
.message {
margin-bottom: 10px;
padding: 5px;
border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>
<h1>WebSocket Client</h1>
<div id="messages"></div>
<script>
const ws = new WebSocket('ws://localhost:9080');
ws.onopen = () => {
console.log('Connected to the WebSocket server');
// Send a JSON formatted message to the server
ws.send(JSON.stringify({ type: 'greeting', name: 'Alice' }));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data);
displayMessage(data);
};
ws.onclose = () => {
console.log('Disconnected from the WebSocket server');
};
function displayMessage(data) {
const messagesDiv = document.getElementById('messages');
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
messageDiv.innerText = `${data.message}`;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight; // Scroll to the bottom
}
</script>
</body>
</html>