1
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
export { CONNECT_STATUS,MQTT_EVENT_TYPE,MQTT_EVENT_CALL,MQTT_EVENT_PUBLISH,MQTT_SUBSCRIBE,MQTT_EVENTS_CALL,MQTT_PUBLISH_TOPIC,MQTT_CONNECT_OPTS } from "../interface.uts"
|
||||
import Paho from './mqtt.js'
|
||||
// var client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
|
||||
// mqtt测试服务器
|
||||
// https://console.hivemq.cloud/
|
||||
// https://github.com/eclipse/paho.mqtt.javascript
|
||||
// https://eclipse.dev/paho/files/jsdoc/Paho.MQTT.Client.html
|
||||
|
||||
type MqttConnectOptions = {
|
||||
userName?:string,
|
||||
password?:string,
|
||||
useSSL?:boolean,
|
||||
keepAliveInterval?:number,
|
||||
timeout?:number,
|
||||
reconnect?:boolean,
|
||||
onSuccess?:any,
|
||||
onFailure?:any
|
||||
}
|
||||
|
||||
|
||||
export class xMqtt {
|
||||
mqtt : MqttAndroidClient | null = null;
|
||||
mqttConnectOptions : MqttConnectOptions | null = null;
|
||||
connectStatus:CONNECT_STATUS = 'wait'
|
||||
events = new Map<string,MQTT_EVENTS_CALL>();
|
||||
constructor(){
|
||||
|
||||
}
|
||||
/**
|
||||
* @param ulr {string} 连接地址
|
||||
* @param clientIdStr {string} 客户端id
|
||||
* @param username {string|null} 用户名称,如果不需要,设置为null即可
|
||||
* @param password {string|null} 登录密码,如果不需要,设置为null即可
|
||||
*/
|
||||
async create(opts:MQTT_CONNECT_OPTS):xMqtt {
|
||||
let t = this;
|
||||
this.mqttConnectOptions = {} as MqttConnectOptions;
|
||||
this.mqttConnectOptions.userName = opts.userName;
|
||||
this.mqttConnectOptions.password = opts.passWord;
|
||||
this.mqttConnectOptions.useSSL = opts.useSSL;
|
||||
this.mqttConnectOptions.keepAliveInterval = opts.keepAliveInterval;
|
||||
this.mqttConnectOptions.timeout = opts.timeout;
|
||||
|
||||
this.mqttConnectOptions.reconnect = opts.reconnect;
|
||||
this.mqtt = new Paho.Client( `${opts.protocol}${opts.server}:${opts.port}${opts.path}`, opts.clientId);
|
||||
this.mqttConnectOptions.onSuccess = ()=>{
|
||||
t.buildCallEvents('open',null,'连接成功')
|
||||
};
|
||||
this.mqttConnectOptions.onFailure = ()=>{
|
||||
t.buildCallEvents('error',null,'连接失败')
|
||||
};
|
||||
|
||||
this.mqtt.onConnectionLost = ()=>{
|
||||
t.buildCallEvents('dissconnect',null,'连接断开')
|
||||
};
|
||||
this.mqtt.onMessageArrived = (evt)=>{
|
||||
|
||||
t.buildCallEvents('message',evt.topic,evt.payloadString)
|
||||
};
|
||||
this.connectStatus = 'wait'
|
||||
this.mqtt.connect(this.mqttConnectOptions)
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type {MQTT_EVENT_TYPE} 事件名称
|
||||
* @param call {MQTT_EVENT_CALL} 事件回调
|
||||
*/
|
||||
addEventListener(type:MQTT_EVENT_TYPE,call:MQTT_EVENT_CALL):string{
|
||||
let id = Date.now().toString()+(Math.random()*100).toString()
|
||||
this.events.set(id,
|
||||
{
|
||||
type,
|
||||
value:call
|
||||
} as MQTT_EVENTS_CALL
|
||||
)
|
||||
|
||||
return id;
|
||||
}
|
||||
/**
|
||||
* @param id {string} addEventListener返回的事件id
|
||||
*/
|
||||
removeEventListener(id:string):xMqtt{
|
||||
this.events.delete(ids[i])
|
||||
return this;
|
||||
}
|
||||
private buildCallEvents(type:MQTT_EVENT_TYPE,toppic:string|null,str:string){
|
||||
this.events.forEach((value:MQTT_EVENTS_CALL,key:string)=>{
|
||||
if(value.type == type){
|
||||
value.value(type,toppic,str)
|
||||
}
|
||||
})
|
||||
}
|
||||
connect():xMqtt{
|
||||
|
||||
this.mqtt?.connect(this.mqttConnectOptions);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 订阅
|
||||
* @param data {MQTT_SUBSCRIBE[]} 订阅的消息数组
|
||||
*/
|
||||
subscribe(data:MQTT_SUBSCRIBE[]):xMqtt{
|
||||
let t = this;
|
||||
if(this.mqtt == null) return this;
|
||||
for(let i=0;i<data.length;i++){
|
||||
|
||||
this.mqtt.subscribe(data[i].topic,{
|
||||
qos:data[i].qos,
|
||||
onSuccess(){
|
||||
console.log("订阅成功")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送消息
|
||||
* @param message {MQTT_EVENT_PUBLISH}
|
||||
* @param call ()=>void 推送消息成功时的回调【web端永为真】
|
||||
*/
|
||||
publish(message:MQTT_PUBLISH_TOPIC,call:MQTT_EVENT_PUBLISH):xMqtt{
|
||||
if(this.mqtt == null) return this;
|
||||
this.mqtt!.publish(message.topic,message.message,message.qos,true);
|
||||
call(true)
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 取消订阅
|
||||
* @param topics {string[]} 主题数组
|
||||
*/
|
||||
unsubscribe(topics:string[]):xMqtt{
|
||||
let t = this;
|
||||
if(this.mqtt == null||topics.length==0) return this;
|
||||
for(let i=0;i<topics.length;i++){
|
||||
this.mqtt!.unsubscribe(topics[i],{
|
||||
onSuccess(){
|
||||
console.log("取消订阅成功")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
disconnect():xMqtt{
|
||||
if(this.mqtt == null) return this;
|
||||
this.mqtt!.disconnect()
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user