-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.jl
More file actions
56 lines (50 loc) · 1.58 KB
/
server.jl
File metadata and controls
56 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const connections = Dict{Int,WebSocket}()
"""
Send a message to a web page to be interpetted by WebSocket listener. See coherent.js -> ws.onmessage.
"""
function message(t,data)
msg = Dict("type"=>t,"data"=>data)
for (k,v) in connections
if ~v.is_closed
write(v, json(msg))
end
end
end
"""
Evaluates a JS script and blocks the Julia control flow until JS sends a coherent.message("notify",name) back.
"""
function block(name,script)
test = """coherent.message("notify","$name")"""
if contains(script,test)
condition[name] = Condition()
message("script",script)
wait(condition[name])
delete!(condition,name)
else
error("""Script must contain "$test".""")
end
end
ws = WebSocketHandler() do req::Request, client::WebSocket
while true
connections[client.id] = client
json = bytestring(read(client))
msg = JSON.parse(json)
if haskey(msg,"args")
callback[msg["name"]](msg["args"])
else
callback[msg["name"]]()
end
end
end
http = HttpHandler() do req::Request, res::Response
if ismatch(r"^/plot.ly", req.resource)
res = Response(open(readall,Pkg.dir("JavaScriptBridge","res","plotly.html")))
elseif ismatch(r"^/julia", req.resource)
res = Response(open(readall,Pkg.dir("JavaScriptBridge","res","bridge.html")))
elseif ismatch(r"^/bridge.js", req.resource)
res = Response(open(readall,Pkg.dir("JavaScriptBridge","res","bridge.js")))
end
res
end
server = Server(http,ws)
@async run(server, 8000)