gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit14abde3b14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre14abde3b/plugins/http1/native_web_socket_server.hl

4.4 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. Number maxBody = 20971520 \\ largest request body read, bytes (default 20 MiB); over it: 413, connection closed
  37. Boolean wsCheckOrigin = false \\ refuse an upgrade whose Origin is foreign (not the Host, not in wsOrigins) with 403 before the 101; no Origin = allowed
  38. String wsOrigins = "" \\ extra allowed origins, comma-separated
  39. Boolean listening = false \\ true after a successful bind
  40. Hybrid clients = {} \\ live WsClients by connection id
  41. Number #srcId = 0
  42. Number #wsSrcId = 0
  43. \* Bind the configured port and start the WebSocket engine (idempotent). *\
  44. listen() {
  45. if (listening == true) {
  46. return null
  47. }
  48. srv = __native("http1.create_server", port, host, "", "", threads, maxBody, wsCheckOrigin, wsOrigins)
  49. if (srv == null) {
  50. // A REFUSAL, NOT PROSE (mission 286) — same reason as NativeHttpServer's.
  51. 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)")
  52. }
  53. wsev = __native("http1.ws_events")
  54. listening = true
  55. #srcId = __native("eventloop.register", srv, "request")
  56. #wsSrcId = __native("eventloop.register", wsev, "wsEvent")
  57. }
  58. \* One text frame to EVERY connected peer; answers how many received it. *\
  59. broadcast(
  60. String text \\ the frame to send
  61. ) {
  62. return __native("http1.ws_broadcast", text)
  63. }
  64. \* Stop this server: both event sources retired, workers joined, listener
  65. closed — the port is free again. Answers false when not listening. *\
  66. close() {
  67. if (listening == false) {
  68. return false
  69. }
  70. listening = false
  71. __native("eventloop.unregister", #srcId)
  72. __native("eventloop.unregister", #wsSrcId)
  73. return __native("http1.close", port)
  74. }
  75. // Raw native events ({ kind, id, data, binary, code }) re-emitted as the
  76. // documented surface — emit is the universal primitive.
  77. on wsEvent(ev) {
  78. key = "c" + ev.id
  79. if (ev.kind == "connect") {
  80. client = new WsClient(ev.id, ev.cookie, ev.host != '' ? ev.host : null, headerHash(ev.headers), ev.ip != '' ? ev.ip : null, ev.secure) // the upgrade's cookie, host and headers ride with the connection (creator, 2026-09-12; tickets #74, #105)
  81. clients[key] = client
  82. emit connect(client)
  83. }
  84. if (ev.kind == "message") {
  85. client = clients[key]
  86. if (client != null) {
  87. emit message(client, ev.data)
  88. }
  89. }
  90. if (ev.kind == "pong") {
  91. client = clients[key]
  92. if (client != null) {
  93. emit pong(client)
  94. }
  95. }
  96. if (ev.kind == "close") {
  97. client = clients[key]
  98. clients[key] = null
  99. if (client != null) {
  100. emit close(client)
  101. }
  102. }
  103. }
  104. // The upgrade's headers arrive as `name: value` lines (names already lowercase);
  105. // a connection is handed them as a hash (ticket #105).
  106. headerHash(lines) {
  107. let nl = "
  108. "
  109. let out = {}
  110. if (lines == null || lines == '') { return out }
  111. for (line of lines.split(nl)) {
  112. let colon = line.indexOf(': ')
  113. if (colon > 0) { out[line.slice(0, colon)] = line.slice(colon + 2) }
  114. }
  115. return out
  116. }
  117. // Run body: bind the configured port (idempotent, like NativeHttpServer).
  118. listen()

Branches

Latest commits

  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre