From 7e4c7ac1507cb13005eaafcfbb4d7c91e462bed9 Mon Sep 17 00:00:00 2001 From: andrei Date: Fri, 25 Nov 2022 16:58:35 +0100 Subject: [PATCH 1/2] remove dependency on WebSockets --- Project.toml | 8 ++++---- src/content/content.jl | 8 +++++--- src/content/server.jl | 44 ++++++++---------------------------------- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/Project.toml b/Project.toml index dd2a304..b7a4b64 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ Mux = "a975b10e-0019-58db-a62f-e48ff68538c9" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" -WebSockets = "104b5d7c-a370-577a-8038-80a2059c5097" +HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" [compat] BinDeps = "0.8, 1.0" @@ -26,11 +26,11 @@ JSON = "0.21, 1.0" Lazy = "0.13, 0.14, 0.15" MacroTools = "0.4, 0.5" Mustache = "0.5, 1.0" -Mux = "0.7" +Mux = "1.0" Reexport = "0.2, 1" WebIO = "=0.8.0, =0.8.1, =0.8.2, =0.8.3, =0.8.4, =0.8.5, 0.8.7" -WebSockets = "1.5" -julia = "1" +HTTP = "1.5.0" +julia = "1.6" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/content/content.jl b/src/content/content.jl index 683f4a7..112e365 100644 --- a/src/content/content.jl +++ b/src/content/content.jl @@ -1,4 +1,6 @@ -using Mux, WebSockets, JSON, Lazy, Mustache +using Mux, JSON, Lazy, Mustache +import HTTP: WebSocket +import HTTP.WebSockets: isclosed export Page, id, active @@ -30,7 +32,7 @@ end include("config.jl") id(p::Page) = p.id -active(p::Page) = isdefined(p, :sock) && isopen(p.sock) && isopen(p.sock.socket) +active(p::Page) = isdefined(p, :sock) && !isclosed(p.sock) handlers(p::Page) = p.handlers function Base.wait(p::Page) @@ -40,7 +42,7 @@ end function msg(p::Page, m) active(p) || wait(p) - write(p.sock, json(m)) + send(p.sock, json(m)) end const pool = Dict{Int, WeakRef}() diff --git a/src/content/server.jl b/src/content/server.jl index 29dcb6a..ee8b99d 100644 --- a/src/content/server.jl +++ b/src/content/server.jl @@ -1,5 +1,5 @@ # Resources - +import HTTP.WebSockets: CloseFrameBody const resources = Dict{String, String}() resource(f, name = basename(f)) = (@assert isfile(f); resources[name] = f) @@ -30,35 +30,22 @@ function page_handler(req) :status => 404) end -function ws_handler(req) - id = try parse(Int, req[:path][end]) catch e @goto fail end - client = req[:socket] +function ws_handler(ws) + id = try parse(Int, split(ws.request.target, "/", keepempty=false)[end]) catch e @goto fail end haskey(pool, id) || @goto fail p = pool[id].value active(p) && @goto fail - p.sock = client + p.sock = ws @async @errs get(handlers(p), "init", identity)(p) put!(p.cb, true) - while active(p) - local data - try - data = read(client) - catch e - if (isa(e, ArgumentError) && contains(e.msg, "closed")) || isa(e, WebSockets.WebSocketClosedError) - handle_message(p, d("type"=>"close", "data"=>nothing)) - yield() # Prevents an HttpServer task error (!?) - return - else - rethrow() - end - end - @errs handle_message(p, JSON.parse(String(data))) + for msg in ws + @errs handle_message(p, JSON.parse(String(msg))) end return @label fail - close(client) + close(ws, CloseFrameBody(1000, "")) end http_default = @@ -67,27 +54,12 @@ http_default = page(":id", page_handler), Mux.notfound()) -ws_default = - mux(Mux.wdefaults, - ws_handler) - const serving = Ref(false) function serve() serving[] && return serving[] = true - http = Mux.http_handler(Mux.App(http_default)) - ws = Mux.ws_handler(Mux.App(ws_default)) @async begin - # Suppress log output from HTTP.jl unless the user has already - # configured a custom logger. See: - # https://github.com/JuliaLang/julia/pull/28229#issuecomment-410229612 - if current_task().logstate === nothing - with_logger(NullLogger()) do - WebSockets.serve(WebSockets.ServerWS(http, ws), ip"127.0.0.1", port[], false) - end - else - WebSockets.serve(WebSockets.ServerWS(http, ws), ip"127.0.0.1", port[], false) - end + Mux.serve(Mux.App(http_default), Mux.App(ws_handler), ip"127.0.0.1", port[]) end end From 8f657af879c69eecdefb301f3da20e6088a8b1e9 Mon Sep 17 00:00:00 2001 From: andrei Date: Fri, 25 Nov 2022 18:15:20 +0100 Subject: [PATCH 2/2] bump version to "0.12.6" --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b7a4b64..cf6d3d4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Blink" uuid = "ad839575-38b3-5650-b840-f874b8c74a25" authors = [] -version = "0.12.5" +version = "0.12.6" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"