Skip to content

Commit

Permalink
Optimize websocket example.
Browse files Browse the repository at this point in the history
  • Loading branch information
gudaoxuri committed Oct 14, 2022
1 parent 18f26cb commit fcca521
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
9 changes: 0 additions & 9 deletions examples/webscoket/config/conf-default.toml

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tardis::web::poem::{get, EndpointExt, Route};
use tardis::TardisFuns;

use crate::processor::ws_broadcast;
use crate::processor::ws_p2p;
use crate::processor::ws_echo;
use crate::processor::Page;

mod processor;
Expand All @@ -23,6 +23,6 @@ async fn main() -> TardisResult<()> {
TardisFuns::init("config").await?;

let mut ws_route = Route::new();
ws_route = ws_route.at("/broadcast/:name", get(ws_broadcast.data(tokio::sync::broadcast::channel::<String>(32).0))).at("/p2p/:name", get(ws_p2p));
ws_route = ws_route.at("/broadcast/:name", get(ws_broadcast.data(tokio::sync::broadcast::channel::<String>(32).0))).at("/echo/:name", get(ws_echo));
TardisFuns::web_server().add_route(Page).await.add_module_raw("ws", ws_route).await.start().await
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Page;

#[poem_openapi::OpenApi]
impl Page {
#[oai(path = "/p2p", method = "get")]
async fn p2p(&self) -> Html<&'static str> {
#[oai(path = "/echo", method = "get")]
async fn echo(&self) -> Html<&'static str> {
Html(
r###"
<body>
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Page {
sendForm.hidden = false;
msgsArea.hidden = false;
msgInput.focus();
ws = new WebSocket("ws://127.0.0.1:8089/ws/p2p/" + nameInput.value);
ws = new WebSocket("ws://127.0.0.1:8089/ws/echo/" + nameInput.value);
ws.onmessage = function(event) {
msgsArea.value += event.data + "\r\n";
}
Expand Down Expand Up @@ -112,6 +112,17 @@ impl Page {
}
}

#[handler]
pub fn ws_echo(Path(name): Path<String>, websocket: WebSocket) -> impl IntoResponse {
websocket.on_upgrade(|mut socket| async move {
while let Some(Ok(Message::Text(text))) = socket.next().await {
if socket.send(Message::Text(format!("{}: {}", name, text))).await.is_err() {
break;
}
}
})
}

#[handler]
pub fn ws_p2p(Path(name): Path<String>, websocket: WebSocket) -> impl IntoResponse {
websocket.on_upgrade(|mut socket| async move {
Expand Down
2 changes: 1 addition & 1 deletion tardis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn main() -> TardisResult<()> {
|-- reldb Relational database usage example
|-- web-basic Web service Usage Example
|-- web-client Web client Usage Example
|-- webscoket WebSocket Usage Example
|-- websocket WebSocket Usage Example
|-- cache Cache Usage Example
|-- mq Message Queue Usage Example
|-- todos A complete project usage example
Expand Down

0 comments on commit fcca521

Please sign in to comment.