feat: msg

This commit is contained in:
2021-05-14 07:58:16 +08:00
parent 10bc4d20a2
commit 78fa15b5ec
3 changed files with 102 additions and 1 deletions

View File

@@ -13,6 +13,53 @@ use tokio::net::{TcpListener, TcpStream};
use tungstenite::protocol::Message;
use rust_util::XResult;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum RoomMessageType {
#[serde(rename = "Enter")]
CreateOrEnter,
Exit,
Destroy,
ListPeers,
Broadcast,
Peer,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum RoomMessageDownType {
PeerEnter,
PeerExit,
PeerMessage,
BroadcastMessage,
ReplayMessage,
}
impl Default for RoomMessageDownType {
fn default() -> Self {
Self::ReplayMessage
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RoomMessage {
r#type: RoomMessageType,
room_id: Option<String>,
client_id: Option<String>,
peer_id: Option<String>,
data: Option<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RoomMessageDown {
r#type: RoomMessageDownType,
peer_id: Option<String>,
replay_code: Option<i32>,
replay_message: Option<String>,
data: Option<String>,
}
type Tx = UnboundedSender<Message>;
type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>;
@@ -51,6 +98,19 @@ async fn inner_handle_connection(peer_map: PeerMap, room_map: RoomMap, raw_strea
warning!("Ignore binary message from: {:?} - {:?}", room_id, client_id)
},
Message::Text(msg) => {
let msg = serde_json::from_str::<RoomMessage>(&msg);
match msg {
Err(e) => {
warning!("Parse message: from: {:?} - {:?}, failed: {}", room_id, client_id, e);
let m = RoomMessageDown{
r#type: RoomMessageDownType::PeerMessage,
..Default::default()
};
},
Ok(msg) => {
},
}
// TODO ...
// create/enter room
// exit room