Skip to content

Commit 8905c2d

Browse files
SimonDanischshashi
authored andcommitted
combine websock + http
1 parent 6c1b96c commit 8905c2d

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/providers/generic_http.jl

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using WebSockets, Sockets
2-
using HTTP, AssetRegistry, WebIO, JSON
2+
import HTTP, AssetRegistry, JSON
33
using WebSockets: is_upgrade, upgrade
4-
4+
using WebIO
55
struct WSConnection{T} <: WebIO.AbstractConnection
66
sock::T
77
end
@@ -26,13 +26,15 @@ function tohtml(io, node)
2626
end
2727

2828
function asseturl(file)
29-
path = string(normpath(WebIO.assetpath), '/', normpath(file))
29+
path = normpath(file)
3030
WebIO.baseurl[] * AssetRegistry.register(path)
3131
end
3232

33+
wio_asseturl(file) = asseturl(string(normpath(WebIO.assetpath), '/', normpath(file)))
34+
3335
function serve_assets(req, serve_page)
3436
response = serve_page(req)
35-
response === missing || return response
37+
response !== missing && return response
3638
if haskey(AssetRegistry.registry, req.target)
3739
filepath = AssetRegistry.registry[req.target]
3840
if isfile(filepath)
@@ -46,7 +48,7 @@ function serve_assets(req, serve_page)
4648
return "not found"
4749
end
4850

49-
function websocket_handler(req, ws)
51+
function websocket_handler(ws)
5052
conn = WSConnection(ws)
5153
while isopen(ws)
5254
data, success = readguarded(ws)
@@ -59,10 +61,9 @@ end
5961
struct WebIOServer{S}
6062
server::S
6163
serve_task::Task
62-
ws_task::Task
6364
end
6465

65-
kill!(server::WebIOServer) = put!(server.in, HTTP.Servers.KILL)
66+
kill!(server::WebIOServer) = put!(server.server.in, HTTP.Servers.KILL)
6667

6768
const singleton_instance = Ref{WebIOServer}()
6869

@@ -93,22 +94,22 @@ server = WebIOServer(
9394
webio_script = asseturl("/webio/dist/bundle.js")
9495
ws_script = asseturl("/providers/websocket_connection.js")
9596
return sprint() do io
96-
print(io, """
97+
print(io, "
9798
<!doctype html>
9899
<html>
99100
<head>
100101
<meta charset="UTF-8">
101-
<script> var websocket_url = $(repr(ws_url)) </script>
102-
<script src="$webio_script"></script>
103-
<script src="$ws_script"></script>
102+
<script> var websocket_url = \$(repr(ws_url)) </script>
103+
<script src="\$webio_script"></script>
104+
<script src="\$ws_script"></script>
104105
</head>
105106
<body>
106-
""")
107+
")
107108
tohtml(io, app[])
108-
print(io, """
109+
print(io, "
109110
</body>
110111
</html>
111-
""")
112+
")
112113
end
113114
end
114115
@@ -124,24 +125,25 @@ app[] = node(:div, "Hello, World",
124125
function WebIOServer(
125126
default_response::Function = (req)-> missing;
126127
baseurl::String = "127.0.0.1", http_port::Int = "8081",
127-
ws_port::Int = 8000, verbose = false, singleton = true,
128-
logging_io = devnull
128+
verbose = false, singleton = true,
129+
websocket_route = "/webio_websocket/",
130+
logging_io = devnull,
131+
server_kw_args...
129132
)
130-
WebIO.setbaseurl!(baseurl) # plus port!?
131133
if !singleton || !isassigned(singleton_instance)
132134
handler = HTTP.HandlerFunction() do req
133135
serve_assets(req, default_response)
134136
end
135-
server = HTTP.Server(handler)
136-
server_task = @async (ret = HTTP.serve(server, baseurl, http_port, verbose = verbose))
137-
tcpref = Ref{Sockets.TCPServer}()
138-
# Start HTTP listen server on port $port_HTTP"
139-
ws_task = @async HTTP.listen(
140-
baseurl, ws_port, tcpref = tcpref, verbose = verbose
141-
) do s
142-
is_upgrade(s.message) && upgrade(websocket_handler, s)
137+
wshandler = WebSockets.WebsocketHandler() do req, sock
138+
try
139+
req.target == websocket_route && websocket_handler(sock)
140+
catch e
141+
@warn(e)
142+
end
143143
end
144-
singleton_instance[] = WebIOServer(server, server_task, ws_task)
144+
server = WebSockets.ServerWS(handler, wshandler; server_kw_args...)
145+
server_task = @async (ret = WebSockets.serve(server, baseurl, http_port, verbose))
146+
singleton_instance[] = WebIOServer(server, server_task)
145147
end
146148
return singleton_instance[]
147149
end

0 commit comments

Comments
 (0)