pub struct WebSocketConfig<'a, B: BroadcastTypeTrait> { /* private fields */ }Expand description
Configuration for a WebSocket connection.
This struct encapsulates all necessary parameters for setting up and managing a WebSocket connection, including context, buffer sizes, capacity, broadcast type, and hook handlers for different lifecycle events.
§Type Parameters
B: The type used for broadcast keys, which must implementBroadcastTypeTrait.
Implementations§
Source§impl<'a, B> WebSocketConfig<'a, B>where
B: BroadcastTypeTrait,
impl<'a, B> WebSocketConfig<'a, B>where
B: BroadcastTypeTrait,
Sourcepub fn take_parts(self) -> (&'a mut Stream, &'a mut Context)
pub fn take_parts(self) -> (&'a mut Stream, &'a mut Context)
Hands out the stream and the context together.
The two borrows live in the same configuration, so they have to be taken together: a caller that wants to run a hook needs both, and the borrow checker will not let it take them one at a time.
§Returns
(&'a mut Stream, &'a mut Context)- The stream and the context.
Source§impl<'a, B> WebSocketConfig<'a, B>where
B: BroadcastTypeTrait,
impl<'a, B> WebSocketConfig<'a, B>where
B: BroadcastTypeTrait,
Sourcepub fn set_capacity(self, capacity: Capacity) -> Self
pub fn set_capacity(self, capacity: Capacity) -> Self
Sourcepub fn set_context(self, context: &'a mut Context) -> Self
pub fn set_context(self, context: &'a mut Context) -> Self
Sourcepub fn set_broadcast_type(self, broadcast_type: BroadcastType<B>) -> Self
pub fn set_broadcast_type(self, broadcast_type: BroadcastType<B>) -> Self
Sourcepub fn set_connected_hook<S>(self) -> Selfwhere
S: ServerHook,
pub fn set_connected_hook<S>(self) -> Selfwhere
S: ServerHook,
Sets the connected hook handler.
This hook is executed when the WebSocket connection is established.
§Type Parameters
S: The hook type, which must implementServerHook.
§Returns
The modified WebSocketConfig instance.
§Examples
use hyperlane::{Context, ServerHook, Status, Stream};
use hyperlane_plugin_websocket::WebSocketConfig;
struct MyConnectedHook;
impl ServerHook for MyConnectedHook {
async fn new(_stream: &mut Stream, _ctx: &mut Context) -> Self {
Self
}
async fn handle(self, _stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.set_attribute("connected", true);
Status::Continue
}
}
// Register the hook with the stream and context for this connection.
let mut config: WebSocketConfig<'_, String> = WebSocketConfig::new(&mut stream, &mut ctx)
.set_connected_hook::<MyConnectedHook>();Sourcepub fn set_request_hook<S>(self) -> Selfwhere
S: ServerHook,
pub fn set_request_hook<S>(self) -> Selfwhere
S: ServerHook,
Sets the request hook handler.
This hook is executed when a new request is received on the WebSocket.
§Type Parameters
S: The hook type, which must implementServerHook.
§Returns
The modified WebSocketConfig instance.
§Examples
use hyperlane::{Context, ServerHook, Status, Stream};
use hyperlane_plugin_websocket::WebSocketConfig;
struct MyRequestHook;
impl ServerHook for MyRequestHook {
async fn new(_stream: &mut Stream, _ctx: &mut Context) -> Self {
Self
}
async fn handle(self, _stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.set_attribute("request_received", true);
Status::Continue
}
}
// Register the hook with the stream and context for this connection.
let mut config: WebSocketConfig<'_, String> = WebSocketConfig::new(&mut stream, &mut ctx)
.set_request_hook::<MyRequestHook>();Sourcepub fn set_sended_hook<S>(self) -> Selfwhere
S: ServerHook,
pub fn set_sended_hook<S>(self) -> Selfwhere
S: ServerHook,
Sets the sended hook handler.
This hook is executed after a message has been successfully sent over the WebSocket.
§Type Parameters
S: The hook type, which must implementServerHook.
§Returns
The modified WebSocketConfig instance.
§Examples
use hyperlane::{Context, ServerHook, Status, Stream};
use hyperlane_plugin_websocket::WebSocketConfig;
struct MySendedHook;
impl ServerHook for MySendedHook {
async fn new(_stream: &mut Stream, _ctx: &mut Context) -> Self {
Self
}
async fn handle(self, _stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.set_attribute("message_sent", true);
Status::Continue
}
}
// Register the hook with the stream and context for this connection.
let mut config: WebSocketConfig<'_, String> = WebSocketConfig::new(&mut stream, &mut ctx)
.set_sended_hook::<MySendedHook>();Sourcepub fn set_closed_hook<S>(self) -> Selfwhere
S: ServerHook,
pub fn set_closed_hook<S>(self) -> Selfwhere
S: ServerHook,
Sets the closed hook handler.
This hook is executed when the WebSocket connection is closed.
§Type Parameters
S: The hook type, which must implementServerHook.
§Returns
The modified WebSocketConfig instance.
§Examples
use hyperlane::{Context, ServerHook, Status, Stream};
use hyperlane_plugin_websocket::WebSocketConfig;
struct MyClosedHook;
impl ServerHook for MyClosedHook {
async fn new(_stream: &mut Stream, _ctx: &mut Context) -> Self {
Self
}
async fn handle(self, _stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.set_attribute("closed", true);
Status::Continue
}
}
// Register the hook with the stream and context for this connection.
let mut config: WebSocketConfig<'_, String> = WebSocketConfig::new(&mut stream, &mut ctx)
.set_closed_hook::<MyClosedHook>();