ident
All repositories: gitoria
4.0 KB
\* hl:http1 — the native WebSocket server. Subclass it (`inheritNativeWebSocketServer`) and answer the declared events below; eachconnected peer arrives as a WsClient (send/ping/close). AUTO-LISTEN: thefile root IS the constructor and ends with listen(), so`new NativeWebSocketServer(port = 8888)` serves immediately. *\/* Mission 064 (decision D9): the h1 plugin performs the RFC 6455 Upgrade/101handshake and framing (shared core plugins/http/ws_common.zig); thiswrapper maps the native event stream onto on/emit, mirroringNativeHttpServer. A SUBCLASS does not inherit this run body and callslisten() from its own root. */\* A peer completed the upgrade and is connected. *\emit connect(WsClient client \\ the new peer — send/ping/close)\* A complete text (or binary) message arrived from a peer. *\emit message(WsClient client \\ who sent itString data \\ the message payload)\* The peer answered a client.ping(). *\emit pong(WsClient client \\ who answered)\* A connection ended — the peer is already unregistered. *\emit close(WsClient client \\ who left)\* OPTIONAL: plain HTTP answered on the same port, exactly asNativeHttpServer's request. *\emit request(Hybrid req \\ method, path, headers, body, bytes, params, respond)Number port = 8080 \\ the port listen() bindsString host = "0.0.0.0" \\ the interface to bindNumber threads = 4 \\ I/O worker threadsBoolean listening = false \\ true after a successful bindHybrid clients = {} \\ live WsClients by connection idNumber #srcId = 0Number #wsSrcId = 0\* Bind the configured port and start the WebSocket engine (idempotent). *\listen() {if (listening == true) {return null}srv = __native("http1.create_server", port, host, "", "", threads)if (srv == null) {// A REFUSAL, NOT PROSE (mission 286) — same reason as NativeHttpServer's.hlError("hl:http1: listen() failed: could not bind port " + port + " on " + host + " — the port is already taken. The line just above, 'bind() failed on port " + port + "', names the process holding it: stop that process, or start this app on another port (--port N, or HL_PORT=N)")}wsev = __native("http1.ws_events")listening = true#srcId = __native("eventloop.register", srv, "request")#wsSrcId = __native("eventloop.register", wsev, "wsEvent")}\* One text frame to EVERY connected peer; answers how many received it. *\broadcast(String text \\ the frame to send) {return __native("http1.ws_broadcast", text)}\* Stop this server: both event sources retired, workers joined, listenerclosed — the port is free again. Answers false when not listening. *\close() {if (listening == false) {return false}listening = false__native("eventloop.unregister", #srcId)__native("eventloop.unregister", #wsSrcId)return __native("http1.close", port)}// Raw native events ({ kind, id, data, binary, code }) re-emitted as the// documented surface — emit is the universal primitive.on wsEvent(ev) {key = "c" + ev.idif (ev.kind == "connect") {client = new WsClient(ev.id, ev.cookie, ev.host != '' ? ev.host : null, headerHash(ev.headers)) // the upgrade's cookie, host and headers ride with the connection (creator, 2026-09-12; tickets #74, #105)clients[key] = clientemit connect(client)}if (ev.kind == "message") {client = clients[key]if (client != null) {emit message(client, ev.data)}}if (ev.kind == "pong") {client = clients[key]if (client != null) {emit pong(client)}}if (ev.kind == "close") {client = clients[key]clients[key] = nullif (client != null) {emit close(client)}}}// The upgrade's headers arrive as `name: value` lines (names already lowercase);// a connection is handed them as a hash (ticket #105).headerHash(lines) {let nl = ""let out = {}if (lines == null || lines == '') { return out }for (line of lines.split(nl)) {let colon = line.indexOf(': ')if (colon > 0) { out[line.slice(0, colon)] = line.slice(colon + 2) }}return out}// Run body: bind the configured port (idempotent, like NativeHttpServer).listen()
Branches
- mainmain branch
Latest commits
- a3a7d21aident: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy); session-writing lambdas take &sessionmre
- 98226b41antcolony#40: mission references point to the moved missionsmre
- ff805b9aantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
- 51a7bcdfident: Hybriel master 73267707 (#122); /code uses the new page() signature; pending address passed as parameter; once-checksmre
- 836f644fident#24: installable app (manifest, service worker, data-free offline /start), own iconmre
- 8bebbbf2deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- cc063ea2deploy.sh: never send .git or .gitignore to Byrodinmre
- 81b15b7bState of 2026-09-27, before the move to gitoriamre