sword_layers/socketio/
config.rs

1use crate::{DisplayConfig, utils::*};
2use console::style;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6#[serde(default)]
7pub struct SocketIoServerConfig {
8    /// Whether to enable the Socket.IO server.
9    /// Defaults to false.
10    pub enabled: bool,
11
12    /// The amount of time the server will wait for an acknowledgement
13    /// from the client before closing the connection.
14    ///
15    /// Defaults to 5 seconds.
16    pub ack_timeout: Option<TimeConfig>,
17
18    /// The amount of time before disconnecting a client that has not
19    /// successfully joined a namespace.
20    ///
21    /// Defaults to 45 seconds.
22    pub connect_timeout: Option<TimeConfig>,
23
24    /// The maximum number of packets that can be buffered per connection
25    /// before being emitted to the client. If the buffer if full the emit()
26    /// method will return an error.
27    ///
28    /// Defaults to 128 packets.
29    pub max_buffer_size: Option<usize>,
30
31    /// The maximum size of a payload in bytes. If a payload is bigger than
32    /// this value the emit() method will return an error.
33    ///
34    /// Defaults to 100 kb.
35    pub max_payload: Option<ByteConfig>,
36
37    /// The interval at which the server will send a ping packet to the client.
38    /// Defaults to 25 seconds.
39    pub ping_interval: Option<TimeConfig>,
40
41    /// The amount of time the server will wait for a ping response from the
42    /// client before closing the connection.
43    ///
44    /// Defaults to 20 seconds.
45    pub ping_timeout: Option<TimeConfig>,
46
47    /// The path to listen for socket.io requests on.
48    /// Defaults to "/socket.io".
49    pub req_path: Option<String>,
50
51    /// The transports to allow for connections.
52    /// Valid options are "polling" and "websocket".
53    pub transports: Option<Vec<String>>,
54
55    /// The parser to use for encoding and decoding messages.
56    /// Valid options are "common" and "msgpack".
57    pub parser: Option<String>,
58
59    /// The size of the read buffer for the websocket transport.
60    /// You can tweak this value depending on your use case.
61    ///
62    /// Defaults to 4KiB.
63    ///
64    /// Setting it to a higher value will improve performance on heavy read scenarios
65    /// but will consume more memory.
66    pub ws_read_buffer_size: Option<usize>,
67
68    /// Whether to display the configuration on startup.
69    pub display: bool,
70}
71
72impl DisplayConfig for SocketIoServerConfig {
73    fn display(&self) {
74        if !self.enabled {
75            return;
76        }
77
78        println!("\n{}", style("Socket.IO Server Configuration:").bold());
79        println!("  ↳  Enabled: {}", self.enabled);
80
81        let mut timeout_parts = Vec::new();
82
83        if let Some(ack) = &self.ack_timeout {
84            timeout_parts.push(format!("ack: {}", ack.raw));
85        }
86        if let Some(connect) = &self.connect_timeout {
87            timeout_parts.push(format!("connect: {}", connect.raw));
88        }
89        if let Some(ping) = &self.ping_timeout {
90            timeout_parts.push(format!("ping: {}", ping.raw));
91        }
92        if !timeout_parts.is_empty() {
93            println!("  ↳  Timeouts: {}", timeout_parts.join(" - "));
94        }
95
96        let mut limit_parts = Vec::new();
97
98        if let Some(buffer) = &self.max_buffer_size {
99            limit_parts.push(format!("buffer: {} packets", buffer));
100        }
101        if let Some(payload) = &self.max_payload {
102            limit_parts.push(format!("payload: {}", payload.raw));
103        }
104        if !limit_parts.is_empty() {
105            println!("  ↳  Limits: {}", limit_parts.join(" - "));
106        }
107
108        let mut connection_parts = Vec::new();
109
110        if let Some(interval) = &self.ping_interval {
111            connection_parts.push(format!("ping interval: {}", interval.raw));
112        }
113        if let Some(path) = &self.req_path {
114            connection_parts.push(format!("path: {}", path));
115        }
116        if let Some(transports) = &self.transports {
117            connection_parts.push(format!("transports: {}", transports.join(", ")));
118        }
119        if let Some(parser) = &self.parser {
120            connection_parts.push(format!("parser: {}", parser));
121        }
122        if !connection_parts.is_empty() {
123            println!("  ↳  Connection: {}", connection_parts.join(" - "));
124        }
125
126        if let Some(ws_size) = &self.ws_read_buffer_size {
127            println!("  ↳  WebSocket: read buffer {} bytes", ws_size);
128        }
129    }
130}