Page

A page server your program runs: answer its routes, talk to every open page, and stop it when you are done.

PageService:ServeAsync gives one, a child of PageService. Its Url carries a key: Handle routes and the live channel refuse a request without it, and a request naming another host or sent from another site's page is refused. A Page keeps the run going while it serves, and Destroy() stops it. Only a page can close its own channel.

ShotInstancenot creatableWeb

PageService:ServeAsync(options) gives one.

Example

Answer a routeShotScript
-- A page server's own route: Handle answers it, the request's Query holds the address's parameters.
use HttpService
use PageService
local page, why = PageService:ServeAsync()
if not page then
	error("couldn't serve: " .. tostring(why))
end
page:Handle("/api/hello", function(request : PageRequest) : string
	return "hello " .. (request.Query.name or "you") .. " (" .. request.Method .. ")"
end)
print("Port is in Url: " .. tostring(string.find(page.Url, ":" .. page.Port .. "/", 1, true) ~= nil))

-- a route needs the server's key, which Url carries as ?k=...
local hello = (string.gsub(page.Url, "/%?", "/api/hello?", 1))
print(tostring((HttpService:GetTextAsync(hello .. "&name=Notes"))))
local base = string.match(page.Url, "^[^?]*") or page.Url
local answer = HttpService:FetchAsync({ Url = base .. "api/hello" })
print("without the key: " .. tostring(if answer then answer.StatusCode else nil))
print("open pages: " .. #page:Clients())
page:Destroy()
What it printedOutput
Port is in Url: true
hello Notes (GET)
without the key: 401
open pages: 0

Properties

NameTypeDefaultDescription
Portnumber0
read-only
The port the server took: the first free one from the Port asked for.
Urlstringread-onlyThe page's address with its key (?k=...); Handle routes refuse a request without the key, while files need none.

Methods

NameArgumentsReturnsDescription
Clients(){ PageClient }The channels of every page open now, as a new table; each client has Id, Send and Closed.
Handle(route: string, callback: (request: PageRequest) -> (PageResponse | string))()Answers a route of the tool's own, GET or POST, with the callback's string or { Status, Body, Type }; an error answers 500.
Send(text: string)()Sends text to every page open now; a client's own Send reaches that one page only.

Events

NamePassesDescription
Connected(PageClient)Fires with the page's client each time an open page connects its live channel through live.js.
Message(PageClient, string)Fires with the client and the text each time a page sends one with ShotLive.send.
From ShotInstance 33