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
- [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
- [ ] handle disconnect correctly
- [x] handle disconnect correctly
- [ ] user accounts
- [ ] retrieve history
- [ ] notifications

View file

@ -1,6 +1,7 @@
#[macro_use]
extern crate serde;
#[allow(unused_imports, dead_code)]
mod prisma;
use std::sync::Arc;
@ -20,7 +21,10 @@ use common::{ClientMessage, Message};
use dashmap::DashMap;
use prisma::PrismaClient;
use serde_json::{json, Value};
use tokio::sync::broadcast::{self, Sender};
use tokio::{
select,
sync::broadcast::{self, Sender},
};
lazy_static::lazy_static! {
static ref AUTH_CHALLENGES: DashMap<String, String> = DashMap::new();
@ -35,7 +39,7 @@ struct AppState {
#[tokio::main]
async fn main() -> Result<()> {
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 {
client: Arc::new(client),
@ -96,13 +100,27 @@ async fn event_stream(
let mut room_rx = room_tx.subscribe();
loop {
let result = room_rx.recv().await.unwrap();
println!("Received message: {result:?}");
let payload = serde_json::to_string(&result).unwrap();
match socket.send(WsMessage::Text(payload)).await {
Ok(_) => {}
Err(err) => {
eprintln!("Error: {err}")
select! {
result = room_rx.recv() => {
let whatever = result.unwrap();
println!("Received message: {whatever:?}");
let payload = serde_json::to_string(&whatever).unwrap();
match socket.send(WsMessage::Text(payload)).await {
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;
}
}
}
}
}