简体   繁体   English

PAHO js 不止一个连接是不可能的?

[英]PAHO js more then one connection not possible?

I connect with PAHO Javascript to my mosquitto broker.我通过泛美卫生组织 Javascript 连接到我的蚊子经纪人。 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?随着 2 秒的超时,c.netions 将被 ping 回并强制执行,但这是否是正确的解决方案?

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.一切,我在 inte.net 上找到的。 There should be a problem in my code.我的代码应该有问题。
I need to connect with more then one Webbrowser (clients).我需要连接多个 Webbrowser(客户端)。

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.问题是这个 function 中的最后一个条目。它是连接到代理的每个客户端必须唯一的客户端 ID。

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.您将需要生成最有可能作为随机数或精度非常高的时间戳的值,以限制 2 个客户端生成 sme 值的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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