1- using . Sockets
2- import . AssetRegistry, . JSON
3- using . WebIO
4- using . WebSockets: is_upgrade, upgrade, writeguarded
5- using . WebSockets: HTTP
1+ module WebSocketsExt
62
3+ using WebIO, JSON, AssetRegistry
4+ using Sockets
5+
6+ using WebIO: WEBIO_APPLICATION_MIME, GENERIC_HTTP_BUNDLE_PATH, bundle_key,
7+ global_server_config, WebIOServer, singleton_instance, routing_callback,
8+ webio_server_config
9+ using WebSockets: WebSockets, HTTP, is_upgrade, upgrade, writeguarded
710
811struct WSConnection{T} <: WebIO.AbstractConnection
912 sock:: T
1215Sockets. send (p:: WSConnection , data) = writeguarded (p. sock, JSON. json (data))
1316Base. isopen (p:: WSConnection ) = isopen (p. sock)
1417
15- if ! isfile (GENERIC_HTTP_BUNDLE_PATH)
16- error (
17- " Unable to find WebIO JavaScript bundle for generic HTTP provider; "
18- * " try rebuilding WebIO (via `Pkg.build(\" WebIO\" )`)."
19- )
20- end
21- const bundle_key = AssetRegistry. register (GENERIC_HTTP_BUNDLE_PATH)
22-
23- include (joinpath (@__DIR__ , " .." , " .." , " deps" , " mimetypes.jl" ))
18+ include (joinpath (@__DIR__ , " .." , " deps" , " mimetypes.jl" ))
2419
2520"""
2621Serve an asset from the asset registry.
@@ -52,17 +47,8 @@ function websocket_handler(ws)
5247 end
5348end
5449
55- struct WebIOServer{S}
56- server:: S
57- serve_task:: Task
58- end
59-
6050kill! (server:: WebIOServer ) = put! (server. server. in, HTTP. Servers. KILL)
6151
62- const singleton_instance = Ref {WebIOServer} ()
63-
64- const routing_callback = Ref {Any} ((req)-> missing )
65-
6652"""
6753Run the WebIO server.
6854
8470server = WebIO.WebIOServer(serve_app, verbose = true)
8571```
8672"""
87- function WebIOServer (
73+ function WebIO . WebIOServer (
8874 default_response:: Function = (req)-> missing ;
8975 baseurl:: String = " 127.0.0.1" , http_port:: Int = 8081 ,
9076 verbose = false , singleton = true ,
@@ -111,7 +97,7 @@ function WebIOServer(
11197 else # relative url
11298 string (" http://" , baseurl, " :" , http_port, WebIO. baseurl[])
11399 end
114- string (base, bundle_key)
100+ string (base, bundle_key[] )
115101 end
116102 wait_time = 5 ; start = time () # wait for max 5 s
117103 while time () - start < wait_time
@@ -127,24 +113,22 @@ function WebIOServer(
127113 return singleton_instance[]
128114end
129115
130- const webio_server_config = Ref {typeof((url = "", bundle_url = "", http_port = 0, ws_url = ""))} ()
131-
132116"""
133117Fetches the global configuration for our http + websocket server from environment
134118variables. It will memoise the result, so after a first call, any update to
135119the environment will get ignored.
136120"""
137- function global_server_config ()
121+ function WebIO . global_server_config ()
138122 if ! isassigned (webio_server_config)
139123
140- setbaseurl! (get (ENV , " JULIA_WEBIO_BASEURL" , " " ))
124+ WebIO . setbaseurl! (get (ENV , " JULIA_WEBIO_BASEURL" , " " ))
141125
142126 url = get (ENV , " WEBIO_SERVER_HOST_URL" , " 127.0.0.1" )
143127 http_port = parse (Int, get (ENV , " WEBIO_HTTP_PORT" , " 8081" ))
144128 ws_default = string (" ws://" , url, " :" , http_port, " /webio_websocket/" )
145129 ws_url = get (ENV , " WEBIO_WEBSOCKT_URL" , ws_default)
146130 # make it possible, to e.g. host the bundle online
147- bundle_url = get (ENV , " WEBIO_BUNDLE_URL" , string (WebIO. baseurl[], bundle_key))
131+ bundle_url = get (ENV , " WEBIO_BUNDLE_URL" , string (WebIO. baseurl[], bundle_key[] ))
148132 webio_server_config[] = (
149133 url = url, bundle_url = bundle_url,
150134 http_port = http_port, ws_url = ws_url
@@ -176,3 +160,15 @@ function Base.show(io::IO, m::WEBIO_APPLICATION_MIME, app::Application)
176160 show (io, " text/html" , app)
177161 return
178162end
163+
164+ function __init__ ()
165+ if ! isfile (GENERIC_HTTP_BUNDLE_PATH)
166+ error (
167+ " Unable to find WebIO JavaScript bundle for generic HTTP provider; "
168+ * " try rebuilding WebIO (via `Pkg.build(\" WebIO\" )`)."
169+ )
170+ end
171+ bundle_key[] = AssetRegistry. register (GENERIC_HTTP_BUNDLE_PATH)
172+ end
173+
174+ end
0 commit comments