reconnecting-websocket

Crates.ioreconnecting-websocket
lib.rsreconnecting-websocket
version0.3.2
sourcesrc
created_at2024-07-14 18:39:14.703274
updated_at2024-07-26 18:56:01.812144
descriptionA wrapper around gloo::net::websocket::futures::WebSocket that reconnects when the socket drops. Uses exponential-backoff::Backoff to determine the delay between reconnects
homepagehttps://github.com/cs2dsb/reconnecting-websocket.rs
repositoryhttps://github.com/cs2dsb/reconnecting-websocket.rs
max_upload_size
id1303183
size66,472
(cs2dsb)

documentation

https://docs.rs/reconnecting-websocket

README

Crate Documentation Workflow Status Maintenance

reconnecting-websocket

A wrapper around WebSocket that reconnects when the socket drops. Uses Backoff to determine the delay between reconnects

Features

  • tracing - enables the tracing crate and logs everything it's doing
  • state-events - changes the Item type of the stream to be an enum that is either a message or a status change Both are enabled by default

Usage

Input means stuff you want to send from this client to the server (Sink)

Outut means stuff you want to receive back from the server (Source)

  1. Implement TryFrom for Message for your input type
  2. Implement TryFrom<Message> for your output type
  3. Both input and output need to implement Unpin and, if using tracing feature, Debug
  4. Use SocketBuilder to set the URL and configure backoff. get_proto_and_host can help constructing the URL relative to the current window.location
  5. Call SocketBuilder::open to connect the socket. The errors open returns are likely fatal (invalid URL, blocked port), see WebSocket::open for details. The first connect is done in the builder so it fails fast if these fatal errors occur but the same kind of error can also occur on any reconnect and be returned by the Socket Stream implementation
  6. The returned Socket can then be polled to get incoming messages. Socket::send can be called to send messages or Socket::get_sender can be used to get an UnboundedSender. Socket::close or dropping it will drop the inner WebSocket which sends a close frame and cleans up the event handlers

Example

tests/reconnect.rs

Docs

Code docs

License

MIT

Commit count: 0

cargo fmt