Websocket Client
Provides a Websocket client compatible with Websocket versions [8, 13]
Currently does not support Websocket Extensions
Minimum required usage:
using SimpleWebsockets
client = WebsocketClient()
listen(client, :connect) do ws::WebsocketConnection #must be called before `open`
#...
end
open(client, "ws://url.url")
Constructor
SimpleWebsockets.WebsocketClient
— TypeWebsocketClient([; options...])
Constructs a new WebsocketClient, overriding clientConfig
with the passed options.
Example
using SimpleWebsockets
client = WebsocketClient([; options...])
Client Methods
Base.open
— Methodopen(client::WebsocketClient, url::String [, headers::Dict{String, String}; options...])
Open a new websocket client connection at the given url
. Blocks until the connection is closed.
Optionally provide custom headers
for the http request.
; options...
are passed to the underlying HTTP.request
Base.isopen
— Methodisopen(client::WebsocketClient)::Bool
Returns a Bool indication if the client TCP connection is open.
Client Events
Client event callback functions are registered using the listen
method.
SimpleWebsockets.listen
— Methodlisten(callback::Function, client::SimpleWebsockets.WebsocketClient, event::Symbol)
Register event callbacks onto a client. The callback must be a function with exactly one argument.
Valid events are:
- :connect
- :connectError
Example
listen(client, :connect) do ws
#...
end
Triggered when the client has successfully connected to the server
Returns a WebsocketConnection
to the callback.
listen(client, :connect) do ws::SimpleWebsockets.WebsocketConnection
#...
end
Triggered when an attempt to open a client connection fails
listen(client, :connectError) do err::WebsocketError.ConnectError
# err.msg::String
# err.log::Function > logs the error message with stack trace
end