allow sending resposne to specific id

This commit is contained in:
Rusty Striker 2024-10-02 13:04:33 +03:00
parent b4e6a3fe39
commit 1beeb9fdbc
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
5 changed files with 21 additions and 21 deletions

2
db.md
View file

@ -20,8 +20,6 @@
**Games**: Directories with the name of the game, with contents that define the game
// TODO figure out how to implement the game type itself
**Table**: game table
- **Game Settigns**: Game specific settings (might not be needed)

View file

@ -5,8 +5,8 @@
[ ] implement tavern functions
[ ] token drag & drop
[ ] Chat
[ ] Send new chat messages
[ ] recv new chat messages
[x] Send new chat messages
[x] recv new chat messages
[ ] roll a die when chat message requests that
[ ] figure out how to zoom on the mouse instead of the center of the div
[ ] data reqs
@ -16,7 +16,7 @@
[ ] impl different requests
[ ] actual normal login
[ ] allow sending of old info
[ ] chat history
[x] chat history
[ ] send texture (map/token/image)
[ ] force show something
[ ] mouse ping (ideally multiple types)

View file

@ -19,7 +19,7 @@ impl GameServer {
}
}
pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender<api::Response>) {
pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender<(Option<String>, api::Response)>) {
while let Some(req) = msgs.recv().await {
// TODO: do stuff yo!
let (id, req) = req;
@ -44,9 +44,9 @@ impl GameServer {
msg.source = id.clone();
}
self.chat.push((id.clone(), msg.clone()));
_ = broadcast.send(api::Response::Message(msg));
_ = broadcast.send((None, api::Response::Message(msg)));
},
api::Request::Quit => { _ = broadcast.send(api::Response::Quit { id })},
api::Request::Quit => { _ = broadcast.send((None, api::Response::Quit { id }))},
api::Request::Shutdown => todo!(),
api::Request::GetChatHistory { mut amount, from: last_msg } => {
if amount == 0 { amount = self.chat.len(); }
@ -55,7 +55,7 @@ impl GameServer {
.take(amount)
.map(|m| m.1.clone())
.collect();
_ = broadcast.send(api::Response::GetChatHistory(history));
_ = broadcast.send((Some(id), api::Response::GetChatHistory(history)));
},
api::Request::GetLastMessages { mut amount } => {
if amount == 0 { amount = self.chat.len(); }
@ -64,10 +64,10 @@ impl GameServer {
.skip(start)
.map(|m| m.1.clone())
.collect();
_ = broadcast.send(api::Response::GetChatHistory(history));
_ = broadcast.send((Some(id), api::Response::GetChatHistory(history)));
},
}
}
_ = broadcast.send(api::Response::Shutdown);
_ = broadcast.send((None, api::Response::Shutdown));
}
}

View file

@ -22,7 +22,7 @@ async fn main() {
axum::serve(listener, app).await.expect("axum server crashed, yaaaaay (unless i crashed him that yay)");
}
async fn ws_handler(ws: ws::WebSocketUpgrade, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<Response>) -> impl axum::response::IntoResponse {
async fn ws_handler(ws: ws::WebSocketUpgrade, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<(Option<String>, Response)>) -> impl axum::response::IntoResponse {
ws.on_upgrade(|w| handle_socket(w, msend, brecv))
}
@ -50,16 +50,18 @@ async fn socket_receiver(mut recv: SplitStream<ws::WebSocket>, msend: mpsc::Send
}
}
async fn socket_sender(mut send: SplitSink<ws::WebSocket, ws::Message>, mut brecv: broadcast::Receiver<Response>) {
while let Ok(msg) = brecv.recv().await {
let err = send.send(ws::Message::Text(serde_json::to_string(&msg).unwrap())).await.is_err();
if err {
break;
async fn socket_sender(id: String, mut send: SplitSink<ws::WebSocket, ws::Message>, mut brecv: broadcast::Receiver<(Option<String>, Response)>) {
while let Ok((to_id, msg)) = brecv.recv().await {
if to_id.is_none() || to_id.map(|t| t == id).unwrap_or(false) {
let err = send.send(ws::Message::Text(serde_json::to_string(&msg).unwrap())).await.is_err();
if err {
break;
}
}
}
}
async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<Response>) {
async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<(Option<String>, Response)>) {
let mut id: Option<String> = None;
loop {
if let Some(msg) = socket.recv().await {
@ -101,8 +103,8 @@ async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, R
if let Some(id) = id {
println!("Got id for socket: {}", &id);
let (send, recv) = socket.split();
tokio::spawn(socket_receiver(recv, msend, id));
tokio::spawn(socket_sender(send, brecv));
tokio::spawn(socket_receiver(recv, msend, id.clone()));
tokio::spawn(socket_sender(id, send, brecv));
}
println!("Done with so-cat");
}

View file

@ -7,7 +7,7 @@ pub enum Grid {
}
/// Texture directory source and file
pub enum TextureSource {
// TODO: I think os string would better fit, to do later
// TODO: I think os string would better fit
/// Server's shared textures directory
Shared(String),
/// Custom table directory