-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexample.rs
38 lines (30 loc) · 1.11 KB
/
example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use async_std::prelude::*;
use tide_websockets::{Message, WebSocket};
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
env_logger::init();
let mut app = tide::new();
app.at("/as_middleware")
.with(WebSocket::new(|_request, mut stream| async move {
while let Some(Ok(Message::Text(input))) = stream.next().await {
let output: String = input.chars().rev().collect();
stream
.send_string(format!("{} | {}", &input, &output))
.await?;
}
Ok(())
}))
.get(|_| async move { Ok("this was not a websocket request") });
app.at("/as_endpoint")
.get(WebSocket::new(|_request, mut stream| async move {
while let Some(Ok(Message::Text(input))) = stream.next().await {
let output: String = input.chars().rev().collect();
stream
.send_string(format!("{} | {}", &input, &output))
.await?;
}
Ok(())
}));
app.listen("127.0.0.1:8080").await?;
Ok(())
}