Wobserver v0.1.8 Wobserver.Web.ClientSocket behaviour View Source

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

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

Callbacks

Handles messages coming from the WS client

Handles messages coming from other processes

Initalizes the WebSocket

Link to this section Types

Link to this type response() View Source
response ::
  {:reply, atom | [atom], any, any} |
  {:reply, atom | [atom], any} |
  {:noreply, any}

Response to browser.

Link to this section Functions

Link to this function parse_command(payload) View Source
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, req) View Source
send_response(message :: {:noreply, any} | {:reply, atom | [atom], any} | {:reply, atom | [atom], map | list | String.t | nil, any}, socket_state :: map, req :: :cowboy_req.req) ::
  {:reply, {:text, String.t}, :cowboy_req.req, map} |
  {:ok, :cowboy_req.req, 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, req) View Source
setup_proxy(proxy :: String.t, state :: map, req :: :cowboy_req.req) ::
  {:reply, {:text, String.t}, :cowboy_req.req, map} |
  {:ok, :cowboy_req.req, map}

Sets up a websocket proxy to a given proxy.

The state is modified to add in the new proxy, while req is returned untouched.

Link to this section Callbacks

Link to this callback client_handle(arg0, any) View Source
client_handle(atom | {atom, any}, any) :: Wobserver.Web.ClientSocket.response

Handles messages coming from the WS client.

Return browser response.

Handles messages coming from other processes.

Return browser response.

Link to this callback client_init() View Source
client_init() :: {:ok, any} | {:ok, any, non_neg_integer}

Initalizes the WebSocket.

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