gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf490bc8cf490bc8cdeploy: Web Push gate tests/push.mjs (PUSH_GATE_PORT)mref490bc8c/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

  • f490bc8cdeploy: Web Push gate tests/push.mjs (PUSH_GATE_PORT)mre
  • 3af29fc8calendar mission 008 (A8): report, README, LOG, push-shots.mjsmre
  • f91f3663calendar mission 008 (A8): push only asked of the browser when the app has the VAPID settings (pushAvailable); real-FCM E2E scripts (push-real-app.mjs, push-real.mjs)mre
  • bf571badcalendar mission 008 (A8): Turn off on this device (unsubscribes the browser, server drops the device, choice kept), subscribe/refresh when the app is shown again; push gate part 3mre
  • 50985708calendar mission 008 (A7): Web Push report, README, mission, LOGmre
  • ee318774calendar mission 008 (A7): push-real.mjs, push sent log linemre
  • ebda0fd1calendar mission 008 (A7): server-side reminder plan on hl:time (push-plan.hl; hl:time fromLocal in a browser static is refused: repro .scratch/hltime-client), push gate 9/9mre
  • d063dad3calendar mission 008 (A7): Web Push (WIP): lib/push.hl subscriptions + sender clock, faces calPushSave/calPushOff, button subscribes, VAPID tool, tests/push.mjs; instantOf on hl:timemre
  • cf175c80calendar mission 008 (A7): re-vendor Hybriel master 0e3c9830 (bin 9d957fed, plugins whole: hl:crypto Web Push primitives, hl:web sendPush)mre
  • 9bf3c38bcalendar mission 008: re-vendor Hybriel f9833544 (gates old = new); Web Push stopped: hl:crypto half missing on mastermre
  • 186f969bcalendar mission 007: signed-in gate for the first-pointer race (fails on 3c5604f), README theme section, reportmre
  • ba9b4955calendar mission 006: first pointer event no longer re-runs the month (stale overwrite race), gate check, e2e settings cleanup, reportmre
  • bd730759calendar mission 006: theme move (WIP): brief, report, LOG, STATUSmre
  • b117b153calendar mission 006 WIP: reminder row fields keep 44 px; e2e script flagsmre
  • 53e3b0debrowser gate: Log out through the user menu, theme colour rgb(15, 20, 25)mre
  • 90bf9e2ecalendar mission 006 WIP: theme dbe3de7, theme shell (AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst), styles cut to the app's own rules, check-theme gate, e2e toolsmre
  • 3c5604f9identkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 4996bbe7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • f5bf0803calendar mission 004: re-vendor Hybriel to the shared static build 0f396652; compose cache volume + tmpfs; notify-paths gate in deploy.sh; gate start waits 150 s; reportmre
  • eb86526fmission brief 004mre