Improved the documentation

This commit is contained in:
Philip Gladstone 2022-04-30 19:48:31 -04:00
parent ec604d9c70
commit e0748a4b9e
1 changed files with 25 additions and 3 deletions

View File

@ -258,7 +258,7 @@ httpd.dynamic(httpd.PUT, "/foo", put_foo)
## httpd.websocket() ## httpd.websocket()
Registers a websocket route handler. Registers a websocket route handler. This is optional, and must be selected explicitly in the configuration.
#### Syntax #### Syntax
```lua ```lua
@ -285,13 +285,35 @@ nil
#### Example #### Example
```lua ```lua
httpd.start({ webroot = "web" }) httpd.start({
webroot = "<static file prefix>",
max_handlers = 20,
auto_index = httpd.INDEX_NONE + httpd.INDEX_ROOT + httpd.INDEX_ALL,
})
function echo_ws(req, ws) function echo_ws(req, ws)
ws:on('text', function(data) ws:text(data) end) ws:on('text', function(data) print(data) ws:text(data) end)
end end
httpd.websocket("/echo", echo_ws) httpd.websocket("/echo", echo_ws)
function tick(ws, n)
ws:text("tick: " .. n)
n = n + 1
if n < 6 then
tmr.create():alarm(1000, tmr.ALARM_SINGLE, function ()
tick(ws ,n)
end)
else
ws:close()
end
end
function heartbeat(req, ws)
tick(ws, 0)
end
httpd.websocket("/beat", heartbeat)
``` ```
## httpd.unregister() ## httpd.unregister()