0

mqtt client has same client id。 how to closing old connection? just like this.

New connection from 192.168.3.57 on port 1883.
1479826181: Client paho166768969170988 already connected, closing old connection.
1479826181: Client paho166768969170988 disconnected.
1479826181: New client connected from 192.168.3.57 as paho166768969170988 (c1, k60, u'admin').

EDIT:

 String clientId ="**public_cloud**";
    try {
        MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
        sampleClient.setCallback(new PushCallback());

        MqttTopic mtopic = sampleClient.getTopic(config.getTopic());

        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setConnectionTimeout(100);
        connOpts.setKeepAliveInterval(200);
        connOpts.setUserName(config.getUsername());
        connOpts.setPassword(config.getPassword().toCharArray());
        connOpts.setWill(mtopic, "close".getBytes(), 0, true);

        sampleClient.connect(connOpts);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

if the clientid is same,has error.

ConnectionLost,cause:(32109) - java.io.EOFException

hardillb
  • 54,545
  • 11
  • 67
  • 105
tommy sun
  • 21
  • 1
  • 4

2 Answers2

1

Every connection to the broker MUST have a unique client id. This is part of the MQTT specification. How the broker handles a new connection with an existing client id, but most will disconnect the oldest connection.

The usual fix in the situation you are seeing is to use a randomly generated client id or the current timestamp

long time = new Date().getTime();
String clientId ="public_cloud" + time;
    try {
        MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
        sampleClient.setCallback(new PushCallback());

        MqttTopic mtopic = sampleClient.getTopic(config.getTopic());

        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setConnectionTimeout(100);
        connOpts.setKeepAliveInterval(200);
        connOpts.setUserName(config.getUsername());
        connOpts.setPassword(config.getPassword().toCharArray());
        connOpts.setWill(mtopic, "close".getBytes(), 0, true);

        sampleClient.connect(connOpts);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
0
 String clientId ="**public_cloud**";
    try {
        MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
        sampleClient.setCallback(new PushCallback());

        MqttTopic mtopic = sampleClient.getTopic(config.getTopic());

        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setConnectionTimeout(100);
        connOpts.setKeepAliveInterval(200);
        connOpts.setUserName(config.getUsername());
        connOpts.setPassword(config.getPassword().toCharArray());
        connOpts.setWill(mtopic, "close".getBytes(), 0, true);

        sampleClient.connect(connOpts);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

If the clientId is the same, it has the error:

ConnectionLost,cause:(32109) - java.io.EOFException

jrbedard
  • 3,662
  • 5
  • 30
  • 34
tommy sun
  • 21
  • 1
  • 4
  • You should have edited the question to add this, not added it as an answer. I have added it for you this time. – hardillb Nov 22 '16 at 19:58