1个不稳定版本
0.3.0 | 2023年10月19日 |
---|---|
0.2.5 |
|
#195 in WebSocket
每月 57 次下载
19KB
258 行
Actix WS (下一代)
Actix Web的WebSocket,无actor。
使用方法
# Cargo.toml
anyhow = "1"
actix-web = "4"
actix-ws-ng = "0.3"
// main.rs
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_ws::Message;
async fn ws(req: HttpRequest, body: web::Payload) -> Result<HttpResponse, Error> {
let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;
actix_rt::spawn(async move {
while let Some(Ok(msg)) = msg_stream.next().await {
match msg {
Message::Ping(bytes) => {
if session.pong(&bytes).await.is_err() {
return;
}
}
Message::Text(s) => println!("Got text, {}", s),
_ => break,
}
}
let _ = session.close(None).await;
});
Ok(response)
}
#[actix_web::main]
async fn main() -> Result<(), anyhow::Error> {
HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.route("/ws", web::get().to(ws))
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
许可
该项目受以下任一许可协议的约束:
- Apache License, Version 2.0, (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
您可自行选择。
依赖项
~14–24MB
~440K SLoC