gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mremain/plugins/http1/native_web_socket_server.hl

4.0 KB

  1. \* hl:http1 — the native WebSocket server. Subclass it (`inherit
  2. NativeWebSocketServer`) and answer the declared events below; each
  3. connected peer arrives as a WsClient (send/ping/close). AUTO-LISTEN: the
  4. file root IS the constructor and ends with listen(), so
  5. `new NativeWebSocketServer(port = 8888)` serves immediately. *\
  6. /* Mission 064 (decision D9): the h1 plugin performs the RFC 6455 Upgrade/101
  7. handshake and framing (shared core plugins/http/ws_common.zig); this
  8. wrapper maps the native event stream onto on/emit, mirroring
  9. NativeHttpServer. A SUBCLASS does not inherit this run body and calls
  10. listen() from its own root. */
  11. \* A peer completed the upgrade and is connected. *\
  12. emit connect(
  13. WsClient client \\ the new peer — send/ping/close
  14. )
  15. \* A complete text (or binary) message arrived from a peer. *\
  16. emit message(
  17. WsClient client \\ who sent it
  18. String data \\ the message payload
  19. )
  20. \* The peer answered a client.ping(). *\
  21. emit pong(
  22. WsClient client \\ who answered
  23. )
  24. \* A connection ended — the peer is already unregistered. *\
  25. emit close(
  26. WsClient client \\ who left
  27. )
  28. \* OPTIONAL: plain HTTP answered on the same port, exactly as
  29. NativeHttpServer's request. *\
  30. emit request(
  31. Hybrid req \\ method, path, headers, body, bytes, params, respond
  32. )
  33. Number port = 8080 \\ the port listen() binds
  34. String host = "0.0.0.0" \\ the interface to bind
  35. Number threads = 4 \\ I/O worker threads
  36. Boolean listening = false \\ true after a successful bind
  37. Hybrid clients = {} \\ live WsClients by connection id
  38. Number #srcId = 0
  39. Number #wsSrcId = 0
  40. \* Bind the configured port and start the WebSocket engine (idempotent). *\
  41. listen() {
  42. if (listening == true) {
  43. return null
  44. }
  45. srv = __native("http1.create_server", port, host, "", "", threads)
  46. if (srv == null) {
  47. // A REFUSAL, NOT PROSE (mission 286) — same reason as NativeHttpServer's.
  48. 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)")
  49. }
  50. wsev = __native("http1.ws_events")
  51. listening = true
  52. #srcId = __native("eventloop.register", srv, "request")
  53. #wsSrcId = __native("eventloop.register", wsev, "wsEvent")
  54. }
  55. \* One text frame to EVERY connected peer; answers how many received it. *\
  56. broadcast(
  57. String text \\ the frame to send
  58. ) {
  59. return __native("http1.ws_broadcast", text)
  60. }
  61. \* Stop this server: both event sources retired, workers joined, listener
  62. closed — the port is free again. Answers false when not listening. *\
  63. close() {
  64. if (listening == false) {
  65. return false
  66. }
  67. listening = false
  68. __native("eventloop.unregister", #srcId)
  69. __native("eventloop.unregister", #wsSrcId)
  70. return __native("http1.close", port)
  71. }
  72. // Raw native events ({ kind, id, data, binary, code }) re-emitted as the
  73. // documented surface — emit is the universal primitive.
  74. on wsEvent(ev) {
  75. key = "c" + ev.id
  76. if (ev.kind == "connect") {
  77. 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)
  78. clients[key] = client
  79. emit connect(client)
  80. }
  81. if (ev.kind == "message") {
  82. client = clients[key]
  83. if (client != null) {
  84. emit message(client, ev.data)
  85. }
  86. }
  87. if (ev.kind == "pong") {
  88. client = clients[key]
  89. if (client != null) {
  90. emit pong(client)
  91. }
  92. }
  93. if (ev.kind == "close") {
  94. client = clients[key]
  95. clients[key] = null
  96. if (client != null) {
  97. emit close(client)
  98. }
  99. }
  100. }
  101. // The upgrade's headers arrive as `name: value` lines (names already lowercase);
  102. // a connection is handed them as a hash (ticket #105).
  103. headerHash(lines) {
  104. let nl = "
  105. "
  106. let out = {}
  107. if (lines == null || lines == '') { return out }
  108. for (line of lines.split(nl)) {
  109. let colon = line.indexOf(': ')
  110. if (colon > 0) { out[line.slice(0, colon)] = line.slice(colon + 2) }
  111. }
  112. return out
  113. }
  114. // Run body: bind the configured port (idempotent, like NativeHttpServer).
  115. listen()

Branches

Latest commits

  • 4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mre
  • 86605446mission 002 (code order) 4/4: README file map + import order + 'Same output' test + gate run with a tickets HEAD copy, STATUS (entry, lessons), LOG, report; tests/realdata-baseline.mjs + realdata-compare.py (a cleanup answers the same on live data: pages, modules, API, git over HTTPS and SSH, faces), tests/letcount.pymre
  • cc7bf7bamission 002 (code order) 3/4: let only where a variable is reassigned or re-bound in a loop body (289 lets → plain declarations; 213 left: 125 reassigned, 88 loop-bound; no member/import/param clash). gates 200/0, 46/0, 44/0; real-data reads + writes identical (browser modules: var → const only)mre
  • 090a20c6mission 002 (code order) 2/4: one lib/ file per topic — git.hl split into git (calls, branches, init, temp folder) / homepage / code / pulls / releases (+ git-helpers: paths, ids, |||PR/|||RL markers); repos-helpers, tickets-helpers, transport-helpers; util.hl = localtime + env, storage dir, addresses, lists, text checks, one newest-first sort (was 3 copies); the function routes out of project.hl into lib/api.hl (thin; plumbing in api-helpers.hl), sshgate.hl folded into api.hl + sshkeys.hl keyLine + repos.hl mayPush; 'Make main' and the merge answer out of the faces (code.hl makeMain, pulls.hl pullsView), one login helper (users.hl userOfLoginCode); project.hl is the map. Session-writing routes get &req + &server.sessions. gates 200/0, 46/0, 44/0; real-data identical except /login/failed now shows the parked reason for a browser that already had a session (the old copy-of-req lost it)mre
  • 110c2799mission 002 (code order) 1/4: .hl files out of the root — lib/ (api, git, localtime, markdown, repos, sshgate, sshkeys, tickets, tokens, transport, users), components/styles.hl; jsoncheck.hl removed (imported nowhere); import paths only. gates 200/0, 46/0, 44/0; real-data reads + writes identicalmre
  • fdfb4b1bgitoria: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 200/0, 46/0, 44/0mre
  • 5b46ac84antcolony#40: LOG.md — missions 069/072 are antcolony missions (report paths on Byrodin)mre
  • 5602ff41gitoria: Hybriel master 190aa11d (fc838894 GC correctness, #127 mountKids by reference, #126, #48) — tracker README flat; gates 200/0, 46/0, 44/0mre
  • e85eaf01gitoria: 069 round 2 — hybriel 1a096ad3 not adopted (Markdown SSR still grows); browser gate waits for the server-side logout before restartmre
  • 09ce4f3fgitoria: mission 069 re-vendor hybriel 8efba065 stopped (big SSR pages grow + slow down); lambda audit clean; old vendor keptmre
  • 3dc43108antcolony#40: mission references point to the moved missionsmre
  • 8d9450fdantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 205d5fe4gitoria: Hybriel master ff51cf46; ssh keys/tokens no double rows (session sync); gates follow #20mre
  • 9b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre
  • 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
  • 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
  • fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
  • 4a2d7125initial commitmre