fix(coap): fix the issue the connection with same clientid is not discarded

This commit is contained in:
z8674558 2020-12-17 15:57:38 +09:00
parent f06ec6baaa
commit a564670863
1 changed files with 7 additions and 2 deletions

View File

@ -111,12 +111,12 @@ init({ClientId, Username, Password, Channel}) ->
_ = run_hooks('client.connect', [conninfo(State0)], undefined), _ = run_hooks('client.connect', [conninfo(State0)], undefined),
case emqx_access_control:authenticate(clientinfo(State0)) of case emqx_access_control:authenticate(clientinfo(State0)) of
{ok, _AuthResult} -> {ok, _AuthResult} ->
ok = emqx_cm:discard_session(ClientId),
_ = run_hooks('client.connack', [conninfo(State0), success], undefined), _ = run_hooks('client.connack', [conninfo(State0), success], undefined),
State = State0#state{connected_at = erlang:system_time(millisecond)}, State = State0#state{connected_at = erlang:system_time(millisecond)},
%% TODO: Evict same clientid on other node??
run_hooks('client.connected', [clientinfo(State), conninfo(State)]), run_hooks('client.connected', [clientinfo(State), conninfo(State)]),
erlang:send_after(?ALIVE_INTERVAL, self(), check_alive), erlang:send_after(?ALIVE_INTERVAL, self(), check_alive),
@ -186,6 +186,11 @@ handle_info({shutdown, conflict, {ClientId, NewPid}}, State) ->
?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid]), ?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid]),
{stop, {shutdown, conflict}, State}; {stop, {shutdown, conflict}, State};
handle_info(discard, State) ->
?LOG(warning, "the connection is discarded. " ++
"possibly there is another client with the same clientid", []),
{stop, {shutdown, discarded}, State};
handle_info(kick, State) -> handle_info(kick, State) ->
?LOG(info, "Kicked", []), ?LOG(info, "Kicked", []),
{stop, {shutdown, kick}, State}; {stop, {shutdown, kick}, State};