#websocket #actix-web #send #stream #helper #sender #response

actix_send_websocket

actix-web框架管理websockets的辅助crate

1个不稳定版本

0.1.0 2020年10月2日

#1272HTTP服务器

每月39次下载
用于 wasm-server

MIT 许可证

33KB
714

actix_send_websocket 是用于 actix-web-v3 管理websockets的辅助crate

示例

use actix_web::{get, App, Error, HttpRequest, HttpServer, Responder};
use actix_send_websocket::{Message, WebSocket};

#[get("/")]
async fn ws(ws: WebSocket) -> impl Responder {
    // stream is the async iterator of incoming client websocket messages.
    // res is the response we return to client.
    // tx is a sender to push new websocket message to client response.
    let (mut stream, res, mut tx) = ws.into_parts();

    // spawn the stream handling so we don't block the response to client.
    actix_web::rt::spawn(async move {
        while let Some(Ok(msg)) = stream.next().await {
            let result = match msg {
                // we echo text message and ping message to client.
                Message::Text(string) => tx.text(string),
                Message::Ping(bytes) => tx.pong(&bytes),
                Message::Close(reason) => {
                    let _ = tx.close(reason);
                    // force end the stream when we have a close message.
                    break;
                }
                // other types of message would be ignored
                _ => Ok(()),
            };
            if result.is_err() {
                // end the stream when the response is gone.
                break;
            }
        }   
    });

    res
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Default behavior of websocket would be 5 seconds interval check for heart beat and 10 seconds for timeout.
    // Server sent ping is disabled by default.
    HttpServer::new(|| App::new().service(ws))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

依赖项

~27MB
~579K SLoC