简体   繁体   English

如何关闭与 mqtt 客户端 node.js 的连接

[英]How to close connection with mqtt client node.js

I'm doing a method that works as follows: When I press a button, I want to be continuously getting messages from the topic I'm subscribed to, and when I press another button I want to close connection and not receive any more messages.我正在做一个方法如下:当我按下一个按钮时,我想不断地从我订阅的主题中获取消息,当我按下另一个按钮时,我想关闭连接并且不再接收任何消息. I think the connection closes correctly because the client.on('close') event is executed, however I continue receiving messages from the topic.我认为连接正确关闭是因为执行了 client.on('close') 事件,但是我继续接收来自该主题的消息。 What should I do to not receive any more messages?我该怎么做才能不再收到任何消息?

startStop is a boolean, it is true when I want to receive messages and false when I want to stop receiving them. startStop是一个布尔值,当我想接收消息时为真,当我想停止接收消息时为假。 What am I doing wrong?我究竟做错了什么?

Here's my code:这是我的代码:

mqttClientSubscribeToTraceability(reguladorId, startStop) {
    const tls = (Meteor.settings.mqttConfigClient.mqtt.port === 8883) || Meteor.settings.mqttConfigClient.mqtt.ca;

    const client = mqtt.connect(`${tls ? 'mqtts' : 'mqtt'}://${Meteor.settings.mqttConfigClient.mqtt.host}:${Meteor.settings.mqttConfigClient.mqtt.port}`, 
 { keyPath: Meteor.settings.mqttConfigClient.mqtt.key, 
certPath: Meteor.settings.mqttConfigClient.mqtt.cert, 
ca: [Meteor.settings.mqttConfigClient.mqtt.ca], 
rejectUnauthorized: false 
});

    const topic = `in/1.0/trafficLights/${global.region}/${reguladorId}`;

    const json = {};

    if (startStop) {
      client.on('connect', () => {
        client.subscribe(topic);
      });
      client.on('message', (topic, message) => {
        const msg = JSON.parse(message);
        console.log('msg', msg);
      });   
    } else {
      client.end();
      client.on('close', () => {
        client.unsubscribe(topic);
      });
    }
 }



The problem is that you are creating the connection object every time you call the function and not keeping a persistent reference to it.问题是每次调用函数时都在创建连接对象,而不是保持对它的持久引用。

This means when you call the function with startstop equal to true, you will create a new client object, subscribe to the topic and then attach the message callback.这意味着当你调用startstop等于 true 的函数时,你将创建一个新的客户端对象,订阅主题,然后附加消息回调。 You then throw away the reference to the client object so there is no way to get it back.然后您丢弃对客户端对象的引用,因此无法取回它。

When you call the function with startstop set to false, you still create a new client object, but then immediately close it.当您在startstop设置为 false 的情况下调用该函数时,您仍然会创建一个新的客户端对象,但会立即关闭它。 (Also calling unsubscribe after the client has been closed will achieve nothing except maybe throwing an error). (在客户端关闭后调用取消订阅除了可能抛出错误之外什么也不会实现)。

Given you have another variable (the reguladorId ) you probably need to use an object which uses that as the key to hold separate client instances.鉴于您有另一个变量( reguladorId ),您可能需要使用一个对象,该对象将其用作保存单独客户端实例的键。

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

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