WebSockex v0.1.1 WebSockex.Client behaviour
A client handles negotiating the connection, then sending frames, receiving frames, closing, and reconnecting that connection.
A simple client implementation would be:
defmodule WsClient do
use WebSockex.Client
def start_link(state) do
WebSockex.Client.start_link(__MODULE__, state)
end
def handle_frame({:text, msg}, state) do
IO.puts "Received a message: #{msg}"
{:ok, state}
end
end
Summary
Types
An integer between 1000 and 4999 that specifies the reason for closing the connection
The frame sent when the negotiating a connection closure
The reason given and sent to the server when locally closing a connection
Functions
Asynchronously sends a message to a client that is handled by handle_cast/2
Queue a frame to be sent asynchronously
Starts a WebSockex.Client process linked to the current process
Callbacks
Invoked when a new version the module is loaded during runtime
Invoked to handle asynchronous cast/2 messages
Invoked when the WebSocket disconnects from the server
Invoked on the reception of a frame on the socket
Invoked to handle all other non-WebSocket messages
Invoked when the Websocket receives a ping frame
Invoked when the Websocket receives a pong frame
Invoked after connection is established
Invoked when the process is terminating
Types
An integer between 1000 and 4999 that specifies the reason for closing the connection.
The frame sent when the negotiating a connection closure.
close_reason :: {:remote | :local, :normal} | {:remote | :local, :normal | close_code, close_frame} | {:error, term}
The reason given and sent to the server when locally closing a connection.
A :normal reason is the same as a 1000 reason.
Functions
Asynchronously sends a message to a client that is handled by handle_cast/2.
Queue a frame to be sent asynchronously.
Starts a WebSockex.Client process linked to the current process.
Options
:extra_headers- defines other headers to be send in the opening request.:insecure- Determines whether to verify the peer in a SSL connection. SSL peer verification is currenctly broken and only works in certain cases in which the:cacertsare also provided. Sorry. Defaults totrue.:cacerts- The CA certifications for use in an secure connection when the:insecureoption isfalse(has no effect when:insecure is true). These certifications need a list of decoded binaries. See the Erlang:public_keymodule for more information.
Callbacks
code_change(old_vsn :: term | {:down, term}, state :: term, extra :: term) ::
{:ok, new_state :: term} |
{:error, reason :: term}
Invoked when a new version the module is loaded during runtime.
handle_cast(msg :: term, state :: term) ::
{:ok, new_state} |
{:reply, frame, new_state} |
{:close, new_state} |
{:close, close_frame, new_state} when new_state: term
Invoked to handle asynchronous cast/2 messages.
handle_disconnect(close_reason, state :: term) :: {:ok, state} | {:reconnect, state} when state: term
Invoked when the WebSocket disconnects from the server.
handle_frame(frame, state :: term) :: {:ok, new_state} | {:reply, frame, new_state} | {:close, new_state} | {:close, close_frame, new_state} when new_state: term
Invoked on the reception of a frame on the socket.
The control frames have possible payloads, when they don’t have a payload
then the frame will have nil as the payload. e.g. {:ping, nil}
handle_info(msg :: term, state :: term) ::
{:ok, new_state} |
{:reply, frame, new_state} |
{:close, new_state} |
{:close, close_frame, new_state} when new_state: term
Invoked to handle all other non-WebSocket messages.
handle_ping(:ping | {:ping, binary}, state :: term) ::
{:ok, new_state} |
{:reply, frame, new_state} |
{:close, new_state} |
{:close, close_frame, new_state} when new_state: term
Invoked when the Websocket receives a ping frame
handle_pong(:pong | {:pong, binary}, state :: term) ::
{:ok, new_state} |
{:reply, frame, new_state} |
{:close, new_state} |
{:close, close_frame, new_state} when new_state: term
Invoked when the Websocket receives a pong frame.
Invoked after connection is established.
Invoked when the process is terminating.