#[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("/ws1")]
struct Websocket1;
impl ServerHook for Websocket1 {
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 buffer size:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws5")]
struct Websocket5;
impl ServerHook for Websocket5 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(1024)]
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("/ws2")]
struct Websocket2;
impl ServerHook for Websocket2 {
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 buffer size and variable name:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws3")]
struct Websocket3;
impl ServerHook for Websocket3 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(1024, 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 buffer size (reversed order):
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws4")]
struct Websocket4;
impl ServerHook for Websocket4 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream(request, 1024)]
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();
}
}