feat(event-topic): Add more fields to disconnected event payload
This commit is contained in:
parent
d6c375ab5b
commit
ebe31c79d4
|
@ -88,17 +88,24 @@ disable() ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
on_client_connected(ClientInfo, ConnInfo) ->
|
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).
|
publish_event_msg(<<"$event/client_connected">>, Payload).
|
||||||
|
|
||||||
on_client_disconnected(_ClientInfo = #{clientid := ClientId, username := Username},
|
on_client_disconnected(ClientInfo,
|
||||||
Reason, _ConnInfo = #{disconnected_at := DisconnectedAt}) ->
|
Reason, ConnInfo = #{disconnected_at := DisconnectedAt}) ->
|
||||||
Payload = #{clientid => ClientId,
|
|
||||||
username => Username,
|
Payload0 = common_infos(ClientInfo, ConnInfo),
|
||||||
reason => reason(Reason),
|
Payload = Payload0#{
|
||||||
disconnected_at => DisconnectedAt,
|
reason => reason(Reason),
|
||||||
ts => erlang:system_time(millisecond)
|
disconnected_at => DisconnectedAt
|
||||||
},
|
},
|
||||||
publish_event_msg(<<"$event/client_disconnected">>, Payload).
|
publish_event_msg(<<"$event/client_disconnected">>, Payload).
|
||||||
|
|
||||||
on_session_subscribed(_ClientInfo = #{clientid := ClientId,
|
on_session_subscribed(_ClientInfo = #{clientid := ClientId,
|
||||||
|
@ -207,6 +214,24 @@ connected_payload(#{peerhost := PeerHost,
|
||||||
ts => erlang:system_time(millisecond)
|
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) ->
|
make_msg(Topic, Payload) ->
|
||||||
emqx_message:set_flag(
|
emqx_message:set_flag(
|
||||||
sys, emqx_message:make(
|
sys, emqx_message:make(
|
||||||
|
|
|
@ -94,12 +94,12 @@ recv_connected(ClientId) ->
|
||||||
{ok, #{qos := ?QOS_0, topic := Topic, payload := Payload}} = receive_publish(100),
|
{ok, #{qos := ?QOS_0, topic := Topic, payload := Payload}} = receive_publish(100),
|
||||||
?assertMatch(<<"$event/client_connected">>, Topic),
|
?assertMatch(<<"$event/client_connected">>, Topic),
|
||||||
?assertMatch(#{<<"clientid">> := ClientId,
|
?assertMatch(#{<<"clientid">> := ClientId,
|
||||||
<<"username">> := <<"username">>,
|
<<"username">> := <<"username">>,
|
||||||
<<"ipaddress">> := <<"127.0.0.1">>,
|
<<"ipaddress">> := <<"127.0.0.1">>,
|
||||||
<<"proto_name">> := <<"MQTT">>,
|
<<"proto_name">> := <<"MQTT">>,
|
||||||
<<"proto_ver">> := ?MQTT_PROTO_V4,
|
<<"proto_ver">> := ?MQTT_PROTO_V4,
|
||||||
<<"connack">> := ?RC_SUCCESS,
|
<<"connack">> := ?RC_SUCCESS,
|
||||||
<<"clean_start">> := true}, emqx_json:decode(Payload, [return_maps])).
|
<<"clean_start">> := true}, emqx_json:decode(Payload, [return_maps])).
|
||||||
|
|
||||||
recv_subscribed(_ClientId) ->
|
recv_subscribed(_ClientId) ->
|
||||||
{ok, #{qos := ?QOS_0, topic := Topic}} = receive_publish(100),
|
{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),
|
{ok, #{qos := ?QOS_0, topic := Topic, payload := Payload}} = receive_publish(100),
|
||||||
?assertMatch(<<"$event/client_disconnected">>, Topic),
|
?assertMatch(<<"$event/client_disconnected">>, Topic),
|
||||||
?assertMatch(#{<<"clientid">> := ClientId,
|
?assertMatch(#{<<"clientid">> := ClientId,
|
||||||
<<"username">> := <<"username">>,
|
<<"username">> := <<"username">>,
|
||||||
<<"reason">> := <<"normal">>}, emqx_json:decode(Payload, [return_maps])).
|
<<"reason">> := <<"normal">>}, emqx_json:decode(Payload, [return_maps])).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
|
|
Loading…
Reference in New Issue