I thought I'd share a bit of code I've written for my next version of my app. It queues http requests one after the other and cycles through the list until all requests have been made. Each time a request is made and the data received it's moved from the list and the others are moved down. If one fails to get a valid response, and instead gets a -1 it's moved to the end of the queue to try again shortly.
It's also been set up so that the list doesn't have the same call to the server more than once. In the do loop I've added a small delay between each send so that he server isn't being called too often and too quickly.
It allows up to a 100 calls, but you can change that number.
This is for server calls where you want to receive data but you don't want the user waiting. It can all happen in the background. There will obviously be calls where you have to wait for the data before you can proceed. You can do that with the below code by putting the RunThroughServerCallQueue() in a loop like 'repeat until' i.e. repeat until that specific call isn't in the list.
type typeServerCall
id as integer
failedCount as integer
parameters$ as string
response$ as string
script$ as string
sentTime# as float
endtype
global http as integer : http = CreateHTTPConnection()
global httpResponse$ as string
global noOfServerCallsQueued as integer
global serverCallQueue as typeServerCall[100]
SetHTTPHost(http, "www.myappdomain.com", 1)
GetServerVariables()
GetLockUpdates(3)
do
if (timer() - timeLastCalledServer# > 0.5)
RunThroughServerCallQueue()
timeLastCalledServer# = timer()
endif
Sync()
loop
// THIS WILL ADD AN SERVER CALL TO THE FRONT OF THE LIST. FOR THOSE IMPORTANT ONES THAT NEED DEALING WITH QUICKLY.
function AddEntryToFirstPlaceInServerCallQueue(id as integer, script$ as string, parameters$ as string)
for i = 1 to noOfServerCallsQueued
if (serverCallQueue[i].id = id and serverCallQueue[i].script$ = script$) then exitfunction
next
inc noOfServerCallsQueued
for i = noOfServerCallsQueued to 1 step -1
serverCallQueue[i].id = serverCallQueue[i - 1].id
serverCallQueue[i].script$ = serverCallQueue[i - 1].script$
serverCallQueue[i].parameters$ = serverCallQueue[i - 1].parameters$
next
serverCallQueue[1].id = id
serverCallQueue[1].script$ = script$
serverCallQueue[1].parameters$ = parameters$
endfunction
// THIS WILL ADD THE SERVER CALL TO THE END OF THE LIST. IF THE SAME SERVER CALL IS ALREADY IN THE LIST IT WILL EXIT FUNCTION.
function AddEntryToServerCallQueue(id as integer, script$ as string, parameters$ as string)
for i = 1 to noOfServerCallsQueued
if (serverCallQueue[i].id = id and serverCallQueue[i].script$ = script$) then exitfunction
next
inc noOfServerCallsQueued
ClearServerCallQueueEntry(noOfServerCallsQueued)
serverCallQueue[noOfServerCallsQueued].id = id
serverCallQueue[noOfServerCallsQueued].script$ = script$
serverCallQueue[noOfServerCallsQueued].parameters$ = parameters$
endfunction
// THIS WILL CLEAR THE DATA IN A PARTICULAR SLOT.
function ClearServerCallQueueEntry(index as integer)
serverCallQueue[index].id = 0
serverCallQueue[index].script$ = ""
serverCallQueue[index].sentTime# = 0
serverCallQueue[index].parameters$ = ""
serverCallQueue[index].failedCount = 0
endfunction
// FOR EACH TYPE OF SERVER CALL YOU CREATE A FUNCTION THAT ADDS IT TO THE END OR IN THE FIRST SLOT AND PASS THROUGH THE SCRIPT NAME AND URL PARAMETERS.
function GetLockUpdates(lockNo as integer)
AddEntryToServerCallQueue(lockNo, "getlockupdates.php", "")
//AddEntryToServerCallQueue(lockNo, "getlockupdates.php", "userid=" + userID$ + "&lockID=" + str(locks[lockNo].lockID) + "&platform=" + GetDeviceBaseName())
endfunction
function GetServerVariables()
AddEntryToFirstPlaceInServerCallQueue(0, "getservervariables.php", "")
endfunction
// THIS MOVES A SERVER CALL TO THE END OF THE LIST.
function MoveEntryToBackOfServerCallQueue(index as integer)
ClearServerCallQueueEntry(noOfServerCallsQueued)
serverCallQueue[noOfServerCallsQueued].id = serverCallQueue[index].id
serverCallQueue[noOfServerCallsQueued].script$ = serverCallQueue[index].script$
serverCallQueue[noOfServerCallsQueued].parameters$ = serverCallQueue[index].parameters$
endfunction
// THIS REMOVES THE ENTRY FROM THE LIST. NORMALLY HAPPENS WHEN THE SERVER CALL WAS SUCCESSFUL.
function RemoveEntryFromServerCallQueue(index as integer)
for i = index to noOfServerCallsQueued
ClearServerCallQueueEntry(i - 1)
serverCallQueue[i - 1].id = serverCallQueue[i].id
serverCallQueue[i - 1].script$ = serverCallQueue[i].script$
serverCallQueue[i - 1].parameters$ = serverCallQueue[i].parameters$
next
ClearServerCallQueueEntry(noOfServerCallsQueued)
endfunction
// THIS IS THE FUNCTION THAT RUNS THROUGH THE LIST OF SERVER CALLS. THIS IS CALLED IN THE MAIN GAME/APP LOOP.
// ONCE A VALID RESPONSE IS RECEIVED IT CHECKS TO SEE WHAT THE NAME OF THE SCRIPT WAS THAT WAS CALLED.
// IT THEN CALLS A PARTICULAR RECEIVED* FUNCTION TO WORK WITH THE DATA RECEIVED.
// YOU CAN CALL RECEIVED* FUNCTIONS AND PASS THROUGH PARAMETERS TO THEM. LOOK AT ReceivedLockUpdates(lockNo) IN THIS FUNCTION.
function RunThroughServerCallQueue()
if (serverCallQueue[1].script$ <> "")
if (GetInternetState() = 1)
if (serverCallQueue[1].sentTime# = 0)
SendHTTPRequestASync(http, serverCallQueue[1].script$, serverCallQueue[1].parameters$)
serverCallQueue[1].sentTime# = timer()
elseif (serverCallQueue[1].sentTime# > 0)
responseCode = GetHTTPResponseReady(http)
if (responseCode = -1)
if (serverCallQueue[1].failedCount < 4)
inc serverCallQueue[1].failedCount
serverCallQueue[1].sentTime# = 0
else
RemoveEntryFromServerCallQueue(1)
MoveEntryToBackOfServerCallQueue(0)
endif
elseif (responseCode > 0)
httpResponse$ = GetHTTPResponse(http)
if (serverCallQueue[1].script$ = "getlockupdates.php")
lockNo = serverCallQueue[1].id
ReceivedLockUpdates(lockNo)
endif
if (serverCallQueue[1].script$ = "getservervariables.php") then ReceivedServerVariables()
RemoveEntryFromServerCallQueue(1)
dec noOfServerCallsQueued
endif
endif
else
// OFFLINE
endif
endif
endfunction
// THESE ARE THE FUNCTIONS YOU'D CREATE TO WORK WITH THE DATA JUST RECEIVED FROM THE LAST SERVER CALL.
function ReceivedLockUpdates(lockNo)
// DO SOMETHING WITH THE HTTPRESPONSE$ DATA
endfunction
function ReceivedServerVariables()
// DO SOMETHING WITH THE HTTPRESPONSE$ DATA
endfunction
CloseHTTPConnection(http)
DeleteHTTPConnection(http)
EDIT: Just updated code as I forgot to add httpResponse$ as a global variable.