-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.html
29 lines (29 loc) · 879 Bytes
/
web.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket API</title>
</head>
<body>
<script>
const URL = 'ws://localhost:8080';
const socket = new WebSocket(URL);
socket.addEventListener('open', ev => {
console.log(`Connected successfully to ${URL}`);
socket.send('Hello from the website!')
})
socket.addEventListener('message', ev => {
console.log(`recv: ${ev.data}`);
console.log('Closing...');
socket.close();
})
socket.addEventListener('close', ev => {
console.log(`Connection closed`, ev);
})
socket.addEventListener('error', err => {
console.error(`Error: ${err}`);
})
</script>
</body>
</html>