2021-06-12 20:50:30 +02:00
|
|
|
//! Example websocket server.
|
|
|
|
|
//!
|
|
|
|
|
//! Run with
|
|
|
|
|
//!
|
2021-08-02 23:09:09 +02:00
|
|
|
//! ```not_rust
|
2021-08-18 00:49:01 +02:00
|
|
|
//! cargo run -p example-websockets
|
2021-06-12 20:50:30 +02:00
|
|
|
//! ```
|
|
|
|
|
|
2021-07-09 21:36:14 +02:00
|
|
|
use axum::{
|
2021-08-07 17:26:23 +02:00
|
|
|
extract::{
|
|
|
|
|
ws::{Message, WebSocket, WebSocketUpgrade},
|
|
|
|
|
TypedHeader,
|
|
|
|
|
},
|
2021-08-18 00:49:01 +02:00
|
|
|
http::StatusCode,
|
2021-08-07 17:26:23 +02:00
|
|
|
response::IntoResponse,
|
2021-11-16 20:49:07 +01:00
|
|
|
routing::{get, get_service},
|
2021-08-19 22:37:48 +02:00
|
|
|
Router,
|
2021-06-13 11:22:02 +02:00
|
|
|
};
|
2021-06-12 20:50:30 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
use tower_http::{
|
|
|
|
|
services::ServeDir,
|
|
|
|
|
trace::{DefaultMakeSpan, TraceLayer},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
2021-08-05 05:25:03 -04:00
|
|
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
2021-09-12 18:39:43 +03:00
|
|
|
if std::env::var_os("RUST_LOG").is_none() {
|
2021-08-18 00:49:01 +02:00
|
|
|
std::env::set_var("RUST_LOG", "example_websockets=debug,tower_http=debug")
|
2021-08-05 05:25:03 -04:00
|
|
|
}
|
2021-08-05 20:43:03 +03:00
|
|
|
tracing_subscriber::fmt::init();
|
2021-06-12 20:50:30 +02:00
|
|
|
|
|
|
|
|
// build our application with some routes
|
2021-08-19 22:37:48 +02:00
|
|
|
let app = Router::new()
|
2021-10-27 17:52:41 +02:00
|
|
|
.fallback(
|
2021-11-16 20:49:07 +01:00
|
|
|
get_service(
|
2021-08-19 22:37:48 +02:00
|
|
|
ServeDir::new("examples/websockets/assets").append_index_html_on_directories(true),
|
|
|
|
|
)
|
2021-11-17 20:09:58 +01:00
|
|
|
.handle_error(|error: std::io::Error| async move {
|
2021-10-24 19:33:03 +02:00
|
|
|
(
|
2021-08-19 22:37:48 +02:00
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
|
format!("Unhandled internal error: {}", error),
|
2021-10-24 19:33:03 +02:00
|
|
|
)
|
2021-08-19 22:37:48 +02:00
|
|
|
}),
|
2021-08-08 14:30:51 +02:00
|
|
|
)
|
2021-08-19 22:37:48 +02:00
|
|
|
// routes are matched from bottom to top, so we have to put `nest` at the
|
|
|
|
|
// top since it matches all routes
|
|
|
|
|
.route("/ws", get(ws_handler))
|
|
|
|
|
// logging so we can see whats going on
|
|
|
|
|
.layer(
|
|
|
|
|
TraceLayer::new_for_http()
|
|
|
|
|
.make_span_with(DefaultMakeSpan::default().include_headers(true)),
|
|
|
|
|
);
|
2021-06-12 20:50:30 +02:00
|
|
|
|
|
|
|
|
// run it with hyper
|
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
|
|
|
tracing::debug!("listening on {}", addr);
|
2021-08-04 15:38:51 +02:00
|
|
|
axum::Server::bind(&addr)
|
2021-06-19 12:50:33 +02:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2021-06-12 20:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-07 17:26:23 +02:00
|
|
|
async fn ws_handler(
|
|
|
|
|
ws: WebSocketUpgrade,
|
2021-08-02 23:09:09 +02:00
|
|
|
user_agent: Option<TypedHeader<headers::UserAgent>>,
|
2021-08-07 17:26:23 +02:00
|
|
|
) -> impl IntoResponse {
|
2021-08-02 23:09:09 +02:00
|
|
|
if let Some(TypedHeader(user_agent)) = user_agent {
|
|
|
|
|
println!("`{}` connected", user_agent.as_str());
|
|
|
|
|
}
|
2021-07-30 15:19:53 +02:00
|
|
|
|
2021-08-07 17:26:23 +02:00
|
|
|
ws.on_upgrade(handle_socket)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn handle_socket(mut socket: WebSocket) {
|
2021-06-12 20:50:30 +02:00
|
|
|
if let Some(msg) = socket.recv().await {
|
2021-08-02 23:09:09 +02:00
|
|
|
if let Ok(msg) = msg {
|
2022-01-25 23:31:33 +08:00
|
|
|
match msg {
|
|
|
|
|
Message::Text(t) => {
|
|
|
|
|
println!("client send str: {:?}", t);
|
|
|
|
|
}
|
|
|
|
|
Message::Binary(_) => {
|
|
|
|
|
println!("client send binary data");
|
|
|
|
|
}
|
|
|
|
|
Message::Ping(_) => {
|
|
|
|
|
println!("socket ping");
|
|
|
|
|
}
|
|
|
|
|
Message::Pong(_) => {
|
|
|
|
|
println!("socket pong");
|
|
|
|
|
}
|
|
|
|
|
Message::Close(_) => {
|
|
|
|
|
println!("client disconnected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-02 23:09:09 +02:00
|
|
|
} else {
|
|
|
|
|
println!("client disconnected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-12 20:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loop {
|
2021-08-07 19:47:22 +02:00
|
|
|
if socket
|
|
|
|
|
.send(Message::Text(String::from("Hi!")))
|
|
|
|
|
.await
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
2021-08-02 23:09:09 +02:00
|
|
|
println!("client disconnected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-12 20:50:30 +02:00
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
|
|
|
|
|
}
|
|
|
|
|
}
|