-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets.html
More file actions
195 lines (159 loc) · 5.78 KB
/
websockets.html
File metadata and controls
195 lines (159 loc) · 5.78 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="junky">
<title>junky.github.io</title>
<!-- Bootstrap core CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<!-- Custom styles for this template -->
<style>
body {
padding-top: 75px;
}
.starter-template {
padding: 40px 15px;
text-align: center;
}
.odometer {
font-size: 100px;
padding-bottom: 50px;
}
</style>
<!-- Odometr includes -->
<link rel="stylesheet" href="https://github.hubspot.com/odometer/themes/odometer-theme-car.css">
<script src="https://github.hubspot.com/odometer/odometer.js"></script>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">junky.github.io</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/noingest.html">No Ingest</a></li>
<li class="nav-item"><a class="nav-link" href="/xhr-demo.html">XHR</a></li>
<li class="nav-item"><a class="nav-link" href="/dates.html">Dates</a></li>
<li class="nav-item"><a class="nav-link" href="/mt.html">MT</a></li>
<li class="nav-item"><a class="nav-link" href="/markdown.html">Markdown</a></li>
<li class="nav-item"><a class="nav-link" href="/dcs-test.html">DCS Test</a></li>
<li class="nav-item"><a class="nav-link" href="/swaps.html">Swaps</a></li>
<li class="nav-item"><a class="nav-link" href="/vue-attributes.html">Vue Attributes</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div id="ws-swap" class="starter-template sl_swap">
<p>15 days before departure for BA mainline and JB Routes</p>
<p>days before departure for BA mainline and JB Routes</p>
<p>15 days after departure for BA mainline and JB Routes</p>
</div>
<div class="starter-template">
<div style="padding: 20px 30px;">
<b>Server URL:</b>
<input type="text" name="server" id="server" style="width: 250px;" value="ws://echo.websocket.org" />
<input type="button" value="Open" id="conn_button" onclick="onConnButtonClick();" />
</div>
<div id="odometer" class="odometer">123</div>
<div style="padding-top:50px;">
<textarea id="log" style="width: 600px; height: 250px;"></textarea>
</div>
</div>
</div><!-- /.container -->
<script type='text/javascript'>
timer = 0;
websocket = null;
isConnected = false;
if(localStorage.getItem("ws_url1") !== null) {
$('#server').val(localStorage.ws_url1);
}
function dateToString(date) {
var min = date.getMinutes();
if(min<10) {
min = "0" + min;
}
var sec = date.getSeconds();
if(sec<10) {
sec = "0" + sec;
}
var dateOfString = date.getHours() + ":" + min + ":" + sec;
return dateOfString;
}
function log(msg) {
$('#log').text(dateToString(new Date()) + " " + msg + "\n" + $('#log').text());
}
function openWebSocket(wsUri) {
log('Connecting to ' + wsUri + ' ... ');
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt) {
log("CONNECTED");
doSend('Websocket works.');
timer = setTimeout(sendNumberedMessage, 5000);
isConnected = true;
$('#conn_button').val('Close');
}
function onClose(evt) {
log("DISCONNECTED");
clearTimeout(timer);
timer = 0;
isConnected = false;
$('#conn_button').val('Open');
}
function onMessage(evt) {
log('RESPONSE: ' + evt.data);
s = evt.data;
if(s.indexOf("nm:")==0) {
odometer.innerHTML = s.substring(3);
}
}
function onError(evt) {
log('ERROR: ' + evt.data);
clearTimeout(timer);
timer = 0;
}
function doSend(message) {
log("SENT: " + message);
websocket.send(message);
}
function getRandomInt (min, max) {
var num = Math.floor(Math.random() * (max - min + 1)) + min;
return num;
}
function sendNumberedMessage() {
var r = getRandomInt(100, 999);
doSend('nm:' + r);
timer = setTimeout(sendNumberedMessage, 5000);
}
function onConnButtonClick(){
if(!isConnected) {
openWebSocket($('#server').val());
localStorage.ws_url1 = $('#server').val();
} else {
clearTimeout(timer);
timer = 0;
websocket.close();
}
}
</script>
<!-- Bootstrap core JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>