gitoriaLog in with ident

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain2149e902notes mission 002 (4/4): code order — README file map + same-output test, STATUS, LOG, report; tests/letcount.py, tests/realdata-baseline.mjs, tests/realdata-compare.pymremain/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

  • mainmain branch

Latest commits

  • 2149e902notes mission 002 (4/4): code order — README file map + same-output test, STATUS, LOG, report; tests/letcount.py, tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 47db4fc1notes mission 002 (3/4): code order — let only where reassigned (58 dropped; 34 left: 19 reassigned, 15 loop-bound); gates 50/0 + 18/0, live-data run = step 2mre
  • 3f8cb383notes mission 002 (2/4): code order — topics, map, thin wrappers: lib/util.hl, lib/notes(-helpers).hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login routes take &req/&sessions (failed-login reason now kept); dead notes#1 functions removed; gates 50/0 + 18/0mre
  • 8cda5412notes mission 002 (1/4): code order — files moved: lib/notes.hl, lib/users.hl, lib/jsoncheck.hl, components/styles.hl (imports only); gates 50/0 + 18/0, live-data run identicalmre
  • 47dad68bnotes: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 50/0 + 18/0mre
  • a4a2b2aeantcolony#40: tracker missions moved too — references to them in missions/reports/LOG.md updatedmre
  • 3dea0ef2notes: Hybriel master 190aa11d (fc838894 GC correctness, #126 closure scopes, #127); gate 50/0mre
  • e27c7d71notes: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy; audit: nothing to fix; gate 50/0)mre
  • 124613b6antcolony#40: mission references point to the moved missionsmre
  • 1ca2f34dantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 2bebebdanotes: Hybriel master ff51cf46 (re-vendor round)mre
  • 3eff126dnotes#3: installable app (manifest + own icon/favicon; notes' own sw.js kept)mre
  • 9883c540deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • eee693b8deploy.sh: never send .git or .gitignore to Byrodinmre
  • c8904061State of 2026-09-27, before the move to gitoriamre