-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
103 lines (101 loc) · 4.16 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Gockets Demonstration</title>
<style>
.flex-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.flex-column {
display: flex;
flex-direction: column;
margin-right: 20px;
min-width: 300px;
}
label, input, button {
margin-bottom: 10px;
}
.buttons-row button {
width: 50%;
}
</style>
</head>
<body class="flex-row">
<div class="flex-column">
<label for="output">Websocket Output:</label>
<textarea readonly id="output" cols="50" rows="15"></textarea>
</div>
<div class="flex-column">
<label for="subscriber-token">Subscriber Token:</label><input type="text" id="subscriber-token">
<div class="flex-row buttons-row">
<button id="ws-connect">Connect</button>
<button id="ws-disconnect">Disconnect</button>
</div>
<label for="message-to-send">Message Text</label><input type="text" id="message-to-send">
<button id="ws-send">Send</button>
</div>
<script>
window.onload = function () {
const outputTextArea = document.getElementById('output');
const connectBtn = document.getElementById('ws-connect');
const disconnectBtn = document.getElementById('ws-disconnect');
const subToken = document.getElementById('subscriber-token');
const sendBtn = document.getElementById('ws-send');
const sendText = document.getElementById('message-to-send');
connectBtn.addEventListener('click', function () {
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8844/channel/subscribe/' + subToken.value);
// Connection opened
socket.addEventListener('open', function (event) {
let textAreaText = outputTextArea.value;
outputTextArea.value = textAreaText + getCurrentTime() + " Connection opened \n";
console.log(event);
});
socket.addEventListener('close', function (event) {
let textAreaText = outputTextArea.value;
outputTextArea.value = textAreaText + getCurrentTime() + " Connection closed by remote \n";
console.log(event);
});
// Listen for messages
socket.addEventListener('message', function (event) {
let textAreaText = outputTextArea.value;
outputTextArea.value = textAreaText + getCurrentTime() + " Got data from socket: \n" + event.data + "\n";
console.log(event);
});
sendBtn.addEventListener('click', function (event) {
socket.send(sendText.value);
let textAreaText = outputTextArea.value;
outputTextArea.value = textAreaText + getCurrentTime() + " Send data to socket: " + sendText.value + "\n";
});
disconnectBtn.addEventListener('click', function (event) {
socket.close();
let textAreaText = outputTextArea.value;
outputTextArea.value = textAreaText + getCurrentTime() + " Connection closed by client\n";
})
});
function getCurrentTime() {
const current = new Date();
let hours = current.getHours();
let minutes = current.getMinutes();
let seconds = current.getSeconds();
if (hours < 10) {
hours = '0' + hours;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
return '[' + hours + ':' + minutes + ':' + seconds + ']';
}
};
</script>
</body>
</html>