From cf6285c34c6d0cadd530728e3c84632bd363beb4 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Mon, 14 Oct 2024 05:16:18 +0200 Subject: [PATCH] add events pushing to clients --- server.spl | 79 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/server.spl b/server.spl index 5f5b15b..7cd5d2e 100644 --- a/server.spl +++ b/server.spl @@ -1,6 +1,7 @@ "#httpserver/base.spl" import "#httpserver/static.spl" import +"#linkedlist.spl" import use net:http:Server 1048576 net:http:server:=bufsize @@ -14,6 +15,9 @@ construct Branch { commit this:=commit this } + json { json | + properties props-to-json + } } construct Branches { @@ -28,7 +32,24 @@ construct Branches { "Loader" dir "/BaseBand-Loader.jar" concat read-commit Branch:new this:=loader "Broadway" dir "/BaseBand-release.jar" concat read-commit Branch:new this:=release "Iceland" dir "/BaseBand-main.jar" concat read-commit Branch:new this:=main - this properties props-to-json + this + } + json { json | + properties props-to-json + } +} + +construct Event { + event-kind + data + ; + construct { this | with kind data this ; + kind this:=event-kind + data this:=data + this + } + json { json | + properties props-to-json } } @@ -37,7 +58,10 @@ construct Object404 { ; construct { this | with this ; "Endpoint not found" this:=error - this properties props-to-json + this + } + json { json | + properties props-to-json } } @@ -46,7 +70,10 @@ construct Refreshed { ; construct { this | with this ; "true" this:=ok - this properties props-to-json + this + } + json { json | + properties props-to-json } } @@ -76,34 +103,60 @@ func main { exitcode | with args ; } } +def clients LinkedList:new =clients + func dir { | with client dir location ; dir "/BaseBand-release.jar" concat location "/download/client/release" concat client:serve-file-cached;<"application/octet-stream"> dir "/BaseBand-main.jar" concat location "/download/client/main" concat client:serve-file-cached;<"application/octet-stream"> dir "/BaseBand-Loader.jar" concat location "/download/loader" concat client:serve-file-cached;<"application/octet-stream"> location "/branches" concat client:path eq if { - dir Branches:new client:write-ok:write-content-type<"application/json">:write-str-body:finish; + dir Branches:new:json client:write-ok:write-content-type<"application/json">:write-str-body:finish; } + "/refresh" client:path eq if { + def event "refresh:" location concat dir Branches:new Event:new:json =event + def new-clients LinkedList:new =new-clients + clients:foreach<{ | with client ; + catch IO { + event "\n" concat :to-bytes client:write-exact; + client new-clients:push-front; + } { pop } + }> + } } func handle-client { | with client ; client:read; - "index.html" "/" client:serve-html-cached; - - client "rewrite" "/1.12.2" dir - - client "ednieva" "/ednieva" dir + "/refresh" client:path eq if { - "refresh" println; client:server:cached-files if { client:server:cached-files:clear; } - Refreshed:new client:write-ok:write-content-type<"application/json">:write-str-body:finish; } - client:wrote-body not if { - Object404:new client:write-head<404 "Not Found">:write-content-type<"application/json">:write-str-body:finish; + "index.html" "/" client:serve-html-cached; + + client "rewrite" "/1.12.2" dir + client "ednieva" "/ednieva" dir + + "/listen" client:path eq if { + client + :write-ok + :write-content-type<"application/x-baseband-events"> + :writeln<""> + dup:=wrote-body;<1> + :stream + dup:flush; + clients:push-front; + } + + "/refresh" client:path eq if { + Refreshed:new:json client:write-ok:write-content-type<"application/json">:write-str-body:finish; + } + + client:is-open if { + Object404:new:json client:write-head<404 "Not Found">:write-content-type<"application/json">:write-str-body:finish; } }