Explorar o código

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

z8674558 %!s(int64=5) %!d(string=hai) anos
pai
achega
a564670863
Modificáronse 1 ficheiros con 7 adicións e 2 borrados
  1. 7 2
      apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl

+ 7 - 2
apps/emqx_coap/src/emqx_coap_mqtt_adapter.erl

@@ -111,12 +111,12 @@ init({ClientId, Username, Password, Channel}) ->
     _ = run_hooks('client.connect', [conninfo(State0)], undefined),
     case emqx_access_control:authenticate(clientinfo(State0)) of
         {ok, _AuthResult} ->
+            ok = emqx_cm:discard_session(ClientId),
+
             _ = run_hooks('client.connack', [conninfo(State0), success], undefined),
 
             State = State0#state{connected_at = erlang:system_time(millisecond)},
 
-            %% TODO: Evict same clientid on other node??
-
             run_hooks('client.connected', [clientinfo(State), conninfo(State)]),
 
             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]),
     {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) ->
     ?LOG(info, "Kicked", []),
     {stop, {shutdown, kick}, State};