ws

Attribute Macro ws 

Source
#[ws]
Expand description

Restricts function execution to WebSocket upgrade requests only.

This attribute macro ensures the decorated function only executes when the incoming request is a valid WebSocket upgrade request with proper request headers and protocol negotiation.

ยงUsage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/ws")]
struct Websocket;

impl ServerHook for Websocket {
    async fn new(_ctx: &Context) -> Self {
        Self
    }

    #[ws]
    #[ws_from_stream]
    async fn handle(self, ctx: &Context) {
        let body: RequestBody = ctx.get_request_body().await;
        let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
        ctx.send_body_list_with_data(&body_list).await.unwrap();
    }
}

impl Websocket {
    #[ws]
    async fn ws_with_ref_self(&self, ctx: &Context) {}
}

#[ws]
async fn standalone_ws_handler(ctx: &Context) {}

The macro takes no parameters and should be applied directly to async functions that accept a &Context parameter.