发送响应
2025/7/17大约 3 分钟hyperlanewebrustusage-introductionsend
提示
hyperlane 框架提供了多种响应发送方法,支持完整 HTTP 响应发送、仅响应体发送,以及连接管理。
stream.try_send和stream.send: 发送完整 HTTP 响应并保留连接。stream.try_send_list和stream.send_list: 批量发送响应体。stream.try_flush和stream.flush: 刷新网络缓冲区。
发送完整 HTTP 响应
try_send 方法
提示
发送完整的 HTTP 响应,发送后 TCP 连接保留。返回 Result 类型,可以进行错误处理。
let data: Vec<u8> = ctx
.get_mut_response()
.set_version(HttpVersion::Http1_1)
.set_status_code(200)
.set_header(SERVER, HYPERLANE)
.set_header(CONTENT_TYPE, TEXT_PLAIN)
.set_body("Hello World")
.build();
if stream.try_send(data).await.is_err() {
stream.set_closed(true);
}send 方法
提示
发送完整的 HTTP 响应,发送后 TCP 连接保留。失败时会 panic。
let data: Vec<u8> = ctx
.get_mut_response()
.set_version(HttpVersion::Http1_1)
.set_status_code(200)
.set_body("Hello World")
.build();
stream.send(data).await;批量发送响应
try_send_list 方法
提示
批量发送多个响应体数据,适用于 WebSocket 帧列表等场景,发送后 TCP 连接保留。返回 Result 类型,可以进行错误处理。
let body: &ResponseBody = ctx.get_response().get_body();
let frame_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
if stream.try_send_list(&frame_list).await.is_err() {
stream.set_closed(true);
}send_list 方法
提示
批量发送多个响应体数据,适用于 WebSocket 帧列表等场景,发送后 TCP 连接保留。失败时会 panic。
let body: &ResponseBody = ctx.get_response().get_body();
let frame_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
stream.send_list(&frame_list).await;刷新缓冲区
try_flush 方法
提示
强制刷新网络缓冲区,确保数据立即发送。返回 Result 类型,可以进行错误处理。
let flush_result: Result<(), ResponseError> = stream.try_flush().await;flush 方法
提示
强制刷新网络缓冲区,确保数据立即发送。失败时会 panic。
stream.flush().await;属性宏写法
提示
hyperlane 框架提供了属性宏来简化响应发送操作,通过 #[try_send] 和 #[send] 属性宏可以自动在函数执行后发送响应。
try_send 属性宏
use hyperlane::*;
use hyperlane_macros::*;
#[route("/try_send")]
struct TrySendRoute;
impl ServerHook for TrySendRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
#[response_body("Hello World")]
#[try_send]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}send 属性宏
use hyperlane::*;
use hyperlane_macros::*;
#[route("/send")]
struct SendRoute;
impl ServerHook for SendRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
#[response_body("Hello World")]
#[send]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}使用 prologue_macros 和 epilogue_macros 组合
提示
prologue_macros 在函数体之前注入代码,epilogue_macros 在函数体之后注入代码。 宏列表中的顺序:prologue_macros 为头部插入顺序(第一个宏是最外层),epilogue_macros 为尾部插入顺序(最后一个宏是最外层)。
use hyperlane::*;
use hyperlane_macros::*;
#[route("/combined")]
struct CombinedRoute;
impl ServerHook for CombinedRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
#[prologue_macros(is_post_method, response_body("combined"), send)]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}try_flush 属性宏
提示
尝试刷新网络缓冲区,确保数据立即发送。返回 Result 类型,可以进行错误处理。
use hyperlane::*;
use hyperlane_macros::*;
#[route("/try_flush")]
struct TryFlushRoute;
impl ServerHook for TryFlushRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
#[response_body("try_flush")]
#[try_send]
#[try_flush]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}flush 属性宏
提示
刷新网络缓冲区,确保数据立即发送。失败时会 panic。
use hyperlane::*;
use hyperlane_macros::*;
#[route("/flush")]
struct FlushRoute;
impl ServerHook for FlushRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
#[response_body("flush")]
#[send]
#[flush]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}