Websocket.jl
Why another Websocket package?
The existing Julia websocket solutions are in various states of development and lack comprehensive high level API’s comparable to offerings in other languages such as JS and Python.
For this reason I have ported the architecture of the popular Node.js websocket package into Julia.
Basic usage server:
using Websocket
server = WebsocketServer()
ended = Condition()
listen(server, :client) do client
listen(client, :message) do message
@info "Got a message" client = client.id message = message
send(client, "Echo back at you: $message")
end
end
listen(server, :connectError) do err
logWSerror(err)
notify(ended, err.msg, error = true)
end
listen(server, :closed) do details
@warn "Server has closed" details...
notify(ended)
end
@async serve(server; verbose = true)
wait(ended)
Basic usage client:
using Websocket
client = WebsocketClient()
ended = Condition()
listen(client, :connect) do ws
listen(ws, :message) do message
@info message
end
listen(ws, :close) do reason
@warn "Websocket connection closed" reason...
notify(ended)
end
for count = 1:10
send(ws, "hello $count")
sleep(1)
end
close(ws)
end
listen(client, :connectError) do err
logWSerror(err)
notify(ended, err.msg, error = true)
end
@async open(client, "ws://localhost:8080")
wait(ended)