-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
rocket_ws
for WebSocket support
- New feature flag `rocket_sync_db_pools` for compatibility with [`rocket_sync_db_pools`](https://crates.io/crates/rocket_sync_db_pools). - New feature flag `rocket_ws` for compatibility with [`rocket_ws`](https://crates.io/crates/rocket_ws). - Added new example for WebSockets. - Added support for new [`Responder`](https://docs.rs/rocket/0.5.0/rocket/response/trait.Responder.html) types (implemented `OpenApiResponderInner`): - `rocket_ws::Channel<'o>` (when `rocket_ws` feature is enabled) - `rocket_ws::stream::MessageStream<'o, S>` (when `rocket_ws` feature is enabled) - Added support for new [`FromRequest`](https://docs.rs/rocket/0.5.0/rocket/request/trait.FromRequest.html) types (implemented `OpenApiFromRequest`): - `rocket_dyn_templates::Metadata<'r>` (when `rocket_dyn_templates` feature is enabled) - `rocket_sync_db_pools::example::ExampleDb` (when `rocket_sync_db_pools` feature is enabled) - `rocket_ws::WebSocket` (when `rocket_ws` feature is enabled)
- Loading branch information
Showing
11 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/target | ||
**/*.rs.bk | ||
Cargo.lock | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "websocket" | ||
version = "0.1.0" | ||
authors = ["Ralph Bisschops <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rocket = { version = "=0.5.0", default-features = false, features = ["json"] } | ||
rocket_ws = "0.1.0" | ||
rocket_okapi = { path = "../../rocket-okapi", features = [ | ||
"swagger", | ||
"rapidoc", | ||
"rocket_ws", | ||
] } | ||
serde = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use rocket::futures::{SinkExt, StreamExt}; | ||
use rocket::get; | ||
use rocket::response::content::RawHtml; | ||
use rocket_okapi::settings::UrlObject; | ||
use rocket_okapi::{openapi, openapi_get_routes, rapidoc::*, swagger_ui::*}; | ||
|
||
#[openapi] | ||
#[get("/")] | ||
fn test_websocket() -> RawHtml<&'static str> { | ||
RawHtml( | ||
r#" | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
Echo: <input type="text" id="echo_text" name="echo" size="10" /> | ||
<input type="button" value="Send" onclick="echo_send()" /> | ||
<br/> | ||
<br/> | ||
<p id="output"><p> | ||
<script> | ||
// Create WebSocket connection. | ||
const hello_socket = new WebSocket("ws://localhost:8000/hello/bob"); | ||
const echo_socket = new WebSocket("ws://localhost:8000/echo"); | ||
const output = document.getElementById('output'); | ||
// Listen for messages | ||
hello_socket.addEventListener("message", (event) => { | ||
console.log("Hello response: ", event.data); | ||
output.innerHTML += "Hello response: " + event.data + "<br/>"; | ||
}); | ||
echo_socket.addEventListener("message", (event) => { | ||
console.log("Echo response: ", event.data); | ||
output.innerHTML += "Echo response: " + event.data + "<br/>"; | ||
}); | ||
function echo_send(){ | ||
echo_socket.send(document.getElementById('echo_text').value); | ||
} | ||
</script> | ||
</body> | ||
</html> | ||
"#, | ||
) | ||
} | ||
|
||
#[openapi] | ||
#[get("/hello/<name>")] | ||
fn hello(ws: rocket_ws::WebSocket, name: &str) -> rocket_ws::Channel<'_> { | ||
ws.channel(move |mut stream| { | ||
Box::pin(async move { | ||
let message = format!("Hello, {}!", name); | ||
let _ = stream.send(message.into()).await; | ||
Ok(()) | ||
}) | ||
}) | ||
} | ||
|
||
#[openapi] | ||
#[get("/echo")] | ||
fn echo(ws: rocket_ws::WebSocket) -> rocket_ws::Channel<'static> { | ||
ws.channel(move |mut stream| { | ||
Box::pin(async move { | ||
while let Some(message) = stream.next().await { | ||
let _ = stream.send(message?).await; | ||
} | ||
|
||
Ok(()) | ||
}) | ||
}) | ||
} | ||
|
||
#[rocket::main] | ||
async fn main() { | ||
let launch_result = rocket::build() | ||
.mount("/", openapi_get_routes![test_websocket, hello, echo,]) | ||
.mount( | ||
"/swagger-ui/", | ||
make_swagger_ui(&SwaggerUIConfig { | ||
url: "../openapi.json".to_owned(), | ||
..Default::default() | ||
}), | ||
) | ||
.mount( | ||
"/rapidoc/", | ||
make_rapidoc(&RapiDocConfig { | ||
general: GeneralConfig { | ||
spec_urls: vec![UrlObject::new("General", "../openapi.json")], | ||
..Default::default() | ||
}, | ||
hide_show: HideShowConfig { | ||
allow_spec_url_load: false, | ||
allow_spec_file_load: false, | ||
allow_spec_file_download: true, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}), | ||
) | ||
.launch() | ||
.await; | ||
match launch_result { | ||
Ok(_) => println!("Rocket shut down gracefully."), | ||
Err(err) => println!("Rocket had an error: {}", err), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters