mty serve¶
Built-in dev server for a Mighty web-game package. v0.23 Track C.
mty serve reads mighty.toml, builds the package with
--target wasm32-web, then serves web/index.html, every static
asset under web/, and the freshly-built wasm module on
127.0.0.1:<port>. With --watch, it file-watches src/ and
rebuilds + pushes a reload to the page over a websocket on every
change.
Synopsis¶
Flags¶
| Flag | Description |
|---|---|
--port <N> |
TCP port to bind on 127.0.0.1. Default 8000. |
--watch |
Watch src/ and rebuild on every change. Pushes a reload to connected pages over ws://127.0.0.1:<port>/_reload. |
--manifest-dir <DIR> |
Package root (default: current directory). |
Routes¶
| Method | Path | Behavior |
|---|---|---|
| GET | / |
Serves web/index.html with Content-Type: text/html; charset=utf-8. |
| GET | /<asset> |
Serves web/<asset> with a mime type derived from the extension (.js, .css, .json, .svg, .png, .jpg, .txt, .md, etc.). |
| GET | /main.wasm |
Serves the freshly-built wasm artefact (typically target/main.wasm) with Content-Type: application/wasm and Cache-Control: no-cache. |
| GET | /_reload |
Websocket upgrade endpoint. The server pushes the string reload after every successful --watch rebuild; the page template's dom-shim.js calls location.reload() on receipt. |
| any | /<path with ..> |
403 forbidden (defence against path-escape attempts). |
| any | unknown path | 404 not found. |
What it expects¶
The current working directory (or --manifest-dir) must contain:
The fastest way to get one is mty new --template web-game <name>.
Build configuration¶
mty serve always builds with:
| Setting | Value |
|---|---|
| target | wasm32-web |
| mode | debug (fast rebuild + smaller binary on disk) |
| output | <pkg>/target/main.wasm |
| wasi-preview | default (p2) |
| component | yes (the same envelope mty build --target wasm32-web emits) |
The browser-side dom-shim that ships with the web-game template
extracts the inner core module from the component envelope at load
time — see the comments in templates/web-game/web/dom-shim.js.
--watch reload protocol¶
- The page opens
ws://127.0.0.1:<port>/_reloadon first load. mty servecompletes the RFC 6455 opening handshake (Sec-WebSocket-Acceptisbase64(sha1(key + magic))).- On each successful rebuild, the server writes a single
server-to-client text frame containing
reload. - The page's
dom-shim.jscallslocation.reload()on receipt. - If the websocket closes (e.g. the server stops), the shim retries every 1s.
Examples¶
# Scaffold a fresh web-game and start the dev loop.
mty new --template web-game asteroids
cd asteroids
mty serve --watch
# Pick a non-default port.
mty serve --port 9000
# Run from outside the package directory.
mty serve --manifest-dir ./asteroids
Exit codes¶
| Code | Meaning |
|---|---|
0 |
Clean shutdown (Ctrl-C). |
1 |
Frontend build error in the user's package (diagnostics printed). |
2 |
Backend build error, missing mighty.toml, missing web/, or bind failure. |