@@ -2,14 +2,13 @@ let currentAnimation = '';
22let currentStatus = 0 ;
33let currentColor = '' ;
44
5- let websocketReady = false ;
5+ let ws_pending_msg ;
66let ws_uri = 'ws://' + ( location . hostname ? location . hostname : 'localhost' ) + ':81/' ;
77let connection = new WebSocket ( ws_uri , [ 'arduino' ] ) ;
88connection . binaryType = 'arraybuffer' ;
99
1010connection . onopen = function ( e ) {
11- sendText ( 'requesting_config' ) ;
12-
11+ ws_pending_msg = [ 30 ]
1312 connectingOverlayText . innerHTML = 'Waiting for response...' ;
1413 refreshButton . style . display = 'none' ;
1514} ;
@@ -27,7 +26,6 @@ connection.onclose = function (e) {
2726connection . onmessage = function ( e ) {
2827 console . log ( 'Received: ' , e . data ) ;
2928
30- websocketReady = true ;
3129 try {
3230 handleJsonData ( JSON . parse ( e . data ) ) ;
3331
@@ -38,7 +36,7 @@ connection.onmessage = function (e) {
3836
3937function handleJsonData ( data ) {
4038 if ( data . hasOwnProperty ( 'animations' ) ) {
41- data . animations . forEach ( animation => {
39+ data . animations . forEach ( ( animation , idx ) => {
4240 // Fill animation dropdowns
4341 alarmAnimation . add ( new Option ( animation ) ) ;
4442 postAlarmAnimation . add ( new Option ( animation ) ) ;
@@ -50,7 +48,7 @@ function handleJsonData(data) {
5048 let btn = document . createElement ( 'button' ) ;
5149 btn . classList . add ( 'btn-secondary' , 'btn' ) ;
5250 btn . innerHTML = animation ;
53- btn . onclick = ( ) => { sendAnimationButton ( animation ) ; } ;
51+ btn . onclick = ( ) => { sendAnimationButton ( idx ) ; } ;
5452 animationButtons . insertBefore ( btn , stopButton ) ;
5553 }
5654 } )
@@ -164,18 +162,15 @@ function sendConfig() {
164162}
165163
166164function sendBytes ( ) {
167- if ( connection . readyState == 1 && websocketReady ) {
168- let data = new Uint8Array ( arguments . length ) ;
169- for ( let i = 0 ; i < arguments . length ; i ++ ) {
170- data [ i ] = arguments [ i ] ;
165+ if ( connection . readyState == 1 && ws_pending_msg ) {
166+ let data = new Uint8Array ( ws_pending_msg . length ) ;
167+ for ( let i = 0 ; i < ws_pending_msg . length ; i ++ ) {
168+ data [ i ] = ws_pending_msg [ i ] ;
171169 }
170+ ws_pending_msg = undefined ;
172171 connection . send ( data . buffer ) ;
173- websocketReady = false ;
174172 console . log ( 'Sent bytes: ' + data ) ;
175173 }
176- else {
177- console . log ( 'Not connected. Data not sent!' ) ;
178- }
179174}
180175
181176function sendText ( text ) {
@@ -189,7 +184,7 @@ function sendText(text) {
189184}
190185
191186function sendAnimationButton ( animation ) {
192- sendText ( 'toggle ' + animation ) ;
187+ ws_pending_msg = [ 1 , animation ] ;
193188}
194189
195190
@@ -219,7 +214,7 @@ let $customColorPicker = $('#colorButton').colorPicker({
219214 let newColor = this . color . colors . HEX + '' ; // dereference by appending ''
220215 if ( currentColor != newColor || toggled === true ) {
221216 let newColorRGB = this . color . colors . RND . rgb ;
222- sendBytes ( 0 , newColorRGB . r , newColorRGB . g , newColorRGB . b ) ;
217+ ws_pending_msg = [ 0 , newColorRGB . r , newColorRGB . g , newColorRGB . b ] ;
223218 currentColor = newColor ;
224219 document . querySelector ( '#colorButton' ) . style . borderColor = '#' + newColor ;
225220 }
@@ -229,19 +224,22 @@ let $customColorPicker = $('#colorButton').colorPicker({
229224// Sliders
230225noUiSlider . create ( speed , { start : 128 , step : 1 , range : { 'min' : 0 , 'max' : 255 } } ) ;
231226noUiSlider . create ( saturation , { start : 255 , step : 1 , range : { 'min' : 110 , 'max' : 255 } } ) ;
232- speed . noUiSlider . on ( 'update' , ( values , handle ) => { sendBytes ( 1 , values [ handle ] ) ; } ) ;
233- saturation . noUiSlider . on ( 'update' , ( values , handle ) => { sendBytes ( 5 , values [ handle ] ) ; } ) ;
227+ speed . noUiSlider . on ( 'update' , ( values , handle ) => { ws_pending_msg = [ 20 , values [ handle ] ] ; } ) ;
228+ saturation . noUiSlider . on ( 'update' , ( values , handle ) => { ws_pending_msg = [ 21 , values [ handle ] ] ; } ) ;
234229document . querySelector ( '#speed .noUi-handle' ) . innerHTML = 'Speed' ;
235230document . querySelector ( '#saturation .noUi-handle' ) . innerHTML = 'Sat.' ;
236231
237232// Close connection after being ianctive for 5 minutes
238233let idleTime = 0 ;
239234setInterval ( ( ) => {
240235 idleTime ++ ;
241- if ( idleTime > 300 ) // s
236+ if ( idleTime > 300 ) // in s
242237 connection . close ( ) ;
243238} , 1000 ) ;
244239
240+ // Rate limit websocket
241+ setInterval ( sendBytes , 15 ) ;
242+
245243document . addEventListener ( 'mousemove' , e => { idleTime = 0 ; } ) ;
246244document . addEventListener ( 'keypress' , e => { idleTime = 0 ; } ) ;
247245document . addEventListener ( 'visibilitychange' , ( ) => {
@@ -252,4 +250,4 @@ document.addEventListener('visibilitychange', () => {
252250 else
253251 location . reload ( ) ;
254252 }
255- } )
253+ } )
0 commit comments