Skip to content

Commit d862137

Browse files
committed
fixed Reconnecting webSocket using joewalnes' js
1 parent 4421c57 commit d862137

File tree

3 files changed

+263
-268
lines changed

3 files changed

+263
-268
lines changed

Python_Data_Monitor/PyScripts/www/html/speedometer_ws.html

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<script src="js/jquery.min.js"></script>
6262
<script src="js/highcharts-more.js"></script>
6363
<script src="js/solid-gauge.js"></script>
64+
<script src="js/reconnectingWebSockets.js"></script>
6465

6566

6667
<figure class="highcharts-figure">
@@ -70,7 +71,7 @@
7071

7172

7273
<script type="text/javascript">
73-
Highcharts.setOptions( {
74+
Highcharts.setOptions({
7475
chart: {
7576
type: 'solidgauge',
7677
backgroundColor: 'rgba(255, 255, 255, 0)',
@@ -82,8 +83,8 @@
8283
animation: Highcharts.svg, // don't animate in old IE
8384
marginRight: 10,
8485
},
85-
title:null,
86-
pane:{
86+
title: null,
87+
pane: {
8788
center: ['50%', '85%'],
8889
size: '140%',
8990
startAngle: -90,
@@ -124,7 +125,7 @@
124125
}
125126
});
126127
var chart1 = Highcharts.chart('container', {
127-
title:null,
128+
title: null,
128129
yAxis: {
129130
min: 60,
130131
max: 160,
@@ -135,8 +136,8 @@
135136
credits: {
136137
enabled: false
137138
},
138-
tooltip:{
139-
enabled:false
139+
tooltip: {
140+
enabled: false
140141
},
141142
series: [{
142143
name: 'BPM',
@@ -152,56 +153,50 @@
152153
}]
153154
});
154155

156+
var lockReconnect;
157+
var timeConnect = 0
155158
if ('WebSocket' in window) {
156-
var ws = new WebSocket('ws://127.0.0.1:8919/ws/hr_json');
159+
// https://github.com/joewalnes/reconnecting-websocket/
160+
var ws = new ReconnectingWebSocket('ws://127.0.0.1:8919/ws/hr_json');
157161
}
158162
else {
159-
alert ('Your browser doesn\'t support websocket, please use api mode instead!')
160-
}
161-
function reconnect(){
162-
setTimeout(function(){
163-
var ws = new WebSocket('ws://127.0.0.1:8919/ws/hr_json');
164-
ws.onclose = function () {
165-
console.warn('Connection closed. Reconnecting.')
166-
reconnect()
167-
};
168-
ws.onerror = function () {
169-
console.error('Connection failed. Reconnecting.')
170-
reconnect()
171-
};
172-
},2000)
163+
alert('Your browser doesn\'t support websocket, please use api mode instead!')
173164
}
174-
ws.onopen = function(){
165+
166+
ws.onopen = function () {
175167
//HeartBeat
176-
setInterval(function(){
177-
ws.send('heartbeat');
178-
console.log('Websocket heartbeat')
179-
},2000);
168+
try {
169+
setInterval(function () {
170+
ws.send('heartbeat');
171+
console.log('Websocket heartbeat')
172+
}, 2000);
173+
}
174+
catch (error) {
175+
console.error(error)
176+
}
180177
};
181178
var i = 0;
182-
ws.onmessage = function(json){
183-
i=i+1;
184-
data= $.parseJSON(json.data);
185-
hr_bpm=data.hr;
186-
point=chart1.series[0].points[0];
187-
if (i % 5 == 0){
179+
ws.onmessage = function (json) {
180+
i = i + 1;
181+
data = $.parseJSON(json.data);
182+
hr_bpm = data.hr;
183+
point = chart1.series[0].points[0];
184+
if (i % 5 == 0) {
188185
var val = parseInt(hr_bpm);
189186
console.log(hr_bpm);
190187
point.update(val);
191188
};
192-
if(i>20000){
193-
i=0;
189+
if (i > 20000) {
190+
i = 0;
194191
};
195192
};
196193

197-
ws.onclose = function(){
198-
console.warn('Connection closed. Reconnecting.')
199-
reconnect()
194+
ws.onclose = function () {
195+
console.warn('Connection closed. Reconnecting.');
200196
};
201197

202-
ws.onerror = function(){
203-
console.error('Connection failed. Reconnecting.')
204-
reconnect()
198+
ws.onerror = function () {
199+
console.error('Connection failed. Reconnecting.');
205200
};
206201

207202
</script>

0 commit comments

Comments
 (0)