如何使用eclipse paho javascript客户端断开与mqtt服务器的连接

如何使用eclipse paho javascript客户端断开与mqtt服务器的连接,mqtt,paho,Mqtt,Paho,我有一个应用程序,在其中我连接到一个mqtt服务器,并进行了更多的修改,并使用用户名和密码建立了连接,以及如何断开与该服务器的连接 从'@angular/core'导入{Injectable}; 从'@angular/Http'导入{Http}; 导入'rxjs/add/operator/map'; 从“ng2 mqtt/mqttws31”导入{Paho}; /* 为MqttService提供程序生成的类。 看见https://angular.io/docs/ts/latest/guide/d

我有一个应用程序,在其中我连接到一个mqtt服务器,并进行了更多的修改,并使用用户名和密码建立了连接,以及如何断开与该服务器的连接

从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http};
导入'rxjs/add/operator/map';
从“ng2 mqtt/mqttws31”导入{Paho};
/*
为MqttService提供程序生成的类。
看见https://angular.io/docs/ts/latest/guide/dependency-injection.html
有关提供者和Angular 2 DI的更多信息。
*/
@可注射()
导出类Mqttservice{
客户:任何;
信息:任何;
主题:任何;
用户:任何;
构造函数(公共http:http){}
connectToMqtt(用户,pwd){
this.client=new Paho.MQTT.client(“iot.eclipse.org”,9001,用户);
this.client.onConnectionLost=this.onConnectionLost;
this.client.onMessageArrived=this.onMessageArrived.bind(this);
//连接客户端
this.client.connect({onSuccess:this.onConnect.bind(this),用户名:user,密码:Pwd});
}
//当客户端连接时调用
onConnect(){
控制台日志(“onConnect”);
}
//订阅主题
订阅(主题){
this.topic=主题;
this.client.subscribe(this.topic);
log(“订阅主题”);
}
//发送消息
发布(主题,msg){
this.topic=主题;
this.message=new Paho.MQTT.message(msg);
this.message.destinationName=this.topic;
this.client.send(this.message);
}
//当客户端失去连接时调用
onConnectionLost(响应对象){
console.log(“连接丢失”);
if(responseObject.errorCode!==0){
日志(“onConnectionLost:+responseObject.errorMessage”);
}
}
//消息到达时调用
onMessageArrived(消息){
日志(“消息来自主题:“+message.destinationName”);
}
}
disconnect(){
日志(“客户端正在断开连接…”);
this.client.disconnect();
}
disconnect(){
日志(“客户端正在断开连接…”);
this.client.disconnect();
}
您可以使用

mqttClient.end()

请在此处查找详细信息: 您可以使用

mqttClient.end()

请在此处查找详细信息:

请在发布之前尝试阅读文档,Paho文档清楚地指出了一个功能将如何断开客户端连接。是的,我可以查看Paho文档thankyou@hardillbPlease试着在发布前阅读文档,Paho文档清楚地指出了一个函数是如何断开客户端的连接的。是的,我本可以查看Paho文档的thankyou@hardillbIt当有人解决了他们自己的问题,然后花时间把它作为一个答案贴在这里给未来的读者,这总是好的。这对我有用,谢谢!当有人解决了他们自己的问题,然后花时间把它作为一个答案发布在这里给未来的读者,这总是好的。这对我有用,谢谢!