1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use actix::{Message, Recipient};
use jamsocket::{ClientId, MessageRecipient};

/// Represents a message or event initiated by a client.
#[derive(Debug, Clone)]
pub enum MessageFromClient {
    /// A client opens a connection to the server.
    Connect(ClientId, Recipient<MessageFromServer>),

    /// A client disconnects from the server (or their connection otherwise drops.)
    Disconnect(ClientId),

    /// A client sends a message.
    Message {
        from_client: ClientId,
        data: MessageData,
    },
}

impl Message for MessageFromClient {
    type Result = ();
}

/// Message received or to be sent over a WebSocket connection, which may be
/// textual or binary.
#[derive(Debug, Clone)]
pub enum MessageData {
    String(String),
    Binary(Vec<u8>),
}

/// Represents a message sent to one or more clients from the server.
#[derive(Debug, Clone)]
pub struct MessageFromServer {
    pub to_client: MessageRecipient,
    pub data: MessageData,
}

impl Message for MessageFromServer {
    type Result = ();
}

impl MessageFromServer {
    #[must_use]
    pub fn new(to_client: MessageRecipient, data: String) -> Self {
        MessageFromServer {
            to_client,
            data: MessageData::String(data),
        }
    }

    #[must_use]
    pub fn new_binary(to_client: MessageRecipient, data: Vec<u8>) -> Self {
        MessageFromServer {
            to_client,
            data: MessageData::Binary(data),
        }
    }
}

/// Represents a request to reserve a client ID and return it. Client IDs are
/// unique only in the context of a room.
///
/// Currently, client IDs are assigned sequentially, but this is an implementation
/// detail and should not be relied on.
pub struct AssignClientId;

impl Message for AssignClientId {
    type Result = ClientId;
}