feat: v0.1.4, update dependencies, add peer exit message

This commit is contained in:
2023-06-02 01:30:19 +08:00
parent 8f88f834ac
commit 7eb6509ff6
4 changed files with 274 additions and 304 deletions

541
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package]
name = "room-rs"
version = "0.1.3"
version = "0.1.4"
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
edition = "2018"
@@ -10,9 +10,9 @@ edition = "2018"
rust_util = "0.6"
simpledateformat = "0.1"
chrono = "0.4"
tungstenite = {version = "0.13", default-features = false}
tokio-tungstenite = "0.14"
tokio = { version = "1.0", features = ["full"]}
tungstenite = {version = "0.19", default-features = false}
tokio-tungstenite = "0.19"
tokio = { version = "1.28", features = ["full"]}
futures-util = "0.3"
lazy_static = "1.4"
futures-channel = "0.3"

View File

@@ -256,7 +256,7 @@ async fn inner_handle_connection(
Message::Close(_) => {
information!("Close connection: {:?} - {:?} from: {}", handle_context.room_id, handle_context.client_id, addr);
handle_context.peer_map.lock().unwrap().remove(&addr);
client_exit(&handle_context.room_map, &handle_context.room_id, &handle_context.client_id);
client_exit(&handle_context, &handle_context.room_id, &handle_context.client_id);
}
Message::Ping(_) => {}
Message::Pong(_) => {}
@@ -270,6 +270,7 @@ async fn inner_handle_connection(
RoomMessageDown::create_error_reply(&None, format!("Error in process text message: {}", e));
}
},
Message::Frame(_) => {}
}
future::ok(())
@@ -281,7 +282,7 @@ async fn inner_handle_connection(
future::select(broadcast_incoming, receive_from_others).await;
information!("Client disconnected: {}", &addr);
client_exit(&handle_context.room_map, &handle_context.room_id, &handle_context.client_id);
client_exit(&handle_context, &handle_context.room_id, &handle_context.client_id);
handle_context.peer_map.lock().unwrap().remove(&addr);
Ok(())
@@ -292,7 +293,8 @@ fn client_not_in_room(tx: &Tx, addr: SocketAddr) {
RoomMessageDown::create_error_reply(&None, "Client is not in room").send(tx);
}
fn client_exit(room_map: &RoomMap, room_id: &Option<String>, client_id: &Option<String>) {
fn client_exit(handle_context: &HandleContext, room_id: &Option<String>, client_id: &Option<String>) {
let room_map = &handle_context.room_map;
if let (Some(room_id), Some(client_id)) = (room_id, client_id) {
let mut room_map = room_map.lock().unwrap();
if let Some(client_map) = room_map.get_mut(room_id) {
@@ -301,6 +303,15 @@ fn client_exit(room_map: &RoomMap, room_id: &Option<String>, client_id: &Option<
if client_map.is_empty() {
information!("Room is empty, close room: {}", room_id);
room_map.remove(room_id);
} else {
let m = RoomMessageDown::create_peer_exit(&None, client_id.clone());
if let Ok(mm) = serde_json::to_string(&m) {
for (_client_id, client_addr) in client_map {
if let Some(client_tx) = handle_context.peer_map.lock().unwrap().get(client_addr) {
client_tx.send_text(mm.clone());
}
}
}
}
}
}

View File

@@ -87,6 +87,18 @@ impl RoomMessageDown {
}
}
pub fn create_peer_exit<S>(message_id: &Option<String>, peer_id: S) -> Self where S: Into<String> {
let peer_id = peer_id.into();
Self {
r#type: RoomMessageDownType::PeerExit,
request_message_id: message_id.clone(),
reply_code: Some(200),
reply_message: Some(format!("Peer {} exited", peer_id)),
peer_id: Some(peer_id),
..Default::default()
}
}
pub fn create_peer_message<S>(message_id: &Option<String>, peer_id: S, data: Option<String>) -> Self where S: Into<String> {
RoomMessageDown {
r#type: RoomMessageDownType::PeerMessage,