как отключиться от сервера mqtt с помощью javascript-клиента eclipse paho

У меня есть приложение, в котором я подключаюсь к серверу mqtt я перешел по этой ссылке и сделал некоторые больше модификаций и сделал соединение с именем пользователя и паролем,как отключиться от этого сервера

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Paho} from 'ng2-mqtt/mqttws31';

/*
  Generated class for the MqttService provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class Mqttservice {
  
  client :any;
  message :any;
  topic : any ;
  user:any;
  
 
  	constructor(public http: Http) {}


	connectToMqtt(user,pwd){
		this.client = new Paho.MQTT.Client("iot.eclipse.org",9001,user);
	  	this.client.onConnectionLost = this.onConnectionLost;
		this.client.onMessageArrived = this.onMessageArrived.bind(this);
	  	// connect the client
	  	this.client.connect({onSuccess:this.onConnect.bind(this),userName:user,password:Pwd});
	}

	// called when the client connects
 	onConnect() {
  		 console.log("onConnect");
  	}
 	
 
  	//subscribe to a topic
  	subscribe(topic){
  		this.topic = topic ;
  		this.client.subscribe(this.topic);
      		console.log("subscribed to a topic");
  	}
  	//send a message 
  	publish(topic,msg){
  		this.topic = topic ;
  		this.message = new Paho.MQTT.Message(msg);
  		this.message.destinationName = this.topic;
  		this.client.send(this.message);
 
  	}
  
	// called when the client loses its connection
 	onConnectionLost(responseObject) {
  		console.log("connection is lost");
  		if (responseObject.errorCode !== 0) {
    			console.log("onConnectionLost:"+responseObject.errorMessage);
    		}
  	}

	// called when a message arrives
 	onMessageArrived(message) {
 		  console.log("message is from topic: "+message.destinationName);
 
	}


}

как отключиться от сервера, используя разъединение (), точно так же, как публикация () или подписка (), которые я использовал в примере кода


person Lisa    schedule 31.03.2017    source источник
comment
Пожалуйста, постарайтесь прочитать документ перед публикацией, документация Paho четко указывает, как функция, которая отключит клиент.   -  person hardillb    schedule 31.03.2017
comment
да, я мог бы заглянуть в документацию Paho, спасибо@hardillb   -  person Lisa    schedule 03.04.2017


Ответы (2)


disconnect() {
      console.log("client is disconnecting..");
      this.client.disconnect();
 }

просто вызовите функцию разъединения() в соответствии с документацией Paho.

person Lisa    schedule 03.04.2017
comment
Всегда хорошо, когда кто-то решает свою проблему, а затем находит время, чтобы опубликовать ее здесь в качестве ответа для будущих читателей. Это сработало для меня, спасибо! - person uhoh; 09.12.2018

Ты можешь использовать.

mqttClient.end();

Подробности здесь: https://github.com/mqttjs/MQTT.js/

person sharmag    schedule 28.06.2018