From ebe31c79d40d7e7b4299d930c09bece30496cdcb Mon Sep 17 00:00:00 2001 From: Turtle Date: Fri, 30 Jul 2021 10:49:47 +0800 Subject: [PATCH] feat(event-topic): Add more fields to disconnected event payload --- apps/emqx_modules/src/emqx_event_topic.erl | 43 +++++++++++++++---- .../test/emqx_event_topic_SUITE.erl | 16 +++---- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/apps/emqx_modules/src/emqx_event_topic.erl b/apps/emqx_modules/src/emqx_event_topic.erl index 93b77a1a8..086e779d7 100644 --- a/apps/emqx_modules/src/emqx_event_topic.erl +++ b/apps/emqx_modules/src/emqx_event_topic.erl @@ -88,17 +88,24 @@ disable() -> %%-------------------------------------------------------------------- on_client_connected(ClientInfo, ConnInfo) -> - Payload = connected_payload(ClientInfo, ConnInfo), + Payload0 = common_infos(ClientInfo, ConnInfo), + Payload = Payload0#{ + connack => 0, %% XXX: connack will be removed in 5.0 + keepalive => maps:get(keepalive, ConnInfo, 0), + clean_start => maps:get(clean_start, ConnInfo, true), + expiry_interval => maps:get(expiry_interval, ConnInfo, 0), + connected_at => maps:get(connected_at, ConnInfo) + }, publish_event_msg(<<"$event/client_connected">>, Payload). -on_client_disconnected(_ClientInfo = #{clientid := ClientId, username := Username}, - Reason, _ConnInfo = #{disconnected_at := DisconnectedAt}) -> - Payload = #{clientid => ClientId, - username => Username, - reason => reason(Reason), - disconnected_at => DisconnectedAt, - ts => erlang:system_time(millisecond) - }, +on_client_disconnected(ClientInfo, + Reason, ConnInfo = #{disconnected_at := DisconnectedAt}) -> + + Payload0 = common_infos(ClientInfo, ConnInfo), + Payload = Payload0#{ + reason => reason(Reason), + disconnected_at => DisconnectedAt + }, publish_event_msg(<<"$event/client_disconnected">>, Payload). on_session_subscribed(_ClientInfo = #{clientid := ClientId, @@ -207,6 +214,24 @@ connected_payload(#{peerhost := PeerHost, ts => erlang:system_time(millisecond) }. +common_infos( + _ClientInfo = #{clientid := ClientId, + username := Username, + peerhost := PeerHost, + sockport := SockPort + }, + _ConnInfo = #{proto_name := ProtoName, + proto_ver := ProtoVer + }) -> + #{clientid => ClientId, + username => Username, + ipaddress => ntoa(PeerHost), + sockport => SockPort, + proto_name => ProtoName, + proto_ver => ProtoVer, + ts => erlang:system_time(millisecond) + }. + make_msg(Topic, Payload) -> emqx_message:set_flag( sys, emqx_message:make( diff --git a/apps/emqx_modules/test/emqx_event_topic_SUITE.erl b/apps/emqx_modules/test/emqx_event_topic_SUITE.erl index 39c34879c..97edd37d4 100644 --- a/apps/emqx_modules/test/emqx_event_topic_SUITE.erl +++ b/apps/emqx_modules/test/emqx_event_topic_SUITE.erl @@ -94,12 +94,12 @@ recv_connected(ClientId) -> {ok, #{qos := ?QOS_0, topic := Topic, payload := Payload}} = receive_publish(100), ?assertMatch(<<"$event/client_connected">>, Topic), ?assertMatch(#{<<"clientid">> := ClientId, - <<"username">> := <<"username">>, - <<"ipaddress">> := <<"127.0.0.1">>, - <<"proto_name">> := <<"MQTT">>, - <<"proto_ver">> := ?MQTT_PROTO_V4, - <<"connack">> := ?RC_SUCCESS, - <<"clean_start">> := true}, emqx_json:decode(Payload, [return_maps])). + <<"username">> := <<"username">>, + <<"ipaddress">> := <<"127.0.0.1">>, + <<"proto_name">> := <<"MQTT">>, + <<"proto_ver">> := ?MQTT_PROTO_V4, + <<"connack">> := ?RC_SUCCESS, + <<"clean_start">> := true}, emqx_json:decode(Payload, [return_maps])). recv_subscribed(_ClientId) -> {ok, #{qos := ?QOS_0, topic := Topic}} = receive_publish(100), @@ -126,8 +126,8 @@ recv_disconnected(ClientId) -> {ok, #{qos := ?QOS_0, topic := Topic, payload := Payload}} = receive_publish(100), ?assertMatch(<<"$event/client_disconnected">>, Topic), ?assertMatch(#{<<"clientid">> := ClientId, - <<"username">> := <<"username">>, - <<"reason">> := <<"normal">>}, emqx_json:decode(Payload, [return_maps])). + <<"username">> := <<"username">>, + <<"reason">> := <<"normal">>}, emqx_json:decode(Payload, [return_maps])). %%-------------------------------------------------------------------- %% Internal functions