5 releases
new 0.2.0 | Nov 27, 2024 |
---|---|
0.1.4 |
|
0.1.1 | Oct 31, 2024 |
#41 in WebSocket
477 downloads per month
51KB
905 lines
MEWS
Minimal and Efficient, Multi-Environment WebSocket implementation for async RustFeatures
-
Minimal and Efficient : minimal codebase to provide efficient, memory-safe WebSocket handling.
-
Multi Environment :
tokio
,async-std
,smol
,nio
,glommio
are supported as async runtime ( by feature flagsrt_{name}
).
Note
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.
Example
[dependencies]
mews = { version = "0.1", features = ["rt_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);
}
License
MEWS is licensed under MIT LICENSE ( LICENSE or https://opensource.org/licenses/MIT ).
Dependencies
~0.5–12MB
~156K SLoC