Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Codea Web Server (Also on WebRepo)

edited November 15 in Code Sharing Posts: 255

Hi all,

After some recent comments about having a separate program running alongside Codea (on iPad) for debugging purposes and things like that I thought I’d make a start.

I hope to integrate some sort of OSC support within the webpage at some point to allow bi-directional communication between the browser and the running Codea instance but this is certainly a start :smile:

So here it is, a very simple web server that serves a web page to a browser running alongside Codea:

-- Basic Web Server

local socket = require("socket")

— This is the served content
local body = [[
Hello Codea World!
]]

local function webserver_update()
    -- wait for a connection from any client
    local client = server:accept()
    if client then
        -- Make sure we don't take too long to respond to the client
        client:settimeout(1 / 60)

        -- Receive the request
        local line, err = client:receive()

        -- If no error then send our response
        if not err then
            client:send("HTTP/1.0 200 Success \r\n\r\n" .. body)
        end

        -- Close the client object
        client:close()
    end
end

local function webserver_loop()
    -- This is beautiful if I do say so myself...
    -- This will 'always' fail (as long as the silly url doesn't exist)
    -- so the fail callback will always be called outside of our main update loop.
    http.request("http://imnotarealurl.lua", function() end, function()
        webserver_update()
        webserver_loop() — Loop again!
    end)
end

function setup()
    server = assert(socket.bind("127.0.0.1", 8888))
    server:settimeout(0.001) -- minimise waiting
    local ip, port = server:getsockname()
    print("Connect to http://" .. ip .. ":" .. port)

    -- Kick off the async web server loop
    webserver_loop()
end

function draw()
    background(128)
end

I’m also incredibly pleased with how well the async update loop ‘hack’ panned out. It runs totally asynchronous to the main project loop!

Hopefully this is useful to some of you.

Cheers,
Steppers

UPDATE 15-Nov-2021:
Version 1.1.0 is also available on WebRepo

Tagged:
«1

Comments

  • Posts: 255

    An improved library version has also been added to WebRepo.

    It now serves files from an asset source folder, handles missing files and only requires a single line to use!

    WebServer(asset.documents.SiteSource)

  • Posts: 2,496
    @Steppers - this looks amazing. Need to play with this a little to understand just what I can use it for.

    On the bi-directional idea that has already been implemented in Codea with Aircode in that you can link to an editor in a browser on a PC/Mac. I still use it occasionally but it is a bit prone to crashing as it is live linked coding.

    I think a better idea would be a module for an external editor where updates are not immediate but posted when 'complete'. You could potentially use one of many editors like Notepad+, Visual Code etc etc.
  • Posts: 255

    @Bri_G Small debug server now too! (also on WebRepo)

    It only supports reading of variables at the moment but I expect I can probably extend it some more to add editing :smile:

  • Posts: 1,547

    Downloaded the web repo version, but the server doesn’t load. I get the message “Safari cannot open the page because it could not contact to the server”.

  • Posts: 255

    @UberGoober Are you on iPad running safari & Codea side-by-side?

  • Posts: 1,547

    Yes

  • edited November 10 Posts: 255

    What URL are you using in Safari?

  • Posts: 1,547

    http://localhost/ is what pops up automatically

  • Posts: 1,275

    try localhost:8888, that looks like the URL stepper's serving.

  • Posts: 1,275

    This is interesting indeed. I am going to have to try it and see what I can do with it. Quite nice.

  • Posts: 1,275

    The new version seems to want localhost:80

  • Posts: 255

    @RonJeffries It should be using port 80 by default as that’s what most http web servers use.

    The url localhost on it’s own is working just fine for me.

  • edited November 10 Posts: 1,275

    @Steppers I don't understand what the do...end blocks are about. That seems to be a trick that I don't know.

    It'd be good to see an example of setting up the resources, but I can probably fumble my way through ...

    I've not used socket. It seems that client:receive() is adding a blank at the end of the input? I can't find any docs that suggest that happens. I'd appreciate some hints as I play with this. Thanks!

    Very interesting!

  • Posts: 1,275

    yes working for me as well, I'm now trying to work out how to use the GET. Do I need to encode with %20 etc?

  • Posts: 255

    @RonJeffries the do … end blocks just create a scope for the local variable so it doesn’t stick around afterwards.

    As for the resources, local server = WebServer(asset.documents["web-server"], 80) will start a web server that serves html files from the web-server folder inside Codea’s folder in the files app. By default if you pass it a blank path, say http://localhost/ rather than http://localhost/test.html then it will serve an index.html file if one’s available.

    As for socket peculiarities, I’m still figuring it out myself but using http://www.promixis.com/lua/luasocket/reference.html for reference.

    You may need to encode spaces as %20 at the moment unfortunately. I think luasocket has a URL escape function somewhere that could be used.

    This is still a WIP on my part though so improvements will come.

  • Posts: 1,275

    OK, I'm not reading the thing properly. More study required. It's cool, for sure.

  • Posts: 1,547

    localhost:8888 still does nothing. Also see attached image.

  • Posts: 255

    @UberGoober Give this one a try, most of the socket based backend has been rewritten and it seems much more reliable now.

    I also think this is progress (see screenshot) :smiley:

  • Posts: 1,547

    See screenshot.

    Safari truncates the “:80” no matter what I do.

    Btw, if you put the urls in their own print() statement they can be copied directly from the output panel, letting people then paste them right into their browser.

    (The copying is a hidden feature I only learned about in the last few months, and I’ve been coming here for years.)

  • Posts: 1,275

    seems quite cool. how are you setting up the actual index.html page? is there some easy way to view / create / edit the assets? (this may be a codea question ... something everyone knows but me.)

  • Posts: 1,275

    it's working for me as released, @UberGoober ... not sure what you're doing differently.

  • Posts: 1,547

    I mean, I’m not doing anything, I’m just running the program, same as you. Something about my setup might be borked, or it might not, but all I do is hit play.

  • Posts: 1,275

    i get codea and safari side by side, hit play, then type localhost in safari, making sure there's no extra stuff, and it runs.

  • Posts: 255
    @UberGoober Are you using that updated one I attached above?
  • Posts: 1,547
    Yes
  • Posts: 1,547
    @RonJeffries that’s what I do to the letter.
  • dave1707dave1707 Mod
    edited November 10 Posts: 9,725

    I’m trying this and I’m not getting anywhere either. I load Codea, then Web Server. I tap the 3 dots at the top of the screen to get split screen. I load Safari. I hit play on the Web Server and then type localhost in Safari and press enter. It sits there for awhile then displays Safari can’t open the page .. etc.

    PS. I’m using the last Web Server.zip

  • Posts: 255
    @dave1707 Thanks, I'll try that and see what's going on.
  • Posts: 2,496

    @dave1707 - I got it to work just as you tried but after loading both apps I clicked on Safari, entered localhost and then ran Codea Webserver. It produced a small page in Safari. Seems like you have to set up Safari to receive first.

  • dave1707dave1707 Mod
    edited November 10 Posts: 9,725

    @Bri_G if I load both webserver and Safari, and type localhost and press enter on Safari, I get the Safari can’t open message immediately, without running the webserver. If I run the webserver first and then Safari, Safari sits there for awhile before showing the Safari can’t open message.

    Ps. What’s supposed to show up in Safari and are you just keying localhost in Safari and not the http://localhost

  • Posts: 2,496

    @dave1707 - typed just local host, this time needed to type it again. Picture below(if it loads) to show result.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G All I get is the Safari can’t open message. I tried it on different iPads, but same thing on them.

  • Posts: 1,275

    have you moved the app to the codea folder, not running it direct from files?

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries Yes. I remember problems I had with another project because I was running it from files. I long press on the zip file, select share, select Codea. Codea opens and asks if I want to load the project. I load it.

  • dave1707dave1707 Mod
    Posts: 9,725

    I tried one of my projects the uses socket to communicate between 2 iPads to transfer code between the 2 of them. The link works OK and I transferred projects from one iPad to the other.

  • Posts: 1,275

    yes. I long pressed, selected move, and moved it to Codea folder, so it shows up as a real project. could be the issue.

  • dave1707dave1707 Mod
    Posts: 9,725

    The Web Server shows up in my project list as a normal project. I’ve tried everything I could think of, but I still end up with the Safari error message. I thought maybe my router was blocking it, but I was able to use my one project that connects 2 iPads using the router. So I don’t think that’s a problem.

  • Posts: 255

    I probably need to be taking a closer look at the http spec really. I must be missing something important.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G That picture you show is the info you get. Where is that coming from.
    @RonJeffries Are you getting the same thing that bri-g shows above in his picture.

  • Posts: 255

    @dave1707 The html source provided in the project zip above is in the project bundle itself. That’s what they’re seeing in the browser.

  • dave1707dave1707 Mod
    edited November 10 Posts: 9,725

    @Steppers I forgot about looking in assets. I see the info there. Still can’t get it to run.

    I put a print statement in every function in the webserver tab. When I run the code, the only print I see is the one in the init function. I see that as soon as I run the webserver code. It’s like the Safari localhost does nothing because nothing else prints.

    PS. I forgot the prints in the webserver_loop. It prints loop, and empty func, but not request.

        local function webserver_loop()
            print("loop")
            http.request("http://imnotarealurl.lua", function() print("empty func") end, function()
                print("request")
                self:_update()
                webserver_loop()
            end)
        end
    
  • edited November 10 Posts: 1,275

    essentially that pic, yes. zoomed differently.

  • Posts: 1,275

    is there some way to view the assets? is there some way to open the unzipped project as files, to see what's in it?

    thanks!

  • dave1707dave1707 Mod
    edited November 11 Posts: 9,725

    @RonJeffries I have an app called iZip (free) that I tried on the webserver.zip file. It extracted 7 files. I was able to view all 7 files right in iZip.

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries @Steppers I don’t see the asset key that Ron shows in his image above. I see the web server running, then I see the connect at: stuff.

  • Posts: 1,275

    i may have izip, will try it. no way in codea or native ios?

  • Posts: 255

    @dave1707 Your problem is with webserver_loop I think.

    If you change it to this does it help?

    local function webserver_loop()
        tween(0.0001, {}, {}, nil, function()
            self:_update()
            webserver_loop() -- Loop again
        end)
    end
    

    If it is that then I suspect it may also be your problem too @UberGoober
    Without the loop functioning nothing will work.

  • dave1707dave1707 Mod
    Posts: 9,725

    @Steppers That was it. After the change it worked just fine.

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries As far as I know, Codea or iOS isn’t going to unzip the files and let you view them.

  • Posts: 1,275

    seems like, yeah,

Sign In or Register to comment.