diff --git a/Project.toml b/Project.toml index dea941d5..96746f3b 100644 --- a/Project.toml +++ b/Project.toml @@ -24,6 +24,7 @@ AssetRegistry = "0.1.0" FunctionalCollections = "0.5.0" JSExpr = "0.5" JSON = "0.18, 0.19, 0.20, 0.21" +Mux = "1.0" Observables = "0.5" Requires = "0.4.4, 0.5, 1.0.0" WebSockets = "1.5.0, 1.6.0" diff --git a/src/providers/mux.jl b/src/providers/mux.jl index eaa9e90e..90cbd358 100644 --- a/src/providers/mux.jl +++ b/src/providers/mux.jl @@ -29,17 +29,21 @@ function webio_serve(app::Mux.App, args...) end -struct WebSockConnection <: WebIO.AbstractConnection - sock +struct WebSockConnection{T} <: WebIO.AbstractConnection + sock::T end -function create_socket(req) +function create_socket(req::Dict) sock = req[:socket] - conn = WebSockConnection(sock) + # Dispatch on the type of socket if needed + _create_socket(sock) +end - t = @async while isopen(sock) - data = read(sock) +function _create_socket(sock::Mux.HTTP.WebSockets.WebSocket) + conn = WebSockConnection(sock) + # Iteration ends when the socket is closed + t = @async for data in sock msg = JSON.parse(String(data)) WebIO.dispatch(conn, msg) end @@ -48,10 +52,11 @@ function create_socket(req) end function Sockets.send(p::WebSockConnection, data) - write(p.sock, sprint(io->JSON.print(io,data))) + Mux.HTTP.WebSockets.send(p.sock, sprint(io->JSON.print(io,data))) end -Base.isopen(p::WebSockConnection) = isopen(p.sock) +# May not be strictly true +Base.isopen(p::WebSockConnection) = !Mux.HTTP.WebSockets.isclosed(p.sock) Mux.Response(o::AbstractWidget) = Mux.Response(Widgets.render(o)) function Mux.Response(content::Union{Node, Scope}) diff --git a/test/mux-tests.jl b/test/mux-tests.jl index edbcfdea..d140137f 100644 --- a/test/mux-tests.jl +++ b/test/mux-tests.jl @@ -1,9 +1,17 @@ using Test using WebIO using Mux +using Blink @testset "Mux sanity" begin @test isdefined(WebIO, :webio_serve) end +w = Window() +@testset "Mux + Blink" begin + t = WebIO.webio_serve(page("/", req -> dom"div"("hello")), 8006) + loadurl(w, "http://localhost:8006") + @test !istaskfailed(t.task) +end + # TODO: real mux tests (possibly using Blink as a headless chrome)