pub struct WebSocketConnection { /* private fields */ }websockets and native only.Expand description
WebSocket connection with activity tracking and timeout support
Implementations§
Source§impl WebSocketConnection
impl WebSocketConnection
Sourcepub fn new(id: String, tx: UnboundedSender<Message>) -> WebSocketConnection
pub fn new(id: String, tx: UnboundedSender<Message>) -> WebSocketConnection
Creates a new WebSocket connection with the given ID and sender.
Uses default ConnectionConfig for timeout settings.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("connection_1".to_string(), tx);
assert_eq!(conn.id(), "connection_1");Sourcepub fn with_config(
id: String,
tx: UnboundedSender<Message>,
config: ConnectionConfig,
) -> WebSocketConnection
pub fn with_config( id: String, tx: UnboundedSender<Message>, config: ConnectionConfig, ) -> WebSocketConnection
Creates a new WebSocket connection with the given ID, sender, and configuration.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use reinhardt_websockets::connection::ConnectionConfig;
use tokio::sync::mpsc;
use std::time::Duration;
let (tx, _rx) = mpsc::unbounded_channel();
let config = ConnectionConfig::new()
.with_idle_timeout(Duration::from_secs(60));
let conn = WebSocketConnection::with_config("conn_1".to_string(), tx, config);
assert_eq!(conn.id(), "conn_1");
assert_eq!(conn.config().idle_timeout(), Duration::from_secs(60));Sourcepub fn with_subprotocol(
id: String,
tx: UnboundedSender<Message>,
subprotocol: Option<String>,
) -> WebSocketConnection
pub fn with_subprotocol( id: String, tx: UnboundedSender<Message>, subprotocol: Option<String>, ) -> WebSocketConnection
Creates a new WebSocket connection with the given ID, sender, and subprotocol.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::with_subprotocol(
"connection_1".to_string(),
tx,
Some("chat".to_string())
);
assert_eq!(conn.id(), "connection_1");
assert_eq!(conn.subprotocol(), Some("chat"));Sourcepub fn subprotocol(&self) -> Option<&str>
pub fn subprotocol(&self) -> Option<&str>
Gets the negotiated subprotocol, if any.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::with_subprotocol(
"test".to_string(),
tx,
Some("chat".to_string())
);
assert_eq!(conn.subprotocol(), Some("chat"));Sourcepub fn id(&self) -> &str
pub fn id(&self) -> &str
Gets the connection ID.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test_id".to_string(), tx);
assert_eq!(conn.id(), "test_id");Sourcepub fn config(&self) -> &ConnectionConfig
pub fn config(&self) -> &ConnectionConfig
Gets the connection timeout configuration.
Sourcepub async fn record_activity(&self)
pub async fn record_activity(&self)
Records activity on the connection, resetting the idle timer.
This is called automatically when sending messages, but can also be called manually to indicate that the connection is still active (e.g., when receiving messages from the client).
§Examples
use reinhardt_websockets::WebSocketConnection;
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
conn.record_activity().await;
assert!(!conn.is_idle().await);Sourcepub async fn idle_duration(&self) -> Duration
pub async fn idle_duration(&self) -> Duration
Returns the duration since the last activity on this connection.
§Examples
use reinhardt_websockets::WebSocketConnection;
use tokio::sync::mpsc;
use std::time::Duration;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
let idle = conn.idle_duration().await;
assert!(idle < Duration::from_secs(1));Sourcepub async fn is_idle(&self) -> bool
pub async fn is_idle(&self) -> bool
Checks whether this connection has exceeded its idle timeout.
§Examples
use reinhardt_websockets::WebSocketConnection;
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
// A freshly created connection is not idle
assert!(!conn.is_idle().await);Sourcepub async fn send(&self, message: Message) -> Result<(), WebSocketError>
pub async fn send(&self, message: Message) -> Result<(), WebSocketError>
Sends a message through the WebSocket connection.
Records activity on the connection when a message is sent successfully.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
let message = Message::text("Hello".to_string());
conn.send(message).await.unwrap();
let received = rx.recv().await.unwrap();
assert!(matches!(received, Message::Text { .. }));Sourcepub async fn send_text(&self, text: String) -> Result<(), WebSocketError>
pub async fn send_text(&self, text: String) -> Result<(), WebSocketError>
Sends a text message through the WebSocket connection.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
conn.send_text("Hello World".to_string()).await.unwrap();
let received = rx.recv().await.unwrap();
match received {
Message::Text { data } => assert_eq!(data, "Hello World"),
_ => panic!("Expected text message"),
}Sourcepub async fn send_binary(&self, data: Vec<u8>) -> Result<(), WebSocketError>
pub async fn send_binary(&self, data: Vec<u8>) -> Result<(), WebSocketError>
Sends a binary message through the WebSocket connection.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
let binary_data = vec![0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
conn.send_binary(binary_data.clone()).await.unwrap();
let received = rx.recv().await.unwrap();
match received {
Message::Binary { data } => assert_eq!(data, binary_data),
_ => panic!("Expected binary message"),
}Sourcepub async fn send_json<T>(&self, data: &T) -> Result<(), WebSocketError>where
T: Serialize,
pub async fn send_json<T>(&self, data: &T) -> Result<(), WebSocketError>where
T: Serialize,
Sends a JSON message through the WebSocket connection.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
use serde::Serialize;
#[derive(Serialize)]
struct User {
name: String,
age: u32,
}
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
let user = User { name: "Alice".to_string(), age: 30 };
conn.send_json(&user).await.unwrap();
let received = rx.recv().await.unwrap();
match received {
Message::Text { data } => assert!(data.contains("Alice")),
_ => panic!("Expected text message"),
}Sourcepub async fn close(&self) -> Result<(), WebSocketError>
pub async fn close(&self) -> Result<(), WebSocketError>
Closes the WebSocket connection.
The connection is always marked as closed regardless of whether the close frame could be sent. This ensures resource cleanup even when the underlying channel is already broken.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
conn.close().await.unwrap();
assert!(conn.is_closed().await);Sourcepub async fn close_with_reason(
&self,
code: u16,
reason: String,
) -> Result<(), WebSocketError>
pub async fn close_with_reason( &self, code: u16, reason: String, ) -> Result<(), WebSocketError>
Closes the connection with a custom close code and reason.
The connection is always marked as closed regardless of whether the close frame could be sent. This ensures resource cleanup even when the underlying channel is already broken.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
conn.close_with_reason(1001, "Idle timeout".to_string()).await.unwrap();
assert!(conn.is_closed().await);
let msg = rx.recv().await.unwrap();
match msg {
Message::Close { code, reason } => {
assert_eq!(code, 1001);
assert_eq!(reason, "Idle timeout");
},
_ => panic!("Expected close message"),
}Sourcepub async fn force_close(&self)
pub async fn force_close(&self)
Forces the connection closed without sending a close frame.
Use this for abnormal close paths where the underlying transport is already broken and sending a close frame would fail.
§Examples
use reinhardt_websockets::WebSocketConnection;
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
conn.force_close().await;
assert!(conn.is_closed().await);Sourcepub async fn is_closed(&self) -> bool
pub async fn is_closed(&self) -> bool
Checks if the WebSocket connection is closed.
§Examples
use reinhardt_websockets::{WebSocketConnection, Message};
use tokio::sync::mpsc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = WebSocketConnection::new("test".to_string(), tx);
assert!(!conn.is_closed().await);Auto Trait Implementations§
impl Freeze for WebSocketConnection
impl !RefUnwindSafe for WebSocketConnection
impl Send for WebSocketConnection
impl Sync for WebSocketConnection
impl Unpin for WebSocketConnection
impl UnsafeUnpin for WebSocketConnection
impl !UnwindSafe for WebSocketConnection
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.