WebsockexNova.Gun.ConnectionWrapper.MessageHandlers (WebsockexNova v0.1.1)

View Source

Handles Gun message processing for the ConnectionWrapper.

This module provides a set of functions for handling different types of Gun messages, organized by category (connection lifecycle, WebSocket, HTTP, error messages). Each handler receives the relevant parameters and the current state, and returns an updated state.

Summary

Functions

Handles the :gun_up message when a connection is established.

Handles the :gun_error message when an error occurs.

Handles the :gun_data message when HTTP data is received.

Handles the :gun_response message when an HTTP response is received.

Handles the :gun_ws message when a WebSocket frame is received.

Handles the :gun_upgrade message when a WebSocket upgrade succeeds.

Notifies a callback process of an event.

Functions

handle_connection_down(gun_pid, protocol, reason, state, killed_streams \\ [], unprocessed_streams \\ [])

@spec handle_connection_down(
  pid(),
  atom(),
  term(),
  WebsockexNova.Gun.ConnectionState.t(),
  [reference()] | nil,
  [reference()] | nil
) ::
  {:noreply, WebsockexNova.Gun.ConnectionState.t()}
  | {:noreply, {:reconnect, WebsockexNova.Gun.ConnectionState.t()}}
  | {:stop, term(), WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_down message when a connection is lost.

Parameters

  • gun_pid - The Gun connection PID
  • protocol - The protocol that was in use
  • reason - Reason for the connection loss
  • state - Current connection state
  • killed_streams - List of stream references that were killed
  • unprocessed_streams - List of stream references with unprocessed data

Returns

  • {:noreply, updated_state} - Standard response
  • {:noreply, {:reconnect, updated_state}} - Request reconnection
  • {:stop, reason, updated_state} - Request process termination

handle_connection_up(gun_pid, protocol, state)

@spec handle_connection_up(pid(), atom(), WebsockexNova.Gun.ConnectionState.t()) ::
  {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_up message when a connection is established.

Parameters

  • gun_pid - The Gun connection PID
  • protocol - The protocol that was negotiated
  • state - Current connection state

Returns

{:noreply, updated_state}

handle_error(gun_pid, stream_ref, reason, state)

@spec handle_error(
  pid(),
  reference() | nil,
  term(),
  WebsockexNova.Gun.ConnectionState.t()
) ::
  {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_error message when an error occurs.

Parameters

  • gun_pid - The Gun connection PID
  • stream_ref - Reference to the stream with the error, or nil
  • reason - The error reason
  • state - Current connection state

Returns

{:noreply, updated_state}

handle_http_data(gun_pid, stream_ref, is_fin, data, state)

@spec handle_http_data(
  pid(),
  reference(),
  atom(),
  binary(),
  WebsockexNova.Gun.ConnectionState.t()
) ::
  {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_data message when HTTP data is received.

Parameters

  • gun_pid - The Gun connection PID
  • stream_ref - Reference to the stream
  • is_fin - Whether this is the final message
  • data - The response data
  • state - Current connection state

Returns

{:noreply, updated_state}

handle_http_response(gun_pid, stream_ref, is_fin, status, headers, state)

@spec handle_http_response(
  pid(),
  reference(),
  atom(),
  integer(),
  list(),
  WebsockexNova.Gun.ConnectionState.t()
) :: {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_response message when an HTTP response is received.

Parameters

  • gun_pid - The Gun connection PID
  • stream_ref - Reference to the stream
  • is_fin - Whether this is the final message
  • status - HTTP status code
  • headers - HTTP response headers
  • state - Current connection state

Returns

{:noreply, updated_state}

handle_websocket_frame(gun_pid, stream_ref, frame, state)

@spec handle_websocket_frame(
  pid(),
  reference(),
  tuple() | atom(),
  WebsockexNova.Gun.ConnectionState.t()
) :: {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_ws message when a WebSocket frame is received.

Parameters

  • gun_pid - The Gun connection PID
  • stream_ref - Reference to the stream that received the frame
  • frame - The WebSocket frame
  • state - Current connection state

Returns

{:noreply, updated_state}

handle_websocket_upgrade(gun_pid, stream_ref, headers, state)

@spec handle_websocket_upgrade(
  pid(),
  reference(),
  list(),
  WebsockexNova.Gun.ConnectionState.t()
) ::
  {:noreply, WebsockexNova.Gun.ConnectionState.t()}

Handles the :gun_upgrade message when a WebSocket upgrade succeeds.

Parameters

  • gun_pid - The Gun connection PID
  • stream_ref - Reference to the stream that was upgraded
  • headers - Response headers from the upgrade
  • state - Current connection state

Returns

{:noreply, updated_state}

notify(callback_pid, message)

@spec notify(pid() | nil, term()) :: :ok

Notifies a callback process of an event.

Parameters

  • callback_pid - PID of the process to notify (or nil)
  • message - Message to send

Returns

  • :ok