5 unstable releases
0.3.2 | Jul 26, 2024 |
---|---|
0.3.1 | Jul 21, 2024 |
0.3.0 | Jul 15, 2024 |
0.2.0 | Jul 15, 2024 |
0.1.0 | Jul 14, 2024 |
#63 in WebSocket
52KB
797 lines
reconnecting-websocket
A wrapper around WebSocket
that reconnects when the socket
drops. Uses Backoff
to determine the delay between reconnects
Features
tracing
- enables thetracing
crate and logs everything it's doingstate-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)
- Implement
TryFrom
forMessage
for your input type- The
TryFrom::Error
type must implementDebug
- The
- Implement
TryFrom<Message>
for your output type- The
TryFrom::Error
type must implementDebug
- The
- Both input and output need to implement
Unpin
and, if using tracing feature,Debug
- Use
SocketBuilder
to set the URL and configure backoff.get_proto_and_host
can help constructing the URL relative to the currentwindow.location
- Call
SocketBuilder::open
to connect the socket. The errorsopen
returns are likely fatal (invalid URL, blocked port), seeWebSocket::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 theSocket
Stream
implementation - The returned
Socket
can then be polled to get incoming messages.Socket::send
can be called to send messages orSocket::get_sender
can be used to get anUnboundedSender
.Socket::close
or dropping it will drop the innerWebSocket
which sends a close frame and cleans up the event handlers
Example
Docs
License
Dependencies
~15MB
~270K SLoC