Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit12ce3cd

Browse files
authored
Merge pull request#349 from bittcrafter/dev/0.19.0
feat(webhook): add user_properties support to client_connected events
2 parents443ce26 +5697d19 commit12ce3cd

File tree

5 files changed

+97
-42
lines changed

5 files changed

+97
-42
lines changed

‎docs/en_US/web-hook.md‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,32 @@ opts include
246246

247247
**client_connected**
248248

249-
| Key| Type| Description|
250-
|-----------------|---------|---------------------------------------------------|
251-
| action| string| Event name<br>Default: "client_connected"|
252-
| node| integer| Node ID|
253-
| ipaddress| string| Source IP address and port of the client|
254-
| clientid| string| Client ID|
255-
| username| string| Client Username; "undefined" if it doesn't exist|
256-
| keepalive| integer| Requested keep-alive time by the client|
257-
| proto_ver| integer| Protocol version number|
258-
| clean_session| bool| Session persistence flag (MQTT 3.1, 3.1.1)|
259-
| clean_start| bool| Session clean start flag (MQTT 5.0)|
249+
| Key| Type| Description|
250+
|-----------------|---------|---------------------------------------------------------------|
251+
| action| string| Event name<br>Default: "client_connected"|
252+
| node| integer| Node ID|
253+
| ipaddress| string| Source IP address and port of the client|
254+
| clientid| string| Client ID|
255+
| username| string| Client Username; "undefined" if it doesn't exist|
256+
| keepalive| integer| Requested keep-alive time by the client|
257+
| proto_ver| integer| Protocol version number|
258+
| clean_session| bool| Session persistence flag (MQTT 3.1, 3.1.1)|
259+
| clean_start| bool| Session clean start flag (MQTT 5.0)|
260260
| connected_at| integer| Timestamp in milliseconds when the connection was established|
261-
| session_present| bool| Indicates whether a persistent session is present|
262-
| time| string| Hook Information Creation Time, Format: %Y-%m-%d %H:%M:%S%.3f|
261+
| session_present| bool| Indicates whether a persistent session is present|
262+
| user_properties| json| Attach additional contextual information|
263+
| time| string| Hook Information Creation Time, Format: %Y-%m-%d %H:%M:%S%.3f|
264+
265+
user_properties contains
266+
267+
| Key| Type| Description|
268+
|------|--------------------|------------------------|
269+
| name| string or[string]| The property name serves as the key, and its value may be a single string or a string array|
270+
271+
Such as:
272+
```
273+
{"age": "23", "interests": ["reading", "gaming", "running"], "name": "Alice"}
274+
```
263275

264276
**client_disconnected**
265277

‎docs/zh_CN/web-hook.md‎

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,33 @@ opts 包含
249249

250250
**client_connected**
251251

252-
| Key| 类型| 说明|
253-
|-----------------|---------|----------------------------------|
254-
| action| string| 事件名称<br>默认为:"client_connected"|
255-
| node| integer| 节点ID|
256-
| ipaddress| string| 客户端源 IP 地址和端口|
257-
| clientid| string| 客户端 ClientId|
258-
| username| string| 客户端 Username,不存在时该值为 "undefined"|
259-
| keepalive| integer| 客户端申请的心跳保活时间|
260-
| proto_ver| integer| 协议版本号|
261-
| clean_session| bool| 保持会话标记(MQTT 3.1, 3.1.1)|
262-
| clean_start| bool| 连接时清除会话标记(MQTT 5.0)|
263-
| connected_at| integer| 时间戳(毫秒)|
264-
| session_present| bool| 是否持久会话|
252+
| Key| 类型| 说明|
253+
|-----------------|---------|-------------------------------------|
254+
| action| string| 事件名称<br>默认为:"client_connected"|
255+
| node| integer| 节点ID|
256+
| ipaddress| string| 客户端源 IP 地址和端口|
257+
| clientid| string| 客户端 ClientId|
258+
| username| string| 客户端 Username,不存在时该值为 "undefined"|
259+
| keepalive| integer| 客户端申请的心跳保活时间|
260+
| proto_ver| integer| 协议版本号|
261+
| clean_session| bool| 保持会话标记(MQTT 3.1, 3.1.1)|
262+
| clean_start| bool| 连接时清除会话标记(MQTT 5.0)|
263+
| connected_at| integer| 时间戳(毫秒)|
264+
| session_present| bool| 是否持久会话|
265+
| user_properties| json| 用户自定义属性|
265266
| time| string| Hook信息创建时间,格式:%Y-%m-%d %H:%M:%S%.3f|
266267

268+
user_properties 包含
269+
270+
| Key| 类型| 说明|
271+
|------|-------------------|------------------------|
272+
| name| string 或[string]| name为属性名,值:单值为串,多值为串数组|
273+
274+
如:
275+
```
276+
{"age": "23", "interests": ["reading", "gaming", "running"], "name": "Alice"}
277+
```
278+
267279
**client_disconnected**
268280

269281
| Key| 类型| 说明|
@@ -279,15 +291,15 @@ opts 包含
279291

280292
**client_subscribe**
281293

282-
| Key| 类型| 说明|
283-
|-------------| -------|-----|
284-
| action| string| 事件名称<br>默认为:"client_subscribe"|
285-
| node| integer| 节点ID|
286-
| ipaddress| string| 客户端源 IP 地址和端口|
287-
| clientid| string| 客户端 ClientId|
288-
| username| string| 客户端 Username,不存在时该值为 "undefined"|
289-
| topic| string| 订阅的主题|
290-
| opts| json| 订阅参数|
294+
| Key| 类型| 说明|
295+
|-------------| -------|-------------------------------------|
296+
| action| string| 事件名称<br>默认为:"client_subscribe"|
297+
| node| integer| 节点ID|
298+
| ipaddress| string| 客户端源 IP 地址和端口|
299+
| clientid| string| 客户端 ClientId|
300+
| username| string| 客户端 Username,不存在时该值为 "undefined"|
301+
| topic| string| 订阅的主题|
302+
| opts| json| 订阅参数|
291303
| time| string| Hook信息创建时间,格式:%Y-%m-%d %H:%M:%S%.3f|
292304

293305
opts 包含

‎rmqtt-plugins/rmqtt-sys-topic/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl Handler for SystemTopicHandler {
185185
letmut body = session
186186
.connect_info()
187187
.await
188-
.map(|connect_info| connect_info.to_hook_body())
188+
.map(|connect_info| connect_info.to_hook_body(true))
189189
.unwrap_or_default();
190190
ifletSome(obj) = body.as_object_mut(){
191191
obj.insert(

‎rmqtt-plugins/rmqtt-web-hook/src/lib.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,14 @@ impl Handler for WebHookHandler {
515515
let now_time =format_timestamp_millis(now);
516516
let bodys =match param{
517517
Parameter::ClientConnect(conn_info) =>{
518-
letmut body = conn_info.to_hook_body();
518+
letmut body = conn_info.to_hook_body(false);
519519
ifletSome(obj) = body.as_object_mut(){
520520
obj.insert("time".into(), serde_json::Value::String(now_time));
521521
}
522522
Some((None, body))
523523
}
524524
Parameter::ClientConnack(conn_info, conn_ack) =>{
525-
letmut body = conn_info.to_hook_body();
525+
letmut body = conn_info.to_hook_body(false);
526526
ifletSome(obj) = body.as_object_mut(){
527527
obj.insert("conn_ack".into(), serde_json::Value::String(conn_ack.reason().to_string()));
528528
obj.insert("time".into(), serde_json::Value::String(now_time));
@@ -531,7 +531,7 @@ impl Handler for WebHookHandler {
531531
}
532532

533533
Parameter::ClientConnected(session) =>{
534-
letmut body = session.connect_info().await.map(|c| c.to_hook_body()).unwrap_or_default();
534+
letmut body = session.connect_info().await.map(|c| c.to_hook_body(true)).unwrap_or_default();
535535
ifletSome(obj) = body.as_object_mut(){
536536
obj.insert(
537537
"connected_at".into(),

‎rmqtt/src/types.rs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl ConnectInfo {
218218
}
219219

220220
#[inline]
221-
pubfnto_hook_body(&self) -> serde_json::Value{
221+
pubfnto_hook_body(&self,user_properties:bool) -> serde_json::Value{
222222
matchself{
223223
ConnectInfo::V3(id, c) =>{
224224
json!({
@@ -232,15 +232,24 @@ impl ConnectInfo {
232232
})
233233
}
234234
ConnectInfo::V5(id, c) =>{
235-
json!({
235+
letmut body =json!({
236236
"node": id.node(),
237237
"ipaddress": id.remote_addr,
238238
"clientid": id.client_id,
239239
"username": id.username_ref(),
240240
"keepalive": c.keep_alive,
241241
"proto_ver":MQTT_LEVEL_5,
242242
"clean_start": c.clean_start,
243-
})
243+
});
244+
if user_properties && !c.user_properties.is_empty(){
245+
ifletSome(obj) = body.as_object_mut(){
246+
obj.insert(
247+
"user_properties".into(),
248+
json!(serialize_user_properties(&c.user_properties)),
249+
);
250+
}
251+
}
252+
body
244253
}
245254
}
246255
}
@@ -2872,6 +2881,28 @@ impl Default for NodeHealthStatus {
28722881
}
28732882
}
28742883

2884+
#[inline]
2885+
pubfnserialize_user_properties(props:&UserProperties) ->HashMap<&str,Value>{
2886+
letmut map:HashMap<&str,Vec<&str>> =HashMap::default();
2887+
2888+
for(k, v)in props{
2889+
map.entry(k.as_ref()).or_default().push(v.as_ref());
2890+
}
2891+
2892+
letmut result =HashMap::default();
2893+
for(key, values)in map{
2894+
if values.len() ==1{
2895+
result.insert(key, serde_json::Value::String(values[0].to_string()));
2896+
}else{
2897+
let array:Vec<serde_json::Value> =
2898+
values.iter().map(|v| serde_json::Value::String(v.to_string())).collect();
2899+
result.insert(key, serde_json::Value::Array(array));
2900+
}
2901+
}
2902+
2903+
result
2904+
}
2905+
28752906
#[test]
28762907
fntest_reason(){
28772908
assert!(Reason::ConnectKicked(false).is_kicked(false));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp