View Source Wobserver.Web.ClientSocket behaviour (Wobserver NG v1.14.0)

Low level WebSocket handler

Connects to the Javascript websocket and parses all requests.

Example:

  defmodule Wobserver.Web.Client do
    use Wobserver.Web.ClientSocket

    alias Wobserver.System

    def client_init do
      {:ok, %{}}
    end

    def client_handle(:hello, state) do
      {:reply, :ehlo, state}
    end

    def client_info(:update, state) do
      {:noreply, state}
    end
  end

Link to this section Summary

Types

Response to browser.

Callbacks

Handles messages coming from the WS client.

Handles messages coming from other processes.

Initalizes the WebSocket.

Functions

Parses the JSON payload to an atom command and map data.

Send a JSON encoded to the websocket client.

Sets up a websocket proxy to a given proxy.

Link to this section Types

@type response() ::
  {:reply, atom() | [atom()], any(), any()}
  | {:reply, atom() | [atom()], any()}
  | {:noreply, any()}

Response to browser.

Link to this section Callbacks

Link to this callback

client_handle(arg1, any)

View Source
@callback client_handle(atom() | {atom(), any()}, any()) :: response()

Handles messages coming from the WS client.

Return browser response.

@callback client_info(any(), any()) :: response()

Handles messages coming from other processes.

Return browser response.

@callback client_init() :: {:ok, any()} | {:ok, any(), non_neg_integer()}

Initalizes the WebSocket.

Return {:ok, initial state} or {:ok, initial state, socket timeout}.

Link to this section Functions

@spec parse_command(payload :: String.t()) :: atom() | {atom(), any()}

Parses the JSON payload to an atom command and map data.

Link to this function

send_response(message, socket_state)

View Source
@spec send_response(
  message ::
    {:noreply, any()}
    | {:reply, atom() | [atom()], any()}
    | {:reply, atom() | [atom()], map() | list() | String.t() | nil, any()},
  socket_state :: map()
) :: {:reply, {:text, String.t()}, map()} | {:ok, map()}

Send a JSON encoded to the websocket client.

The given message is JSON encoded (exception: :noreply). The socket_state is used updated to reflect changes made by the client. The cowboy req is returned untouched.

Link to this function

setup_proxy(proxy, state)

View Source
@spec setup_proxy(proxy :: String.t(), state :: map()) ::
  {:reply, {:text, String.t()}, map()} | {:ok, map()}

Sets up a websocket proxy to a given proxy.

The state is modified to add in the new proxy.