As the title says... Chat on...

User avatar
By Mike
#8892 Hi, picstart,

you've been aksing for the "config.pages[request.path]" -List.

This is how it is comming to live:

First you have to create the List containing the functions handling the Page-Requests sent to the Web server:
Code: Select allpages = {}

pages["/led.gif"] = function(request)
    file.open("led.gif")
     X = file.read()
     file.close()
     tmr.wdclr()
     return X , "image/gif"
    end
....


Now you load the Servers code :
Code: Select alldofile("httpServer.lua")

This defines a Function "startWeb()" with the Argument "cfg"

When invoking the Server you have to pass this List as an Argument.
Code: Select allw = startWeb({pages = pages})

This is how your pages-List becomes a Item of the List "cfg" passed as Parameter to startWeb()
(The Example is somewhat irritating by using the same name "pages" for the global List created by the user and the local copy used in the body of startWeb()-Function)
The part of the Webserver-script creates a local copy of the cfg-list named "config":

Code: Select all... 
function startWeb(cfg)
  -- define member variables
  local config = cfg
...

Now the Server-script knows a List named "config" containing a item "pages" (which was passed as parameter to the function "startWeb(cfg)". This pages-item is an other list containing the Callback-functions handling the page-requests.

So config.pages[request.path] points to a function (hopefully) defined by you as a item af the pages-list to handle the request to "path" comming from the web- client.

The "request.path" is extracted from the Browsers Request by the Weberserver-script doing some (not so easy to understand) regexp-magic. (Discussing this would lead too far here).

Remember that all above Examples show only relevant parts of the code you'll need.
Some fine day i will clean up my code and publish an complete example for a working Server with pages containing images and cgi-funtions (read GPIO, read ADC, set PWM-Value, "beep"), so stay tuned..
User avatar
By picstart
#8926 Thanks for the information
Issue
I understand that the server sends the web page to the browser and if the web page calls for several resources ( specifically <img src *gif ) then several GET/*gif
are requested. These requests are asynchronous so they will often arrive before the web page is fully served.
If the web page is in a lua file and the *gif files are lua files then before the web page is fully transfer a on: receive event will occur and the socket number will change from the web page socket number to the new socket number assigned to the GET/*gif request.
Web pages and especially *gif files will not fit into a single packet ( 1460 bytes) so they will take several sends with call backs to initiate the next packet being sent

Work around for single file at a time lua file system
Place web page into lua inline code ( avoids limitation of single lua file open at at time)

I don't understand how the issue of the multiple socket numbers is handled...the multiple on receive events must be handled asap. If several *gif files are to be displayed then there are several sockets opened by the browser as the lua server sends the *.gif's the browser can time out the other pending requests. A work around would be to serve the web page up to and including each <img src line and wait for the browser GET/*gif request and then send the gif from the lua file system but then the GET/*gif changed the socked number so the rest of the web page can't be sent.
Observation
Many lua example scripts lack comments ( perhaps this is a language issue) but explanation type comments would be nice.
User avatar
By Mike
#9177 Hi Picstart,

due to the (heap) memory-restrictions of the ESP8266 i din't even try to create a Webpage with more than one <img ..>-Tag. Also the HTML-Page itself uses less than 1460Bytes, so ist ist delivered in one single Ethernet-Frame, giving the Browser no chance for asyncronous overlapping requests.
As a (dirty) workaround it might be possible to use Client-side scripting (eg. embedded jscript in the HTML-Page) to let the Browser load the images one file after the other. You might modify the LUA-Server-code to enable client-side caching of the image-files so that the delay for preloading the images occures only the first time you connect to the ESP.
But -as i said before- this are dirty tricks which won't work in every environment. I think doing so should be avoided if possible.

Mike