Crates.io | mews |
lib.rs | mews |
version | |
source | src |
created_at | 2024-10-29 16:38:31.948798 |
updated_at | 2024-11-08 17:45:02.881868 |
description | Minimal and Efficient, Multi-Environment WebSocket implementation for async Rust |
homepage | https://crates.io/crates/mews |
repository | https://github.com/ohkami-rs/mews |
max_upload_size | |
id | 1427249 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Minimal and Efficient : minimal codebase to provide efficient, memory-safe WebSocket handling.
Multi Environment : tokio
, async-std
, smol
, glommio
are supported as async runtime ( by feature flags of the names ).
MEWS is NOT WebSocket server, just protocol implementation. So :
Tend to be used by web libraries internally, not by end-developers.
Doesn't builtins wss://
support.
[dependencies]
mews = { version = "0.1", features = ["tokio"] }
tokio = { version = "1", features = ["rt"] }
# ...
( with pseudo Request & Response )
/* server */
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| 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 */
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| 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);
}
MEWS is licensed under MIT LICENSE ( LICENSE or https://opensource.org/licenses/MIT ).