Hi
I have an app which will have a number of interface screens. I plan for this to be seperate lua files. Each of these interface screens will need to call a generic network http url with various requests and receive a response.
I really want to keep all of the network interface stuff away from the interface files. So, I would like a structure where I have a number of files all referencing a general network file which I have called httpwork.
I have coded the first interface file and this generic network file.
However, I am stuck as to how to get the lua file that is calling the httpwork function to receive the request response.
For example: -
my first file is called home.lua. It has some code like this: -
local http = require("httpwork") http.MakeNetworkCall(Param1)
This calls a function in my httpwork file called MakeNetworkCall which is like this: -
local network = require("network") local url = require("socket.url") local resp = {} local function networkListener( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE: " .. event.response ) end end local function MakeNetworkCall(param1) network.request( "somewebsite.html?param="..param1, "GET", networkListener ) end resp.MakeNetworkCall = MakeNetworkCall return resp
So far so good.
However, I cannot work out how to get the response back from this file to my original calling file. I know that the response is picked up by the listener, but the listener is in this file!
I tried referencing the return of the function, but of course the network.request call if asynchronous so returns immediately with nothing! Then the listener kicks in, but that is a little late for the return.
I am probably getting too bogged down in code in different files, but I really want to try and separate my code layers if I can and keeping all this network interface stuff hidden from the front end screen code.
Has anyone got any tips on how I can best achieve this? Happy to start again if I have to!