У меня есть приложение, в котором я подключаюсь к серверу 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);
}
}
как отключиться от сервера, используя разъединение (), точно так же, как публикация () или подписка (), которые я использовал в примере кода