forked from lavina/lavina
1
0
Fork 0

send channels on connect

This commit is contained in:
Nikita Vilunov 2023-02-14 19:28:49 +01:00
parent a8d6a98a5b
commit d10cddec61
3 changed files with 27 additions and 3 deletions

View File

@ -49,6 +49,10 @@ impl PlayerConnection {
.join_room(room_id, self.connection_id.clone()) .join_room(room_id, self.connection_id.clone())
.await .await
} }
pub async fn send(&self, command: PlayerCommand) {
self.player_handle.send(command).await;
}
} }
/// Handle to a player actor. /// Handle to a player actor.
@ -149,6 +153,7 @@ pub enum PlayerCommand {
connection_id: ConnectionId, connection_id: ConnectionId,
body: String, body: String,
}, },
GetRooms(Promise<Vec<RoomInfo>>),
/** Events from rooms */ /** Events from rooms */
IncomingMessage { IncomingMessage {
room_id: RoomId, room_id: RoomId,
@ -223,6 +228,7 @@ impl Player {
let (tx, mut rx) = channel(32); let (tx, mut rx) = channel(32);
let handle = PlayerHandle { tx }; let handle = PlayerHandle { tx };
let handle_clone = handle.clone(); let handle_clone = handle.clone();
let mut my_rooms = HashMap::new();
let fiber = tokio::task::spawn(async move { let fiber = tokio::task::spawn(async move {
while let Some(cmd) = rx.recv().await { while let Some(cmd) = rx.recv().await {
match cmd { match cmd {
@ -238,6 +244,7 @@ impl Player {
let mut room = rooms.get_or_create_room(room_id.clone()); let mut room = rooms.get_or_create_room(room_id.clone());
room.subscribe(player_id.clone(), connection_id, handle.clone()) room.subscribe(player_id.clone(), connection_id, handle.clone())
.await; .await;
my_rooms.insert(room_id.clone(), room.clone());
let members = room.get_members().await; let members = room.get_members().await;
promise.send(RoomInfo { promise.send(RoomInfo {
id: room_id, id: room_id,
@ -261,6 +268,13 @@ impl Player {
} }
} }
} }
PlayerCommand::GetRooms(promise) => {
let mut response = vec![];
for (_, handle) in &my_rooms {
response.push(handle.get_room_info().await);
}
promise.send(response);
}
PlayerCommand::IncomingMessage { PlayerCommand::IncomingMessage {
room_id, room_id,
author, author,

View File

@ -8,7 +8,7 @@ use tokio::net::{TcpListener, TcpStream};
use tokio::sync::oneshot::channel; use tokio::sync::oneshot::channel;
use crate::core::player::{ use crate::core::player::{
ConnectionId, PlayerConnection, PlayerHandle, PlayerId, PlayerRegistry, Updates, PlayerConnection, PlayerId, PlayerRegistry, Updates, PlayerCommand,
}; };
use crate::core::room::{RoomId, RoomInfo, RoomRegistry}; use crate::core::room::{RoomId, RoomInfo, RoomRegistry};
use crate::prelude::*; use crate::prelude::*;
@ -206,6 +206,14 @@ async fn handle_registered_socket<'a>(
} }
.write_async(writer) .write_async(writer)
.await?; .await?;
let (promise, deferred) = oneshot();
connection.send(PlayerCommand::GetRooms(promise)).await;
let rooms_list = deferred.await?;
for room in &rooms_list {
produce_on_join_cmd_messages(&config, &user, &Chan::Global(room.id.0.clone()), room, writer).await?;
}
writer.flush().await?; writer.flush().await?;
loop { loop {

View File

@ -114,8 +114,10 @@ fn client_message_user(input: &[u8]) -> IResult<&[u8], ClientMessage> {
let (input, _) = tag("USER ")(input)?; let (input, _) = tag("USER ")(input)?;
let (input, username) = receiver(input)?; let (input, username) = receiver(input)?;
let (input, _) = tag(" ")(input)?; let (input, _) = tag(" ")(input)?;
let (input, _) = take(1_usize)(input)?; // 0 in spec, but any in fact let (input, _) = receiver(input)?;
let (input, _) = tag(" * :")(input)?; let (input, _) = tag(" ")(input)?;
let (input, _) = receiver(input)?;
let (input, _) = tag(" :")(input)?;
let (input, realname) = token(input)?; let (input, realname) = token(input)?;
Ok(( Ok((