11using Sockets
22import AssetRegistry, JSON
3- using WebSockets: is_upgrade, upgrade
3+ using WebIO
4+ using . WebSockets: is_upgrade, upgrade
5+ using . WebSockets: HTTP
6+
47
58struct WSConnection{T} <: WebIO.AbstractConnection
69 sock:: T
3235
3336wio_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)
5152function 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"""
132100function 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 ```
192160The above example enables display code & webio code that doesn't rely on any
193161provider 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>" )
0 commit comments