Skip to content

Commit

Permalink
Merge pull request #295 from anj00/switch2http
Browse files Browse the repository at this point in the history
remove dependency on WebSockets
  • Loading branch information
pfitzseb authored Dec 15, 2022
2 parents 054c1e8 + 8f657af commit 5176c9e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions src/content/content.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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}()
Expand Down
44 changes: 8 additions & 36 deletions src/content/server.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 =
Expand All @@ -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

0 comments on commit 5176c9e

Please sign in to comment.