#[ws_from_stream]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.
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: The buffer to read from the WebSocket stream.TokenStream: The function item to be modified
§Returns
Returns a TokenStream containing the modified function with WebSocket stream processing logic.
§Examples
Using no parameters (default buffer size):
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();
}
}Using only request config:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws")]
struct Websocket;
impl ServerHook for Websocket {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(RequestConfig::default())]
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();
}
}Using variable name to store request data:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws")]
struct Websocket;
impl ServerHook for Websocket {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(request)]
async fn handle(self, ctx: &Context) {
let body: &RequestBody = &request.get_body();
let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
ctx.send_body_list_with_data(&body_list).await.unwrap();
}
}Using request config and variable name:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws")]
struct Websocket;
impl ServerHook for Websocket {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(RequestConfig::default(), request)]
async fn handle(self, ctx: &Context) {
let body: &RequestBody = request.get_body();
let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
ctx.send_body_list_with_data(&body_list).await.unwrap();
}
}Using variable name and request config (reversed order):
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws")]
struct Websocket;
impl ServerHook for Websocket {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(request, RequestConfig::default())]
async fn handle(self, ctx: &Context) {
let body: &RequestBody = request.get_body();
let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
ctx.send_body_list_with_data(&body_list).await.unwrap();
}
}
impl Websocket {
#[ws_from_stream(request)]
async fn ws_from_stream_with_ref_self(&self, ctx: &Context) {}
}
#[ws_from_stream]
async fn standalone_ws_from_stream_handler(ctx: &Context) {}