gossamer/web_socket

Types

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

Constructors

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

A bidirectional, message-oriented network connection to a server.

See WebSocket on MDN.

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) -> Nil

Closes the WebSocket connection. Does nothing if the connection is already closing or closed.

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

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

pub fn extensions(of socket: WebSocket) -> String
pub fn from_url(
  url: url.URL,
) -> Result(WebSocket, js_error.JsError)

Creates a new WebSocket connection to url. Returns an error if url’s scheme is not ws: or wss:.

pub fn from_url_string(
  url: String,
) -> Result(WebSocket, js_error.JsError)

Creates a new WebSocket connection to the URL given as a string. Returns an error if url is not a valid ws: or wss: URL.

Examples

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

Creates a new WebSocket connection to the URL given as a string, with the specified sub-protocols. Returns an error if url is not a valid ws: or wss: URL, or if protocols contains duplicates or invalid entries.

Examples

let assert Ok(ws) = web_socket.from_url_string_with_protocols("ws://localhost:8080", ["json"])
pub fn from_url_with_protocols(
  url: url.URL,
  with protocols: List(String),
) -> Result(WebSocket, js_error.JsError)

Creates a new WebSocket connection to url with the specified sub-protocols. Returns an error if url’s scheme is not ws: or wss:, or if protocols contains duplicates or invalid entries.

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, js_error.JsError)

Sends a Blob through the WebSocket. Returns an error if the connection is still connecting. Data sent after the connection is closing or closed is silently discarded.

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

Sends an ArrayBuffer through the WebSocket. Returns an error if the connection is still connecting. Data sent after the connection is closing or closed is silently discarded.

pub fn send_data_view(
  to socket: WebSocket,
  data data: data_view.DataView,
) -> Result(Nil, js_error.JsError)

Sends a DataView through the WebSocket. Returns an error if the connection is still connecting. Data sent after the connection is closing or closed is silently discarded.

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

Sends a string through the WebSocket. Returns an error if the connection is still connecting. Data sent after the connection is closing or closed is silently discarded.

pub fn send_typed_array(
  to socket: WebSocket,
  data data: typed_array.TypedArray,
) -> Result(Nil, js_error.JsError)

Sends binary data as a TypedArray through the WebSocket. Returns an error if the connection is still connecting. Data sent after the connection is closing or closed is silently discarded.

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