Skip to content

Commit 1bd23a9

Browse files
authored
Merge pull request #205 from JuliaGizmos/sd/require_bugs
readguarded not exported via require
2 parents 8608fb4 + 966c15d commit 1bd23a9

File tree

5 files changed

+99
-76
lines changed

5 files changed

+99
-76
lines changed

assets/providers/websocket_connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
}
1818
}
1919
}
20+
2021
tryconnect(window.websocket_url)
2122
})();

src/WebIO.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using AssetRegistry
66
using Base64: stringmime
77
import Widgets
88
import Widgets: node, AbstractWidget
9+
using Logging
910

1011
abstract type AbstractConnection end
1112

@@ -65,10 +66,8 @@ function __init__()
6566
@require IJulia="7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
6667
include(joinpath("providers", "ijulia.jl"))
6768
end
68-
@require HTTP="cd3eb016-35fb-5094-929b-558a96fad6f3" begin
69-
@require WebSockets="104b5d7c-a370-577a-8038-80a2059c5097" begin
70-
include(joinpath("providers", "generic_http.jl"))
71-
end
69+
@require WebSockets="104b5d7c-a370-577a-8038-80a2059c5097" begin
70+
include(joinpath("providers", "generic_http.jl"))
7271
end
7372

7473
end

src/providers/generic_http.jl

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Sockets
22
import AssetRegistry, JSON
3-
using WebSockets: is_upgrade, upgrade
3+
using WebIO
4+
using .WebSockets: is_upgrade, upgrade
5+
using .WebSockets: HTTP
6+
47

58
struct WSConnection{T} <: WebIO.AbstractConnection
69
sock::T
@@ -32,9 +35,7 @@ end
3235

3336
wio_asseturl(file) = asseturl(string(normpath(WebIO.assetpath), '/', normpath(file)))
3437

35-
function serve_assets(req, serve_page)
36-
response = serve_page(req)
37-
response !== missing && return response
38+
function serve_assets(req)
3839
if haskey(AssetRegistry.registry, req.target)
3940
filepath = AssetRegistry.registry[req.target]
4041
if isfile(filepath)
@@ -51,7 +52,7 @@ end
5152
function websocket_handler(ws)
5253
conn = WSConnection(ws)
5354
while isopen(ws)
54-
data, success = readguarded(ws)
55+
data, success = WebSockets.readguarded(ws)
5556
!success && break
5657
msg = JSON.parse(String(data))
5758
WebIO.dispatch(conn, msg)
@@ -77,80 +78,47 @@ const routing_callback = Ref{Any}((req)-> missing)
7778
logging_io = devnull
7879
)
7980
80-
usage:
81-
82-
```example
83-
asset_port = 8081
84-
base_url = "127.0.0.1"
85-
const app = Ref{Any}()
86-
#WebIO.setbaseurl!(baseurl) # plus port!?
87-
server = WebIOServer(
88-
baseurl = base_url, http_port = asset_port, verbose = true
89-
) do req
90-
req.target != "/" && return missing # don't do anything
91-
isassigned(app) || return "no app"
92-
ws_url = string("ws://", base_url, ':', asset_port, "/webio_websocket/")
93-
webio_script = wio_asseturl("/webio/dist/bundle.js")
94-
ws_script = wio_asseturl("/providers/websocket_connection.js")
81+
usage to serve some webio html:
82+
83+
```example
84+
using WebSockets, WebIO
85+
app = Ref{Any}(node(:div, "hi"))
86+
function serve_app(req)
87+
req.target != "/" && return missing
9588
return sprint() do io
96-
print(io, "
97-
<!doctype html>
98-
<html>
99-
<head>
100-
<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>
104-
</head>
105-
<body>
106-
")
107-
tohtml(io, app[])
108-
print(io, "
109-
</body>
110-
</html>
111-
")
89+
print(io, \"\"\"
90+
<!doctype html><html><head>
91+
<meta charset="UTF-8"></head><body>
92+
\"\"\")
93+
show(io, MIME"application/webio"(), app[])
94+
print(io, "</body></html>")
11295
end
11396
end
114-
115-
w = Scope()
116-
117-
obs = Observable(w, "rand-value", 0.0)
118-
119-
on(obs) do x
120-
println("JS sent \$x")
121-
end
122-
123-
using JSExpr
124-
app[] = w(
125-
dom"button"(
126-
"generate random",
127-
events=Dict("click"=>@js () -> \$obs[] = Math.random()),
128-
),
129-
);
130-
```
97+
server = WebIO.WebIOServer(serve_app, logger = stdout, verbose = true)
98+
```
13199
"""
132100
function WebIOServer(
133101
default_response::Function = (req)-> missing;
134102
baseurl::String = "127.0.0.1", http_port::Int = 8081,
135103
verbose = false, singleton = true,
136104
websocket_route = "/webio_websocket/",
137-
logging_io = devnull,
105+
logger = devnull,
138106
server_kw_args...
139107
)
140108
# TODO test if actually still running, otherwise restart even if singleton
141109
if !singleton || !isassigned(singleton_instance)
142110
handler = HTTP.HandlerFunction() do req
143-
serve_assets(req, default_response)
111+
response = default_response(req)
112+
response !== missing && return response
113+
return serve_assets(req)
144114
end
145115
wshandler = WebSockets.WebsocketHandler() do req, sock
146-
try
147-
req.target == websocket_route && websocket_handler(sock)
148-
catch e
149-
@warn(e)
150-
end
116+
req.target == websocket_route && websocket_handler(sock)
117+
end
118+
server = WebSockets.ServerWS(handler, wshandler, logger; server_kw_args...)
119+
server_task = with_logger(NullLogger()) do
120+
@async WebSockets.serve(server, baseurl, http_port, verbose)
151121
end
152-
server = WebSockets.ServerWS(handler, wshandler; server_kw_args...)
153-
server_task = @async (ret = WebSockets.serve(server, baseurl, http_port, verbose))
154122
singleton_instance[] = WebIOServer(server, server_task)
155123
end
156124
return singleton_instance[]
@@ -170,7 +138,7 @@ function global_server_config()
170138

171139
url = get(ENV, "WEBIO_SERVER_HOST_URL", "127.0.0.1")
172140
http_port = parse(Int, get(ENV, "WEBIO_HTTP_PORT", "8081"))
173-
ws_default = string(url, ":", http_port, "/webio_websocket/")
141+
ws_default = string("ws://", url, ":", http_port, "/webio_websocket/")
174142
ws_url = get(ENV, "WEBIO_WEBSOCKT_URL", ws_default)
175143
webio_server_config[] = (url = url, http_port = http_port, ws_url = ws_url)
176144

@@ -191,15 +159,26 @@ can be used in the following way to create a generic display method for webio:
191159
```
192160
The above example enables display code & webio code that doesn't rely on any
193161
provider dependencies.
162+
If you want to host the bundle + websocket script somewhere else, you can also call:
163+
```example
164+
function Base.display(d::MyWebDisplay, m::MIME"application/webio", app)
165+
println(d.io, "outer html")
166+
show(io, m, app, bundle_url, websocket_url)
167+
println(d.io, "close outer html")
168+
end
169+
```
194170
"""
195-
function Base.show(io::IO, ::MIME"application/webio", app::Union{Scope, Node})
196-
# Make sure we run a server
197-
c = global_server_config()
198-
WebIOServer(routing_callback[], baseurl = c.url, http_port = c.http_port)
199-
171+
function Base.show(io::IO, m::MIME"application/webio", app::Union{Scope, Node})
200172
webio_script = wio_asseturl("/webio/dist/bundle.js")
201173
ws_script = wio_asseturl("/providers/websocket_connection.js")
174+
show(io, m, app, webio_script, ws_script)
175+
return
176+
end
202177

178+
function Base.show(io::IO, ::MIME"application/webio", app::Union{Scope, Node}, webio_script, ws_script)
179+
# Make sure we run a server
180+
c = global_server_config()
181+
WebIOServer(routing_callback[], baseurl = c.url, http_port = c.http_port)
203182
println(io, "<script> var websocket_url = $(repr(c.ws_url)) </script>")
204183
println(io, "<script src=$(repr(webio_script))></script>")
205184
println(io, "<script src=$(repr(ws_script)) ></script>")

test/REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ JSExpr
44
IJulia
55
NBInclude 2.0.0
66
NodeJS
7-
HTTP
87
WebSockets

test/http-tests.jl

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
11
using WebIO
2-
using HTTP, WebSockets
2+
using WebSockets
33
using JSExpr
44
using Test
55

66
@testset "HTTP provider" begin
77
output = sprint(io-> show(io, MIME"application/webio"(), node(:div, "hi")))
8-
@test occursin("<script> var websocket_url = \"127.0.0.1:8081/webio_websocket/\" </script>", output)
8+
@test occursin("<script> var websocket_url = \"ws://127.0.0.1:8081/webio_websocket/\" </script>", output)
99
@test occursin("""{"props":{},"nodeType":"DOM","type":"node","instanceArgs":{"namespace":"html","tag":"div"},"children":["hi"]}""", output)
10-
@test WebIO.webio_server_config[] == (url = "127.0.0.1", http_port = 8081, ws_url = "127.0.0.1:8081/webio_websocket/")
10+
@test WebIO.webio_server_config[] == (url = "127.0.0.1", http_port = 8081, ws_url = "ws://127.0.0.1:8081/webio_websocket/")
1111
@test isassigned(WebIO.singleton_instance)
1212
end
13+
14+
15+
using WebSockets, WebIO
16+
17+
app = Ref{Any}(node(:div, "hi"))
18+
function serve_app(req)
19+
if req.target == "/"
20+
return sprint() do io
21+
print(io, """
22+
<!doctype html>
23+
<html>
24+
<head>
25+
<meta charset="UTF-8">
26+
</head>
27+
<body>
28+
""")
29+
show(io, MIME"application/webio"(), app[])
30+
print(io, "
31+
</body>
32+
</html>
33+
")
34+
end
35+
else
36+
return missing
37+
end
38+
end
39+
server = WebIO.WebIOServer(serve_app, logger = stdout, verbose = true)
40+
using JSExpr
41+
WebIO.wio_asseturl("/webio/dist/bundle.js")
42+
server.serve_task
43+
44+
w = Scope()
45+
46+
obs = Observable(w, "rand-value", 0.0)
47+
48+
on(obs) do x
49+
println("JS sent $x")
50+
end
51+
52+
app[] = w(
53+
dom"button"(
54+
"generate random",
55+
events=Dict("click"=>@js () -> $obs[] = Math.random()),
56+
),
57+
);

0 commit comments

Comments
 (0)