-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
79 lines (68 loc) · 2.3 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
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
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WS Test Client</title>
</head>
<body>
<div>
<button id="connect">Connect</button><button id="disconnect">Disconnect</button><button id="abandon">Abandon</button><br>
<input placeholder="Message"></input><button id="send">Send</button>
</div>
<script>
const HOST = 'plotter-server.local';
const PORT = '4321';
let ws;
function connect() {
ws = new WebSocket(`ws://${HOST}:${PORT}`);
ws.onopen = (e) => {
console.log('Connection open', e);
};
ws.onmessage = (e) => {
console.log('Message', e.data, e);
};
ws.onerror = (e) => {
console.log('Connection error', e);
ws = null;
};
ws.onclose = (e) => {
console.log('Connection closed', e);
ws = null;
};
}
function disconnect() {
ws?.close(1000, 'disconnect');
}
function abandon() {
console.log("Abandon connection")
if (ws) {
ws.onopen = undefined;
ws.onmessage = undefined;
ws.onerror = undefined;
ws.onclose = undefined;
}
ws = undefined;
}
const input = document.querySelector('input');
const btn_send = document.querySelector('#send');
const btn_connect = document.querySelector('#connect');
const btn_disconnect = document.querySelector('#disconnect');
const btn_abandon = document.querySelector('#abandon');
function send() {
if (ws) {
const msg = input.value;
input.value = '';
ws.send(msg);
}
}
btn_send.onmousedown = send;
btn_connect.onmousedown = connect;
btn_disconnect.onmousedown = disconnect;
btn_abandon.onmousedown = abandon;
input.onkeydown = (e) => {
if (e.key === 'Enter') send();
}
</script>
</body>
</html>