simple websocket implementation (without auth use)

This commit is contained in:
2025-09-26 02:23:25 +02:00
parent ab0fbbce79
commit 00c5c2bd63
7 changed files with 218 additions and 3 deletions

View File

@@ -1,7 +1,9 @@
use axum::serve;
use axum::Extension;
use axum::extract::{ws::{Message, WebSocket, WebSocketUpgrade}, State};
use jsonwebtoken::{DecodingKey, EncodingKey};
use tokio::net::TcpListener;
use tokio::sync::mpsc;
mod utils;
mod routes;
@@ -9,6 +11,8 @@ mod rooms;
mod chat;
use r2d2::{Pool};
use r2d2_sqlite::SqliteConnectionManager;
use dashmap::DashMap;
use std::sync::Arc;
use crate::utils::db_pool::{HotelPool,AppState};
use routes::create_router;
@@ -16,6 +20,7 @@ use crate::utils::auth::JwtKeys;
#[tokio::main]
async fn main() -> std::io::Result<()> {
@@ -26,9 +31,17 @@ async fn main() -> std::io::Result<()> {
.build(logs_manager)
.expect("Failed to build logs pool");
type UserMap = DashMap<i32, mpsc::UnboundedSender<Message>>;
/// hotel_id → users
type HotelMap = DashMap<i32, Arc<UserMap>>;
/// global map of all hotels
type WsMap = Arc<HotelMap>;
let state = AppState {
hotel_pools,
logs_pool,
ws_map: Arc::new(DashMap::new()),
//jwt_secret: "your_jwt_secret_key s".to_string(), // better: load from env var
};
@@ -38,6 +51,8 @@ async fn main() -> std::io::Result<()> {
decoding: DecodingKey::from_secret(jwt_secret.as_ref()),
};
let app = create_router(state)
.layer(Extension(jwt_keys));