From b4fc8e9b1be864292bbebf9dadbbc0d0e576f0d7 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Sun, 18 Aug 2024 20:42:48 -0400 Subject: [PATCH] Fix Mux integration. Use HTTP.WebSockets instead of Websockets.jl --- src/providers/mux.jl | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) 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})