Me and a friend are doing a school project where we use MQTT to establish a connection between a client and a server.
int qos = 0;
//BROKER CONNECTION
String broker = "tcp://localhost:1883";
String PubId = "127.0.0.1";
MemoryPersistence persistence = new MemoryPersistence();
MqttClient sampleClient = new MqttClient(broker, PubId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setConnectionTimeout(60);
connOpts.setKeepAliveInterval(60);
connOpts.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
System.out.println("Connecting to broker: " broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
If im already connected to the broker (using my program), and he tries to establish a connection, my program terminates with an error code 0 (no error) and vice versa (if he is connected and i try to connect he crashes).
However, if someone connects to the Broker via MQTT Explorer (MQTT client) the program doesn't crash.
Is there something wrong in our script?
CodePudding user response:
MQTT Client ids have to be globally unique. In your code you have hard coded the PubId
to "127.0.0.1"
.
This value needs to be different for every instance of the code running.