From 3032731a2d6ff4647bf04e65b33bd4499cd4fd87 Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Fri, 9 Aug 2019 15:28:47 +0800 Subject: [PATCH] Publish will msg when abnormal terminate --- src/emqx_protocol.erl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/emqx_protocol.erl b/src/emqx_protocol.erl index d35c85cf5..1831abe12 100644 --- a/src/emqx_protocol.erl +++ b/src/emqx_protocol.erl @@ -265,8 +265,6 @@ handle_in(?DISCONNECT_PACKET(?RC_SUCCESS), PState) -> {stop, normal, PState#protocol{will_msg = undefined}}; handle_in(?DISCONNECT_PACKET(RC), PState = #protocol{proto_ver = Ver}) -> - %% TODO: - %% {stop, {shutdown, abnormal_disconnet}, PState}; {stop, {shutdown, emqx_reason_codes:name(RC, Ver)}, PState}; handle_in(?AUTH_PACKET(), PState) -> @@ -439,9 +437,15 @@ handle_timeout(TRef, Msg, PState = #protocol{session = Session}) -> handle_out({publish, Publishes}, PState#protocol{session = NSession}) end. -terminate(Reason, _PState) -> - io:format("Terminated for ~p~n", [Reason]), - ok. +terminate(normal, _PState) -> + ok; +terminate(_Reason, #protocol{will_msg = WillMsg}) -> + publish_will_msg(WillMsg). + +publish_will_msg(undefined) -> + ok; +publish_will_msg(Msg) -> + emqx_broker:publish(Msg). %%-------------------------------------------------------------------- %% Validate incoming packet