feat: ✨ online counter
This commit is contained in:
parent
ab51292439
commit
63bc49ccf6
1 changed files with 7 additions and 1 deletions
|
@ -4,7 +4,7 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use axum::{Router, http::StatusCode, response::IntoResponse, routing::get};
|
use axum::{Json, Router, http::StatusCode, response::IntoResponse, routing::get};
|
||||||
use fastwebsockets::{
|
use fastwebsockets::{
|
||||||
Frame, OpCode, WebSocketError,
|
Frame, OpCode, WebSocketError,
|
||||||
upgrade::{IncomingUpgrade, UpgradeFut},
|
upgrade::{IncomingUpgrade, UpgradeFut},
|
||||||
|
@ -32,6 +32,7 @@ pub async fn start_signalling() {
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.route("/", get(alive))
|
.route("/", get(alive))
|
||||||
.route("/signal", get(signal))
|
.route("/signal", get(signal))
|
||||||
|
.route("/count/online", get(online_counter))
|
||||||
.layer(CorsLayer::permissive());
|
.layer(CorsLayer::permissive());
|
||||||
|
|
||||||
let listener = TcpListener::bind(SERVER_ADDRESS).await.unwrap();
|
let listener = TcpListener::bind(SERVER_ADDRESS).await.unwrap();
|
||||||
|
@ -44,6 +45,11 @@ async fn alive() -> impl IntoResponse {
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn online_counter() -> impl IntoResponse {
|
||||||
|
let online_count = ONLINE_USERS.read().await.len();
|
||||||
|
(StatusCode::OK, Json(online_count))
|
||||||
|
}
|
||||||
|
|
||||||
async fn signal(websocket: IncomingUpgrade) -> impl IntoResponse {
|
async fn signal(websocket: IncomingUpgrade) -> impl IntoResponse {
|
||||||
let (response, websocket) = websocket.upgrade().unwrap();
|
let (response, websocket) = websocket.upgrade().unwrap();
|
||||||
tokio::spawn(websocket_handler(websocket));
|
tokio::spawn(websocket_handler(websocket));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue