简体   繁体   中英

PAHO js more then one connection not possible?

I connect with PAHO Javascript to my mosquitto broker. If i connect with a second client, the first one will be disconnected. With the timeout of 2 Seconds the c.netions wil be ping back and force, but can this be the reight solution?

var client = new Paho.MQTT.Client("192.168.5.100", 9880, "/", "mybro");  
var reconnectTimeout = 2500;

function connectMqtt(){
  console.log("connecting to mqtt ...");
  try {
    client.connect({
      timeout: 3,
      onSuccess: onConnect,
      useSSL: false,
      userName: "user",
      password: "password",
      keepAliveInterval: 30, 
      reconnect : false
    });
  } catch (error) {
    console.log(error);
  }
  client.onConnectionLost = onConnectionLost;
  client.onMessageArrived = onMessageArrived;
    
}

function onConnect() {
 try {
    client.subscribe("shellies/#");
    client.subscribe("openWB/#");
    console.log("Connected!");  
  } catch (error) {
    console.log(error);
  }
}

function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
    setTimeout(connectMqtt, reconnectTimeout);

  }
}

function onMessageArrived(message) {
  setDataToGui(message.destinationName, message.payloadString);
}

What did I try? Everything, what i have found in inte.net. There should be a problem in my code.
I need to connect with more then one Webbrowser (clients).

var client = new Paho.MQTT.Client("192.168.5.100", 9880, "/", "mybro");  

The problem is the last entry in this function. It is the client ID that must be unique to EVERY client connecting to the broker.

You will need to generate the value most likely as a random number or a very high precision timestamp to limit the chances of 2 clients generating the sme value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM