Skip to main content

ws_upgrade_type

Attribute Macro ws_upgrade_type 

Source
#[ws_upgrade_type]
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_upgrade_type")]
struct Websocket;

impl ServerHook for Websocket {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[ws_upgrade_type]
    #[try_get_websocket_request(body)]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
        let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
        stream.send_list(body_list).await;
    }
}

impl Websocket {
    #[ws_upgrade_type]
    async fn ws_with_ref_self(&self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

#[ws_upgrade_type]
async fn standalone_ws_handler(stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }

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