moquette
moquette copied to clipboard
How to forcefully disconnect the client from the broker?
Expected behavior
In Version 0.11, we could use the below code snippet to disconnect the client forcefully from the broker.
public boolean disconnectClient(String endpoint) { ConnectionDescriptor connectionDescriptor = mqttBroker.getConnectionsManager().getConnection(endpoint); mqttBroker.getConnectionsManager().getConnection(endpoint).abort(); boolean status = mqttBroker.getConnectionsManager().removeConnection(connectionDescriptor); return status; }
Server.java exposed the API to get the ConnectionsManager through which we were able to abort the connection.
public IConnectionsManager getConnectionsManager() { return m_processorBootstrapper.getConnectionDescriptors(); }
Actual behavior
As of now in the version 0.12.1, we are unable to find the similar API to abort the client connection. Could you please help us with the information as to how we can disconnect the client forcefully using any other API that's available.
Hi @giri110890 I plan to solve it, not giving access to the connection manager or internal data structures (like the train wrecks as before), but with just a simple direct method (for example broker, close(clientId))
Hi @andsel Thanks for the quick response. Can you please let me know what's the approximate timeframe when this feature can be implemented?
I don't know when I'll have free time to dedicate to this, sorry
On Mon, Apr 8, 2019 at 5:45 PM giri110890 [email protected] wrote:
Hi @andsel https://github.com/andsel Thanks for the quick response. Can you please let me know what's the approximate timeframe when this feature can be implemented?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/moquette-io/moquette/issues/469#issuecomment-480887442, or mute the thread https://github.com/notifications/unsubscribe-auth/AA_Y0ctodk7Er-BsB0Akf2lNnCZyIcatks5ve2QGgaJpZM4ciB-O .
any further movement on this?
we really need a mechanism for disconnecting a client gracefully.
@abonstu as I wrote in the comment of 8th April the solution could be to provide an API as a close(clientId)
method at broker level. If you want you could provide a PR
Hi there. I have a need for this right now. So, I want to get a PR in for this. I think I see a fairly easy way to do it when exposing a close(clientId)
method on the Server
class as you mentioned.
This is what I was thinking of doing, but I have a couple of questions.
This is not the exact code I would use, but it should get the idea across.
public void close(String clientId) {
sessions.disconnect(clientId);
sessions.remove(clientId);
}
The question I have is that the Session
class has a method closeImmediately()
, which in turn drops the mqtt connection. I was wondering if I should call this first on the session before calling sessions.disconnect()
. The sessions.disconnect()
method just sets the mqttConnection
to null, but does not call closeImmediately
.
Is the code I have sufficient, or should I be closing the mqtt connection before it gets set to null
?
Any help is appreciated. I will probably play around with it and answer my own question, but also looking for feedback.
sessions.disconnect(cliID)
should does a clean disconnect, while the other drop the socket channel
Awesome, thanks!
So, I had to go with this solution. Just doing sessions.disconnect()
was not actually closing the client connection and my client remained connected.
public void close(String clientId) {
final Session session = sessions.retrieve(clientId);
if (session != null) {
session.closeImmediately();
session.disconnect();
}
}