cleanup
This commit is contained in:
parent
67e29f2a5d
commit
4c6fbfe66b
2 changed files with 48 additions and 45 deletions
|
@ -1,7 +1,13 @@
|
|||
use crate::meta::*;
|
||||
use crate::AppState;
|
||||
use anyhow::Result;
|
||||
use axum::extract::ws::{Message, WebSocket};
|
||||
use futures::stream::SplitSink;
|
||||
use futures::SinkExt;
|
||||
use lib::*;
|
||||
use rand::prelude::SliceRandom;
|
||||
use serde_json::to_string;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
|
||||
|
@ -35,3 +41,44 @@ pub fn user_client_self_update(new_user: &Arc<RwLock<User>>) -> String {
|
|||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Create, Register, and Hydrate new user
|
||||
pub async fn user_handle_new(
|
||||
sender: &mut SplitSink<WebSocket, Message>,
|
||||
state: &Arc<AppState>,
|
||||
addr: &SocketAddr,
|
||||
) -> Result<()> {
|
||||
// Create
|
||||
let new_user = Arc::new(RwLock::new(User::new(state)));
|
||||
|
||||
// Notify client of new username
|
||||
sender
|
||||
.send(Message::Text(user_client_self_update(&new_user)))
|
||||
.await?;
|
||||
|
||||
// Register using `addr` as key until something longer lived exists
|
||||
state.online_users.write().unwrap().insert(*addr, new_user);
|
||||
|
||||
// Hydrate client
|
||||
// this should probably be combined and sent as one
|
||||
sender.send(Message::Text(meta_chat_update(state))).await?;
|
||||
sender.send(Message::Text(meta_motd())).await?;
|
||||
sender
|
||||
.send(Message::Text(meta_server_summary_update(state)))
|
||||
.await?;
|
||||
sender
|
||||
.send(Message::Text(meta_games_browser_update(state)))
|
||||
.await?;
|
||||
sender
|
||||
.send(Message::Text(meta_new_game_card_packs(state)))
|
||||
.await?;
|
||||
|
||||
// Broadcast new user's existence
|
||||
// this should probably be combined and sent as one
|
||||
state.tx.send(meta_announce_user_join(state, addr))?;
|
||||
state.tx.send(meta_server_summary_update(state))?;
|
||||
state.tx.send(meta_chat_update(state))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -12,14 +12,10 @@ use axum::{
|
|||
},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use futures::stream::SplitSink;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use lib::*;
|
||||
use serde_json::{from_str, to_string};
|
||||
use std::{
|
||||
net::SocketAddr,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use tokio::sync::broadcast::Sender;
|
||||
|
||||
/// Establish the WebSocket connection
|
||||
|
@ -71,46 +67,6 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
|
|||
};
|
||||
}
|
||||
|
||||
/// Create, Register, and Hydrate new user
|
||||
pub async fn user_handle_new(
|
||||
sender: &mut SplitSink<WebSocket, Message>,
|
||||
state: &Arc<AppState>,
|
||||
addr: &SocketAddr,
|
||||
) -> Result<()> {
|
||||
// Create
|
||||
let new_user = Arc::new(RwLock::new(User::new(state)));
|
||||
|
||||
// Notify client of new username
|
||||
sender
|
||||
.send(Message::Text(user_client_self_update(&new_user)))
|
||||
.await?;
|
||||
|
||||
// Register using `addr` as key until something longer lived exists
|
||||
state.online_users.write().unwrap().insert(*addr, new_user);
|
||||
|
||||
// Hydrate client
|
||||
// this should probably be combined and sent as one
|
||||
sender.send(Message::Text(meta_chat_update(state))).await?;
|
||||
sender.send(Message::Text(meta_motd())).await?;
|
||||
sender
|
||||
.send(Message::Text(meta_server_summary_update(state)))
|
||||
.await?;
|
||||
sender
|
||||
.send(Message::Text(meta_games_browser_update(state)))
|
||||
.await?;
|
||||
sender
|
||||
.send(Message::Text(meta_new_game_card_packs(state)))
|
||||
.await?;
|
||||
|
||||
// Broadcast new user's existence
|
||||
// this should probably be combined and sent as one
|
||||
state.tx.send(meta_announce_user_join(state, addr))?;
|
||||
state.tx.send(meta_server_summary_update(state))?;
|
||||
state.tx.send(meta_chat_update(state))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle incoming messages over the WebSocket
|
||||
pub async fn websocket_message_handler(
|
||||
state: Arc<AppState>,
|
||||
|
|
Loading…
Add table
Reference in a new issue