From e0931ed3518a1b8c89ac37e37f94f32a6a87bdfb Mon Sep 17 00:00:00 2001 From: Jamelele <35628223+Jamelele@users.noreply.github.com> Date: Sun, 24 May 2020 15:22:39 +0000 Subject: [PATCH 1/5] Add callbacks to all events to prevent requests from stalling --- cl_plate_reader.lua | 4 +++- cl_radar.lua | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cl_plate_reader.lua b/cl_plate_reader.lua index c6f868d..7c93274 100644 --- a/cl_plate_reader.lua +++ b/cl_plate_reader.lua @@ -174,15 +174,17 @@ AddEventHandler( "wk:togglePlateLock", function( cam, beep, bolo ) end ) -- Runs when the "Toggle Display" button is pressed on the plate reder box -RegisterNUICallback( "togglePlateReaderDisplay", function() +RegisterNUICallback( "togglePlateReaderDisplay", function( data, cb ) -- Toggle the display state READER:ToggleDisplayState() + cb({}) end ) -- Runs when the "Set BOLO Plate" button is pressed on the plate reader box RegisterNUICallback( "setBoloPlate", function( plate, cb ) -- Set the BOLO plate READER:SetBoloPlate( plate ) + cb({}) end ) -- This is the main function that runs and scans all vehicles in front and behind the patrol vehicle diff --git a/cl_radar.lua b/cl_radar.lua index c01f8fa..cf806d5 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -1419,25 +1419,28 @@ end NUI callback ----------------------------------------------------------------------------------]]-- -- Runs when the "Toggle Display" button is pressed on the remote control -RegisterNUICallback( "toggleRadarDisplay", function() +RegisterNUICallback( "toggleRadarDisplay", function( data, cb ) -- Toggle the display state RADAR:ToggleDisplayState() + cb({}) end ) -- Runs when the user presses the power button on the radar ui -RegisterNUICallback( "togglePower", function() +RegisterNUICallback( "togglePower", function( data, cb ) -- Toggle the radar's power RADAR:TogglePower() + cb({}) end ) -- Runs when the user presses the ESC or RMB when the remote is open -RegisterNUICallback( "closeRemote", function() +RegisterNUICallback( "closeRemote", function( data, cb ) -- Remove focus to the NUI side SetNuiFocus( false, false ) + cb({}) end ) -- Runs when the user presses any of the antenna mode buttons on the remote -RegisterNUICallback( "setAntennaMode", function( data ) +RegisterNUICallback( "setAntennaMode", function( data, cb ) -- Only run the codw if the radar has power and is not powering up if ( RADAR:IsPowerOn() and not RADAR:IsPoweringUp() ) then -- As the mode buttons are used to exit the menu, we check for that @@ -1464,11 +1467,12 @@ RegisterNUICallback( "setAntennaMode", function( data ) SendNUIMessage( { _type = "audio", name = "beep", vol = RADAR:GetSettingValue( "beep" ) } ) end ) end - end + end + cb({}) end ) -- Runs when the user presses either of the XMIT/HOLD buttons on the remote -RegisterNUICallback( "toggleAntenna", function( data ) +RegisterNUICallback( "toggleAntenna", function( data, cb ) -- Only run the codw if the radar has power and is not powering up if ( RADAR:IsPowerOn() and not RADAR:IsPoweringUp() ) then -- As the xmit/hold buttons are used to change settings in the menu, we check for that @@ -1488,11 +1492,12 @@ RegisterNUICallback( "toggleAntenna", function( data ) SendNUIMessage( { _type = "audio", name = RADAR:IsAntennaTransmitting( data.value ) and "xmit_on" or "xmit_off", vol = RADAR:GetSettingValue( "beep" ) } ) end ) end - end + end + cb({}) end ) -- Runs when the user presses the menu button on the remote control -RegisterNUICallback( "menu", function() +RegisterNUICallback( "menu", function( data, cb ) -- Only run the codw if the radar has power and is not powering up if ( RADAR:IsPowerOn() and not RADAR:IsPoweringUp() ) then -- As the menu button is a multipurpose button, we first check to see if the menu is already open @@ -1509,18 +1514,21 @@ RegisterNUICallback( "menu", function() -- Play the standard audio beep SendNUIMessage( { _type = "audio", name = "beep", vol = RADAR:GetSettingValue( "beep" ) } ) - end + end + cb({}) end ) -- Runs when the JavaScript side sends the UI data for saving RegisterNUICallback( "saveUiData", function( data, cb ) UTIL:Log( "Saving updated UI settings data." ) SetResourceKvp( "wk_wars2x_ui_data", json.encode( data ) ) + cb({}) end ) -- Runs when the JavaScript side sends the quick start video has been watched RegisterNUICallback( "qsvWatched", function( data, cb ) SetResourceKvpInt( "wk_wars2x_new_user", 1 ) + cb({}) end ) From 4ec8391b6f14196f59d8b93cda1cc9ffa69516ca Mon Sep 17 00:00:00 2001 From: Jamelele <35628223+Jamelele@users.noreply.github.com> Date: Sun, 24 May 2020 15:22:52 +0000 Subject: [PATCH 2/5] Send nui requests over HTTPS --- nui/radar.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nui/radar.js b/nui/radar.js index 3a31ea0..a37e4af 100644 --- a/nui/radar.js +++ b/nui/radar.js @@ -622,7 +622,7 @@ function displayKeyLock( state ) // This function is used to send data back through to the LUA side function sendData( name, data ) { - $.post( "http://" + resourceName + "/" + name, JSON.stringify( data ), function( datab ) { + $.post( "https://" + resourceName + "/" + name, JSON.stringify( data ), function( datab ) { if ( datab != "ok" ) { console.log( datab ); } @@ -1033,7 +1033,7 @@ $( "body" ).find( "button, div" ).each( function( i, obj ) { ------------------------------------------------------------------------------------*/ function closeRemote() { - sendData( "closeRemote", null ); + sendData( "closeRemote", {} ); setEleVisible( elements.plateReaderBox, false ); setEleVisible( elements.uiSettingsBox, false ); From 285cee53a06952bcd70c222789d0c0ee2fea8bac Mon Sep 17 00:00:00 2001 From: Jamelele <35628223+Jamelele@users.noreply.github.com> Date: Sun, 24 May 2020 21:04:06 +0000 Subject: [PATCH 3/5] Set correct headers for NUI callbacks --- nui/radar.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nui/radar.js b/nui/radar.js index a37e4af..5471414 100644 --- a/nui/radar.js +++ b/nui/radar.js @@ -620,6 +620,13 @@ function displayKeyLock( state ) }, 2000 ); } +// Prepare headers for HTTP requests +$.ajaxSetup({ + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, + }); + // This function is used to send data back through to the LUA side function sendData( name, data ) { $.post( "https://" + resourceName + "/" + name, JSON.stringify( data ), function( datab ) { @@ -1033,7 +1040,7 @@ $( "body" ).find( "button, div" ).each( function( i, obj ) { ------------------------------------------------------------------------------------*/ function closeRemote() { - sendData( "closeRemote", {} ); + sendData( "closeRemote", null ); setEleVisible( elements.plateReaderBox, false ); setEleVisible( elements.uiSettingsBox, false ); From bc13b14aedb7eb921615044298bf67c8d199a8f7 Mon Sep 17 00:00:00 2001 From: Jamelele <35628223+Jamelele@users.noreply.github.com> Date: Sun, 24 May 2020 21:52:26 +0000 Subject: [PATCH 4/5] Send empty payloads instead of 'null' Odd stuff seems to occur and you can't return the callback from lua --- nui/radar.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nui/radar.js b/nui/radar.js index 5471414..f1210cf 100644 --- a/nui/radar.js +++ b/nui/radar.js @@ -257,7 +257,7 @@ elements.closeHelp.click( function() { // Sets the action for the "No" button on the new user popup to close the popup elements.closeNewUser.click( function() { setEleVisible( elements.newUser, false ); - sendData( "qsvWatched", null ); + sendData( "qsvWatched", {} ); } ) // Sets the action for the "Yes" button on the new user popup to open the quick start window and load the video @@ -271,7 +271,7 @@ elements.openQsv.click( function() { elements.closeQsv.click( function() { setEleVisible( elements.qsvWindow, false ); loadQuickStartVideo( false ); - sendData( "qsvWatched", null ); + sendData( "qsvWatched", {} ); } ) @@ -1040,7 +1040,7 @@ $( "body" ).find( "button, div" ).each( function( i, obj ) { ------------------------------------------------------------------------------------*/ function closeRemote() { - sendData( "closeRemote", null ); + sendData( "closeRemote", {} ); setEleVisible( elements.plateReaderBox, false ); setEleVisible( elements.uiSettingsBox, false ); From b8b2eecaeaf6857168ac972a67ed0bcdcd46a47c Mon Sep 17 00:00:00 2001 From: Jamelele <35628223+Jamelele@users.noreply.github.com> Date: Tue, 2 Jun 2020 14:36:53 +0000 Subject: [PATCH 5/5] Return callbacks with 'ok' instead of an empty object --- cl_plate_reader.lua | 4 ++-- cl_radar.lua | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cl_plate_reader.lua b/cl_plate_reader.lua index 7c93274..aa817de 100644 --- a/cl_plate_reader.lua +++ b/cl_plate_reader.lua @@ -177,14 +177,14 @@ end ) RegisterNUICallback( "togglePlateReaderDisplay", function( data, cb ) -- Toggle the display state READER:ToggleDisplayState() - cb({}) + cb('ok') end ) -- Runs when the "Set BOLO Plate" button is pressed on the plate reader box RegisterNUICallback( "setBoloPlate", function( plate, cb ) -- Set the BOLO plate READER:SetBoloPlate( plate ) - cb({}) + cb('ok') end ) -- This is the main function that runs and scans all vehicles in front and behind the patrol vehicle diff --git a/cl_radar.lua b/cl_radar.lua index cf806d5..f943078 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -1422,21 +1422,21 @@ end RegisterNUICallback( "toggleRadarDisplay", function( data, cb ) -- Toggle the display state RADAR:ToggleDisplayState() - cb({}) + cb('ok') end ) -- Runs when the user presses the power button on the radar ui RegisterNUICallback( "togglePower", function( data, cb ) -- Toggle the radar's power RADAR:TogglePower() - cb({}) + cb('ok') end ) -- Runs when the user presses the ESC or RMB when the remote is open RegisterNUICallback( "closeRemote", function( data, cb ) -- Remove focus to the NUI side SetNuiFocus( false, false ) - cb({}) + cb('ok') end ) -- Runs when the user presses any of the antenna mode buttons on the remote @@ -1468,7 +1468,7 @@ RegisterNUICallback( "setAntennaMode", function( data, cb ) end ) end end - cb({}) + cb('ok') end ) -- Runs when the user presses either of the XMIT/HOLD buttons on the remote @@ -1493,7 +1493,7 @@ RegisterNUICallback( "toggleAntenna", function( data, cb ) end ) end end - cb({}) + cb('ok') end ) -- Runs when the user presses the menu button on the remote control @@ -1515,20 +1515,20 @@ RegisterNUICallback( "menu", function( data, cb ) -- Play the standard audio beep SendNUIMessage( { _type = "audio", name = "beep", vol = RADAR:GetSettingValue( "beep" ) } ) end - cb({}) + cb('ok') end ) -- Runs when the JavaScript side sends the UI data for saving RegisterNUICallback( "saveUiData", function( data, cb ) UTIL:Log( "Saving updated UI settings data." ) SetResourceKvp( "wk_wars2x_ui_data", json.encode( data ) ) - cb({}) + cb('ok') end ) -- Runs when the JavaScript side sends the quick start video has been watched RegisterNUICallback( "qsvWatched", function( data, cb ) SetResourceKvpInt( "wk_wars2x_new_user", 1 ) - cb({}) + cb('ok') end )