This commit is contained in:
Michael Zhang 2023-12-25 18:06:09 -05:00
parent b5902d67ea
commit e866009491
3 changed files with 32 additions and 11 deletions

1
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -3,9 +3,11 @@
## Roadmap ## Roadmap
- [x] send messages from one client to another - [x] send messages from one client to another
- [ ] save messages to database - [ ] rooms
- [ ] permission model
- [ ] save connection info to local storage db
- [ ] get rid of unwraps - [ ] get rid of unwraps
- [ ] handle disconnect correctly - [x] handle disconnect correctly
- [ ] user accounts - [ ] user accounts
- [ ] retrieve history - [ ] retrieve history
- [ ] notifications - [ ] notifications

View file

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
extern crate serde; extern crate serde;
#[allow(unused_imports, dead_code)]
mod prisma; mod prisma;
use std::sync::Arc; use std::sync::Arc;
@ -20,7 +21,10 @@ use common::{ClientMessage, Message};
use dashmap::DashMap; use dashmap::DashMap;
use prisma::PrismaClient; use prisma::PrismaClient;
use serde_json::{json, Value}; use serde_json::{json, Value};
use tokio::sync::broadcast::{self, Sender}; use tokio::{
select,
sync::broadcast::{self, Sender},
};
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref AUTH_CHALLENGES: DashMap<String, String> = DashMap::new(); static ref AUTH_CHALLENGES: DashMap<String, String> = DashMap::new();
@ -35,7 +39,7 @@ struct AppState {
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let client = PrismaClient::_builder().build().await?; let client = PrismaClient::_builder().build().await?;
let (room_tx, room_rx) = broadcast::channel::<Message>(10_000); let (room_tx, _room_rx) = broadcast::channel::<Message>(10_000);
let state = AppState { let state = AppState {
client: Arc::new(client), client: Arc::new(client),
@ -96,13 +100,27 @@ async fn event_stream(
let mut room_rx = room_tx.subscribe(); let mut room_rx = room_tx.subscribe();
loop { loop {
let result = room_rx.recv().await.unwrap(); select! {
println!("Received message: {result:?}"); result = room_rx.recv() => {
let payload = serde_json::to_string(&result).unwrap(); let whatever = result.unwrap();
match socket.send(WsMessage::Text(payload)).await { println!("Received message: {whatever:?}");
Ok(_) => {} let payload = serde_json::to_string(&whatever).unwrap();
Err(err) => { match socket.send(WsMessage::Text(payload)).await {
eprintln!("Error: {err}") Ok(_) => {}
Err(err) => {
eprintln!("Error: {err}")
}
}
}
socket_read = socket.recv() => {
match socket_read {
Some(_) => {}
None => {
// The client disconnected, handle gracefully
// TODO: Is there any cleanup to do here
break;
}
}
} }
} }
} }