gossamer/web_socket

Types

pub type CloseEvent {
  CloseEvent(code: Int, reason: String, was_clean: Bool)
}

Constructors

  • CloseEvent(code: Int, reason: String, was_clean: Bool)

Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

pub type WebSocket

Values

pub fn binary_type(
  of socket: WebSocket,
) -> binary_type.BinaryType
pub fn buffered_amount(of socket: WebSocket) -> Int

Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send but not yet been transmitted to the network.

If the WebSocket connection is closed, this attribute’s value will only increase with each call to the send method. (The number does not reset to zero once the connection closes.)

pub fn close(socket: WebSocket) -> Result(Nil, String)

Closes the WebSocket connection. Returns an error if the connection is already closing or closed.

pub fn close_with(
  socket: WebSocket,
  code code: Int,
  reason reason: String,
) -> Result(Nil, String)

Closes the WebSocket connection with the given code and reason. Returns an error if the code is invalid (must be 1000 or 3000–4999) or the reason exceeds 123 bytes.

pub fn extensions(of socket: WebSocket) -> String
pub fn new(url: String) -> Result(WebSocket, String)

Creates a new WebSocket connection to the given URL.

Examples

let assert Ok(ws) = web_socket.new("ws://localhost:8080")
pub fn new_with_protocols(
  url: String,
  with protocols: List(String),
) -> Result(WebSocket, String)

Creates a new WebSocket connection to the given URL with the specified sub-protocols.

Examples

let assert Ok(ws) = web_socket.new_with_protocols("ws://localhost:8080", ["json"])
pub fn on_close(
  socket: WebSocket,
  run handler: fn(CloseEvent) -> a,
) -> Nil
pub fn on_error(socket: WebSocket, run handler: fn() -> a) -> Nil
pub fn on_message(
  socket: WebSocket,
  run handler: fn(message_event.MessageEvent) -> a,
) -> Nil
pub fn on_open(socket: WebSocket, run handler: fn() -> a) -> Nil
pub fn protocol(of socket: WebSocket) -> String
pub fn ready_state(
  of socket: WebSocket,
) -> ready_state.ReadyState
pub fn send_blob(
  to socket: WebSocket,
  data data: blob.Blob,
) -> Result(Nil, String)

Sends a Blob through the WebSocket. Returns an error if the connection is not open.

pub fn send_buffer(
  to socket: WebSocket,
  data data: array_buffer.ArrayBuffer,
) -> Result(Nil, String)

Sends an ArrayBuffer through the WebSocket. Returns an error if the connection is not open.

pub fn send_bytes(
  to socket: WebSocket,
  data data: uint8_array.Uint8Array,
) -> Result(Nil, String)

Sends binary data as a Uint8Array through the WebSocket. Returns an error if the connection is not open.

pub fn send_string(
  to socket: WebSocket,
  data data: String,
) -> Result(Nil, String)

Sends a string through the WebSocket. Returns an error if the connection is not open.

pub fn set_binary_type(
  of socket: WebSocket,
  to value: binary_type.BinaryType,
) -> Nil
pub fn url(of socket: WebSocket) -> String
Search Document