# Connect phones to a local game

The six starters using [examples/local-common](/sdk/examples/local-common/README.md) run from the unpacked SDK archive with Node 22 or newer; it already holds the packages they need (in a source checkout, run `npm ci` once at the SDK root). No vendor service, account, bundler or development watcher is needed. Each game has two phone seats. Keep `examples/local-common` beside a starter when adapting it; that folder contains the HTTP(S) server, QR lobby, lifecycle handlers and test helpers. Source imports point into this checkout; change them to the SDK's public package exports when integrating into another project.

The browser tests import the committed driver from `../../testing/index.mjs` so they run directly in the checkout. The SDK archive also ships the public `@controller-plus/sdk/testing` export; use that import in your own project. Install Chrome separately or set `CHROME_PATH` to its installed executable. No browser download occurs. Run tests one at a time, with one Chrome process for both phones. Godot tests use two SDK `Session` phones and stop Godot before any optional browser capture.

## Contents

- [Join from a phone](#join-from-a-phone)
- [Motion on iPhone](#motion-on-iphone)
- [Seats, output and shutdown](#seats-output-and-shutdown)
- [Scratch files and captures](#scratch-files-and-captures)

## Join from a phone

Start the game with `PUBLIC_ORIGIN` set to this computer's reachable Wi-Fi address, including the starter's port. Replace the address in each README with your own. Open that same address on the shared display, then scan its QR with the phones' cameras. You can also open **Open phone controller**. Phones and computer must be on the same network, and the firewall and Wi-Fi must allow connections between them.

The default `127.0.0.1` address is for testing on this computer. A separate phone cannot reach it. These six servers accept `PORT=49700` through `PORT=49799`; set `PUBLIC_ORIGIN` to the same port. An occupied port fails with an error so the QR never silently advertises a different address.

The [pygame starter](python.md) uses its own launcher and port range. [Rally Court](rally-court.md) has a loopback showcase host.

## Motion on iPhone

Touch controls work over ordinary LAN HTTP. Browser motion on iPhone needs a secure page and the player's permission. A phone opening another computer's HTTP address does not get the browser's localhost exception. [MDN explains the secure-context and user-gesture requirements](https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent/requestPermission_static).

Choose the setup that fits your game:

- Use HTTP and touch controls when you do not need motion. Gamepad sticks, a dragged wheel and a dragged flight stick remain usable.
- For motion, use a TLS certificate whose hostname or IP matches the advertised address and whose issuer the phone trusts. You can use a local development CA that you explicitly trust on your devices, or an existing trusted certificate for a hostname that resolves to this computer. Dismissing an untrusted-certificate warning is insufficient for reliable sensor access.

All six servers accept certificate and key paths. For example, from the SDK root:

```sh
TLS_CERT=/absolute/path/to/trusted-cert.pem \
TLS_KEY=/absolute/path/to/private-key.pem \
PUBLIC_ORIGIN=https://game-host.local:49701 \
node examples/wheel-race/server.mjs
```

Replace the paths and hostname. Keep keys outside the checkout. The server serves HTTPS and WSS on the same port. Trust is required on the phone and any native engine client; these examples do not disable certificate verification. The Godot and C starters use gamepad controls, so their normal HTTP setup needs no sensor permission.

A new game's `modes` list turns on its first mode in the browser. A player can choose another supported mode in **MENU**; updates within that game keep the choice. Tap **TAP FOR TILT on the wheel or flight instrument**, allow motion access, then move the phone gently and check that the control responds. Wait for this first reading before tapping **CENTER** (Wheel) or **LEVEL** (Flight) in your comfortable hold. If permission is denied or no reading arrives, turn tilt off in MENU and drag the wheel or stick. Flight's throttle lever remains touch controlled. LEVEL also recenters the touch flight stick.

## Seats, output and shutdown

`runtime.mjs` handles `join` with `resumed`, `leave`, `removed`, `invitation` and `feedback-result`. Games keep progress by player ID, clear input on departure, and delete it on permanent removal. A disconnected seat stays reserved for five minutes. The browser keeps its invitation and latest resume ticket in the same tab's session storage: refreshing the scrubbed `/join` page resumes automatically while those saved details and the seat remain valid. The driver's `reload()` performs a real page reload after releasing held touches. Reopening a current invitation in that tab also uses the saved ticket. A fresh tab without the ticket takes a new seat. If storage was cleared or the seat expired, scan the current QR; Disconnect clears the saved room and ticket. Server restarts create new rooms and reset progress.

Type `remove p1` in the server terminal, then Enter, to free that seat. Removal invalidates its resume ticket and rotates the player invitation. The lobby observes the invitation revision and reloads both its QR and link. Only the player invitation is public. Engine host credentials stay in process memory or the authorized child environment.

Each scoring/action cue has a distinct request ID, followed by bounded rumble. A `feedback-result` receipt reports browser handling; it does not prove audible sound or physical vibration. Muted or unsupported output does not prevent playing.

Ctrl+C or SIGTERM closes the simulation timer, engine child if present, host sockets and HTTP(S) server. Test teardown uses `finally`, including on failure. The native starter also releases input when its host transport closes. The Godot add-on neutralizes on connection loss and reconnects with its runtime credentials.

## Scratch files and captures

Set `EXAMPLES_TMP` and `CONTROLLER_PLUS_TEST_TMP` to a scratch directory on a disk with space. Godot stages its project and add-on there, so imports and `.godot` files never appear in the source checkout. Its staged project is removed after shutdown. On macOS, a sandbox permits file writes only inside that staged project; this game keeps progress in memory and never uses `user://`. Native builds stay in scratch for inspection and can be deleted afterward. No engine, browser or compiled binary is checked in.

Tests normally need no screenshot tool. For optional evidence, set `EXAMPLE_CAPTURE_DIR` and `RENDER_PAGE` to the installed `render-page` CLI. Tests save both phone PNGs, close their browser, then render the game with that CLI. Godot is stopped before this pass; its image is a scene SVG produced by the actual GDScript simulation, rather than a GPU framebuffer. Engine phone PNGs are a separate rendering pass after their Session tests.

Headless checks cover input routing, independent seats, feedback messages, resume, removal and cleanup. Real Wi-Fi, camera scanning, iPhone trust and motion prompts, physical tilt, speakers, vibration motors and physical gamepads still need device checks.

[All start pages](index.md) · [Search developer docs](search.md) · [Ship a game](shipping.md)
