zero4rs/websocket/webchat/input/
actions.rs

1use actix::Message as ActixMessage;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5use bytestring::ByteString;
6
7#[derive(Serialize, Deserialize)]
8pub enum Action {
9    // {"id": "1", "action": "Subscribe", "data": {"channel": "ServerTime"}}
10    // {"id": "2", "action": "Unsubscribe", "data": {"channel": "ServerTime"}}
11    Subscribe,
12    Unsubscribe,
13
14    // {"id": "3", "action": "SendMessage", "data": {"to": "ServerTime", "msg": "add"}}
15    SendMessage, // 发送聊天信息
16}
17
18#[derive(Serialize, Deserialize, ActixMessage)]
19#[rtype(result = "()")]
20pub struct Input {
21    pub id: Option<String>,
22    pub action: Action,
23    pub data: Value,
24}
25
26impl From<Input> for ByteString {
27    fn from(val: Input) -> Self {
28        let json_string = serde_json::to_string(&val).unwrap();
29        let byte_string: ByteString = ByteString::from(json_string);
30
31        byte_string
32    }
33}