Skip to main content

try_get_websocket_request

Attribute Macro try_get_websocket_request 

Source
#[try_get_websocket_request]
Expand description

Wraps function body with WebSocket stream processing.

This attribute macro generates code that wraps the function body with a check to see if data can be read from a WebSocket stream. The function body is only executed if data is successfully read from the stream.

§Arguments

  • TokenStream: Optional variable name to store the read request data.
  • TokenStream: The function item to be modified

§Returns

Returns a TokenStream containing the modified function with WebSocket stream processing logic.

§Examples

Using no parameters:

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;
    }
}

Using variable name to store request data:

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(request)]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
        let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&request);
        stream.send_list(body_list).await;
    }
}

impl Websocket {
    #[try_get_websocket_request(request)]
    async fn try_get_websocket_request_with_ref_self(&self, stream: &mut Stream, ctx: &mut Context) -> Status {}
}

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