|
61 | 61 | <script src="js/jquery.min.js"></script> |
62 | 62 | <script src="js/highcharts-more.js"></script> |
63 | 63 | <script src="js/solid-gauge.js"></script> |
| 64 | + <script src="js/reconnectingWebSockets.js"></script> |
64 | 65 |
|
65 | 66 |
|
66 | 67 | <figure class="highcharts-figure"> |
|
70 | 71 |
|
71 | 72 |
|
72 | 73 | <script type="text/javascript"> |
73 | | - Highcharts.setOptions( { |
| 74 | + Highcharts.setOptions({ |
74 | 75 | chart: { |
75 | 76 | type: 'solidgauge', |
76 | 77 | backgroundColor: 'rgba(255, 255, 255, 0)', |
|
82 | 83 | animation: Highcharts.svg, // don't animate in old IE |
83 | 84 | marginRight: 10, |
84 | 85 | }, |
85 | | - title:null, |
86 | | - pane:{ |
| 86 | + title: null, |
| 87 | + pane: { |
87 | 88 | center: ['50%', '85%'], |
88 | 89 | size: '140%', |
89 | 90 | startAngle: -90, |
|
124 | 125 | } |
125 | 126 | }); |
126 | 127 | var chart1 = Highcharts.chart('container', { |
127 | | - title:null, |
| 128 | + title: null, |
128 | 129 | yAxis: { |
129 | 130 | min: 60, |
130 | 131 | max: 160, |
|
135 | 136 | credits: { |
136 | 137 | enabled: false |
137 | 138 | }, |
138 | | - tooltip:{ |
139 | | - enabled:false |
| 139 | + tooltip: { |
| 140 | + enabled: false |
140 | 141 | }, |
141 | 142 | series: [{ |
142 | 143 | name: 'BPM', |
|
152 | 153 | }] |
153 | 154 | }); |
154 | 155 |
|
| 156 | + var lockReconnect; |
| 157 | + var timeConnect = 0 |
155 | 158 | 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'); |
157 | 161 | } |
158 | 162 | 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!') |
173 | 164 | } |
174 | | - ws.onopen = function(){ |
| 165 | + |
| 166 | + ws.onopen = function () { |
175 | 167 | //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 | + } |
180 | 177 | }; |
181 | 178 | 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) { |
188 | 185 | var val = parseInt(hr_bpm); |
189 | 186 | console.log(hr_bpm); |
190 | 187 | point.update(val); |
191 | 188 | }; |
192 | | - if(i>20000){ |
193 | | - i=0; |
| 189 | + if (i > 20000) { |
| 190 | + i = 0; |
194 | 191 | }; |
195 | 192 | }; |
196 | 193 |
|
197 | | - ws.onclose = function(){ |
198 | | - console.warn('Connection closed. Reconnecting.') |
199 | | - reconnect() |
| 194 | + ws.onclose = function () { |
| 195 | + console.warn('Connection closed. Reconnecting.'); |
200 | 196 | }; |
201 | 197 |
|
202 | | - ws.onerror = function(){ |
203 | | - console.error('Connection failed. Reconnecting.') |
204 | | - reconnect() |
| 198 | + ws.onerror = function () { |
| 199 | + console.error('Connection failed. Reconnecting.'); |
205 | 200 | }; |
206 | 201 |
|
207 | 202 | </script> |
|
0 commit comments