tinyweb in other languages
tinyweb draws, plays and answers on a web page, and every language a Pyking program compiles to has an implementation of it — the file Pyking copies beside the program. Each is also a library a program written in that language can use with no Pyking at all: the same functions, under the language’s own names and types.
| Language | Files | Build |
|---|---|---|
| Python | tinyweb.py | python3 example.py |
| JavaScript | tinyweb.js | node example.js |
| OCaml | tinyweb.ml, tinyweb.mli | ocamlopt -I +unix unix.cmxa tinyweb.mli tinyweb.ml example.ml -o example |
| C | tinyweb.c, tinyweb.h | cc -o example example.c tinyweb.c |
| Java | Tinyweb.java | javac Tinyweb.java Example.java |
A program calls web_init(port) first, which serves its page and prints
its address; port 0 picks a free one. Everything the program draws or
adds to the page is sent there as a command, and what the reader does
comes back as an event that calls the function the program handed over.
Once the program’s own code has finished, it goes on serving the page
until it is stopped. A page that is opened late, or reloaded, is sent
what is on it first.
What the page and the program say to each other
For anyone writing an implementation of their own: the page the program
serves is tinyweb_page.html and tinyweb_page.js, which every
implementation carries, and the program answers
| request | answer |
|---|---|
GET / | the page |
GET /tinyweb_page.js | the page’s script |
GET /commands | server-sent events, each a JSON array of commands |
POST /events | one event, as JSON |
GET /<file> | a file beside where the program runs, such as a sample |
A command is an object such as {"op": "circle", "c": 3, "x": 100, "y": 50, "radius": 30, "color": "#ff0000"}, and an event one such as
{"ev": "click", "id": 4, "t": 1.25}, where t is the page’s clock.
After each event the program sends {"op": "event_done"}, and after a
frame’s callbacks {"op": "frame_done"}, before which the page asks for
no next frame.