Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Mux integration. Use HTTP.WebSockets instead of Websockets.jl #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 13 additions & 8 deletions src/providers/mux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
Expand Down
8 changes: 8 additions & 0 deletions test/mux-tests.jl
Original file line number Diff line number Diff line change
@@ -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)
Loading