Skip to content

MEWS - Minimal and Efficient, Multi-Environment WebSocket implementation for async Rust

License

Notifications You must be signed in to change notification settings

TechfaneTechnologies/mews

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MEWS

Minimal and Efficient, Multi-Environment WebSocket implementation for async Rust

License CI status crates.io

Features

  • Minimal and Efficient : minimal codebase to provide efficient, memory-safe WebSocket handling.

  • Multi-Environment : works with any async runtimes with tokio::io or futures::io interface (for example: tokio or nio use tokio::io interface, and smol or glommio use futures::io interface).
    Feature flags io_tokio or io_futures enables integration for respective IO interface.

Usage

io_tokio or io_futures must be activated to integrate with async IO interfaces.

Here specifying io_tokio to use with tokio:

[dependencies]
mews  = { version = "0.5", features = ["io_tokio"] }
tokio = { version = "1", features = ["full"] }
# ...

( with pseudo Request & Response )

/* server */

use tokio::net::TcpStream;
use mews::{WebSocketContext, Connection, Message};

async fn handle_websocket(
    req: &Request/* upgrade request */,
    tcp: TcpStream
) {
    let ctx = WebSocketContext::new(
        &req.headers["Sec-WebSocket-Key"]
    );

    let (sign, ws) = ctx.on_upgrade(
        |mut conn: Connection<TcpStream>| async move {
            while let Ok(Some(Message::Text(text))) = conn.recv().await {
                conn.send(text).await
                    .expect("failed to send message");
                sleep(Duration::from_secs(1)).await;
            }
        }
    );

    send(Response::SwitchingProtocol()
        .with(Connection, "Upgrade")
        .with(Upgrade, "websocket")
        .with(SecWebSocketAccept, sign),
        &mut tcp
    ).await.expect("failed to send handshake response");

    ws.manage(tcp);
}
/* client */

use tokio::net::TcpStream;

async fn start_websocket(
    mut tcp: TcpStream
) {
    let websocket_key = "my-sec-websocket-key";

    let ctx = WebSocketContext::new(
        websocket_key
    );

    let (sign, ws) = ctx.on_upgrade(
        |mut conn: Connection<TcpStream>| async move {
            conn.send("Hello!").await.expect("failed to send message");
            while let Ok(Some(Message::Text(text))) = conn.recv().await {
                println!("got: `{text}`")
            }
        }
    );

    let res = send(Request::GET("/ws")
        .with(Host, "localhost:3000")
        .with(Connection, Upgrade)
        .with(Upgrade, "websocket")
        .with(SecWebSocketVersion, "13")
        .with(SecWebSocketKey, websocket_key),
        &mut tcp
    ).await.expect("failed to send handshake request");

    assert!(res.header(SecWebSocketAccept), Some(sign));

    ws.manage(tcp);
}

License

MEWS is licensed under MIT LICENSE ( LICENSE or https://opensource.org/licenses/MIT ).

About

MEWS - Minimal and Efficient, Multi-Environment WebSocket implementation for async Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%