Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/the10thwiz/rocket-tungstenite
https://github.com/the10thwiz/rocket-tungstenite
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/the10thwiz/rocket-tungstenite
- Owner: the10thWiz
- License: apache-2.0
- Created: 2021-05-04T22:56:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-05-05T13:48:34.000Z (over 3 years ago)
- Last Synced: 2023-08-14T22:32:44.797Z (about 1 year ago)
- Language: Rust
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rocket-tungstenite
This crate allows [`rocket`](https://github.com/the10thwiz/Rocket/tree/websockets)
servers to accept websocket connections, backed by
[`tungstenite`](https://docs.rs/tungstenite).The `RocketWebsocket` responder implements Rocket's `Responder` trait, and
automatically handles converting the connection into a websocket.
By default, the `RocketWebsocket` will return an `UpgradeRequired` status to the
client if the request cannot be upgraded to a websocket. For most applications,
this is perfectly acceptable. Alternatively, expecially if the route is dynamic
and might not exist, it should return a `Result`, which behaves
exactly as expected.## Rocket support
This crate is only supported by a specific branch of a fork, namely the
websocket branch of [the10thwiz/Rocket](https://github.com/the10thwiz/Rocket/tree/websockets).
Because of this, this crate will not be published until Rocket reaches 5.0
and the http upgrade mechanism is merged into it.## Example
```rust
use rocket::{get, futures::{SinkExt, StreamExt}};
use rocket_tungstenite::{RocketWebsocket, Message};#[get("/socket")]
async fn join_room() -> Websocket {
let (ret, rx) = Websocket::new();
tokio::spawn(async move {
if let Ok(mut ws) = rx.await {
ws.send(Message::text("Example message")).await.expect("Error");
//ws.send("example_message").await?;
let mut ctr: usize = 0;
while let Some(Ok(message)) = ws.next().await {
ws.send(Message::text(format!("Recieved: {}", message))).await.expect("Error");
ctr+= 1;
if ctr > 5 {
return;
}
}
}
});
ret
}
```See [examples/echo.rs] for a full example.
## TODO
[-] Create optional request gaurd that checks headers