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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Socket.IO 协议的报文编解码辅助。
//! Socket.IO protocol packet encoding and decoding helpers.
//!
//! 协议格式(参见 `doc/socket-io-protocol.md`):
//! Protocol format (see `doc/socket-io-protocol.md`):
//!
//! ```text
//! <packet type>[<# of binary attachments>-][<namespace>,][<ack id>]<JSON payload>
//! ```
/// 解析 Socket.IO 报文的 Namespace 和剩余载荷。
/// Parses the Namespace and remaining payload of a Socket.IO packet.
///
/// 按照协议规范,如果载荷以 `/` 开头,则认为指明了 Namespace,并且后面必须跟着 `,` 和具体数据。
/// According to the protocol specification, if the payload starts with `/`, it is considered to specify a Namespace, and must be followed by `,` and the specific data.
///
/// # 示例 (Examples)
/// - `/chat,["event",{...}]` -> `("/chat", "[\"event\",{...}]")`
/// - `["event",{...}]` -> `("/", "[\"event\",{...}]")`
///
/// # 返回值 (Returns)
/// 返回一个元组 `(Namespace 字符串, 剩余的载荷字符串)`。
/// Returns a tuple `(Namespace string, remaining payload string)`.
/// 在解析 Namespace 基础上,继续提取报文中的 Ack ID。
/// On the basis of parsing the Namespace, continues to extract the Ack ID from the packet.
///
/// Socket.IO 允许在 Namespace 后面紧跟一段数字作为 ACK ID(用于实现请求-响应模式),
/// Socket.IO allows a string of numbers to immediately follow the Namespace as an ACK ID (used to implement the request-response pattern),
/// 之后才是真正的 JSON 载荷。
/// followed by the actual JSON payload.
///
/// # 返回值 (Returns)
/// 返回一个元组 `(Namespace 字符串, 提取到的 Ack ID(可选), 剩余的载荷字符串)`。
/// Returns a tuple `(Namespace string, extracted Ack ID (optional), remaining payload string)`.
/// 解析 BINARY_EVENT / BINARY_ACK 报文的附件数量前缀 `<N>-`。
/// Parses the attachment count prefix `<N>-` of a BINARY_EVENT / BINARY_ACK packet.
///
/// 在发送带有二进制附件的消息时,载荷最前面会附加附件的数量 `N` 和一个破折号 `-`。
/// When sending a message with binary attachments, the payload is prefixed with the number of attachments `N` and a dash `-`.
///
/// # 示例 (Examples)
/// - `1-/ns,["evt",{...}]` -> `(1, "/ns,[\"evt\",{...}]")`
/// - `2-["evt",{...},{...}]` -> `(2, "[\"evt\",{...},{...}]")`
///
/// # 返回值 (Returns)
/// 返回一个元组 `(附件数量, 剩余的载荷字符串)`。如果解析不到,返回 `(0, 原始字符串)`。
/// Returns a tuple `(number of attachments, remaining payload string)`. If it cannot be parsed, returns `(0, original string)`.
/// 编码并生成普通的文本事件报文字符串。
/// Encodes and generates a normal text event packet string.
///
/// 格式为 `<类型>[<Namespace>,]<载荷>`。
/// Format is `<type>[<Namespace>,]<payload>`.
///
/// # 参数 (Parameters)
/// * `ns`: Namespace(例如 `/chat` 或 `/`)
/// * `ns`: Namespace (e.g., `/chat` or `/`)
/// * `typ`: 报文类型标识(例如 `"42"` 表示 Socket.IO 事件报文)
/// * `typ`: Packet type identifier (e.g., `"42"` for a Socket.IO event packet)
/// * `payload`: 序列化后的 JSON 字符串
/// * `payload`: Serialized JSON string
/// 编码并生成带 Ack ID 的文本事件报文字符串。
/// Encodes and generates a text event packet string with an Ack ID.
///
/// 格式为 `<类型>[<Namespace>,]<Ack ID><载荷>`。
/// Format is `<type>[<Namespace>,]<Ack ID><payload>`.
///
/// # 参数 (Parameters)
/// * `ns`: Namespace
/// * `typ`: 报文类型标识(例如 `"43"` 表示带 Ack ID 的报文)
/// * `typ`: Packet type identifier (e.g., `"43"` for a packet with Ack ID)
/// * `id`: 期待响应的 Ack ID 数字
/// * `id`: Expected Ack ID number for the response
/// * `payload`: 序列化后的 JSON 字符串
/// * `payload`: Serialized JSON string
/// 编码并生成带二进制附件的事件报文头部。
/// Encodes and generates the header of an event packet with binary attachments.
///
/// 生成类似 `<类型><附件数量>-[<Namespace>,][<Ack ID>]<载荷>` 格式的字符串。
/// Generates a string formatted like `<type><number of attachments>-[<Namespace>,][<Ack ID>]<payload>`.
///
/// # 参数 (Parameters)
/// * `ns`: Namespace
/// * `typ`: 报文类型标识(通常为 `"45"`(BINARY_EVENT) 或 `"46"`(BINARY_ACK))
/// * `typ`: Packet type identifier (usually `"45"`(BINARY_EVENT) or `"46"`(BINARY_ACK))
/// * `num_attachments`: 附件的数量(`N`)
/// * `num_attachments`: Number of attachments (`N`)
/// * `id`: 可选的 Ack ID
/// * `id`: Optional Ack ID
/// * `payload`: 包含对二进制文件引用的 JSON 载荷
/// * `payload`: JSON payload containing references to binary files