mqtt consumer add ping
This commit is contained in:
parent
7d26801d98
commit
a9bcd368c0
@ -137,6 +137,7 @@ handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
|
|||||||
handle_info({timeout, _, create_consumer}, State = #state{mqtt_props = Props, conn_pid = undefined}) ->
|
handle_info({timeout, _, create_consumer}, State = #state{mqtt_props = Props, conn_pid = undefined}) ->
|
||||||
try
|
try
|
||||||
{ok, ConnPid} = create_consumer(Props),
|
{ok, ConnPid} = create_consumer(Props),
|
||||||
|
erlang:start_timer(10000, self(), ping_ticker),
|
||||||
{noreply, State#state{conn_pid = ConnPid}}
|
{noreply, State#state{conn_pid = ConnPid}}
|
||||||
catch _:Error:Stack ->
|
catch _:Error:Stack ->
|
||||||
lager:warning("[iot_zd_consumer] config: ~p, create consumer get error: ~p, stack: ~p", [Props, Error, Stack]),
|
lager:warning("[iot_zd_consumer] config: ~p, create consumer get error: ~p, stack: ~p", [Props, Error, Stack]),
|
||||||
@ -144,6 +145,14 @@ handle_info({timeout, _, create_consumer}, State = #state{mqtt_props = Props, co
|
|||||||
{noreply, State#state{conn_pid = undefined}}
|
{noreply, State#state{conn_pid = undefined}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
%% 周期ping服务器,避免连接丢失
|
||||||
|
handle_info({timeout, _, ping_ticker}, State = #state{conn_pid = undefined}) ->
|
||||||
|
{noreply, State};
|
||||||
|
handle_info({timeout, _, ping_ticker}, State = #state{conn_pid = ConnPid}) when is_pid(ConnPid) ->
|
||||||
|
erlang:start_timer(10000, self(), ping_ticker),
|
||||||
|
pong = emqtt:ping(ConnPid),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
%% postman进程挂掉时,重新建立新的
|
%% postman进程挂掉时,重新建立新的
|
||||||
handle_info({'EXIT', ConnPid, Reason}, State = #state{conn_pid = ConnPid}) ->
|
handle_info({'EXIT', ConnPid, Reason}, State = #state{conn_pid = ConnPid}) ->
|
||||||
lager:warning("[iot_zd_consumer] consumer exited with reason: ~p", [Reason]),
|
lager:warning("[iot_zd_consumer] consumer exited with reason: ~p", [Reason]),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user