gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/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", port)
  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, port)
  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, ev.path) // 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

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre