Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,56 @@ let app = WireframeApp::new()?

## Connection Lifecycle

`WireframeApp` can run callbacks when a connection is opened or closed. The
state produced by `on_connection_setup` is passed to `on_connection_teardown`
when the connection ends.
Protocol callbacks are consolidated under the `WireframeProtocol` trait,
replacing the individual `on_connection_setup`/`on_connection_teardown`
closures. The trait methods are synchronous so the trait remains object safe,
but callbacks can spawn asynchronous tasks when needed. A protocol
implementation registers hooks for connection setup, frame mutation and command
completion. The associated `ProtocolError` type is used by other parts of the
API, such as request handling.

```rust
let app = WireframeApp::new()
.on_connection_setup(|| async { 42u32 })
.on_connection_teardown(|state| async move {
println!("closing with {state}");
pub trait WireframeProtocol: Send + Sync + 'static {
type Frame: FrameLike;
type ProtocolError;

fn on_connection_setup(
&self,
handle: PushHandle<Self::Frame>,
ctx: &mut ConnectionContext,
);

fn before_send(&self, frame: &mut Self::Frame, ctx: &mut ConnectionContext);

fn on_command_end(&self, ctx: &mut ConnectionContext);
}

struct MySqlProtocolImpl;

impl WireframeProtocol for MySqlProtocolImpl {
type Frame = Vec<u8>;
type ProtocolError = ();

fn on_connection_setup(
&self,
handle: PushHandle<Self::Frame>,
_ctx: &mut ConnectionContext,
) {
// Spawn an async task to send a heartbeat after setup
tokio::spawn(async move {
let _ = handle.push_high_priority(b"ping".to_vec()).await;
});
}

fn before_send(&self, _frame: &mut Self::Frame, _ctx: &mut ConnectionContext) {}

fn on_command_end(&self, _ctx: &mut ConnectionContext) {}
}

```

```rust
let app = WireframeApp::new().with_protocol(MySqlProtocolImpl);
```

## Custom Extractors
Expand Down