/* * @copyright: Huang Ding * @Author: ding-cx * @Date: 2021-02-21 17:47:08 * @LastEditors: ding-cx * @LastEditTime: 2021-02-21 17:47:51 * @Description: file content */ import Egg from "egg"; import * as mqtt from "mqtt"; import { AppMQTTClient } from "./appMQTTClient"; // import InitAgentMQTT from "./lib/agentMQTTClient"; import { EventEmitter } from "events"; import AgentMQTTClient from "./lib/agentMQTTClient"; import { AppClient } from "./lib/client/AppClient"; declare module "egg" { interface EggAppConfig { /** 可 提供多个mqtt客户端实例的配置 */ mqtt: { /** 是否启用mqtt */ enable: boolean; /** mqtt服务器列表,可以连接多个broker */ clients: mqttClient.IClientOptions[]; /** * 是否在agent上启用单个长连接(app与agent通讯实现交互) * default true */ agent?: boolean; /** * 是否在每个app上都连接mqtt,每个app自己单独处理消息,但注意同一个订阅不能多次执行,需自己处理其中逻辑 * default false * */ app?: boolean; }; } interface Agent { /** agent 进程上管理多个mqtt实例的类 */ mqtt: AgentMQTTClient; } interface Application { /** 挂载mqtt客户端api,注意如果配置mqtt.enable未开启,调用将会报错 */ mqtt: { /** agent连接模式,获取一个app端实例 此实例通过ipc通讯实现交互 */ getAgentInstance(clientId: string): mqttClient.AppMQTTClient; /** 获取app进程本地连接实例,此类是app连接mqtt服务器的 */ getAppInstance(clientId: string): AppClient; }; } namespace mqttClient { /** mqtt 实例 */ type MqttClientType = mqtt.MqttClient & { clientId: string }; /** egg-mqtt封装扩展的客户端连接配置参数 */ interface IClientOptions extends mqtt.IClientOptions { /** 可以提供url参数代替主机、port和path参数选项 */ url?: string; /** 上线通知话题 */ onlineTopic?: string; } /** app ipc 通信发布消息格式 */ interface IPCDataAppPublish { /** 客户端id */ clientId: string; /** 话题 */ topic: string; /** 随机id,用于回调消息对应 发起端携带 */ nanoid: string; /** app进程的id */ pid: number; /** 消息数据 */ message: any; /** mqtt发布所需的配置 */ options?: mqtt.IClientPublishOptions; } /** app ipc 通信订阅话题消息格式 */ interface IPCDataAppSubscribe { /** 客户端id */ clientId: string; /** 话题 */ topic: string; /** 随机id,用于回调消息对应 发起端携带 */ nanoid: string; /** app进程的id */ pid: number; /** mqtt订阅所需的配置 */ options: mqtt.IClientSubscribeOptions; } /** app ipc 通信取消订阅话题消息格式 */ interface IPCDataAppUnsubscribe { /** 客户端id */ clientId: string; /** 话题 */ topic: string; /** 随机id,用于回调消息对应 发起端携带 */ nanoid: string; /** app进程的id */ pid: number; } /** ipc agent 响应app回复发布事件传递的数据 */ interface IPCDataAgentResponseAppPublish { errmsg?: string | null; success: boolean; } /** ipc agent 响应app回复订阅话题传递的数据 */ interface IPCDataAgentResponseAppSubscribe { errmsg?: string | null; success: boolean; granted: mqtt.ISubscriptionGrant[]; } /** ipc agent 响应app回复发布事件传递的数据 */ interface IPCDataAgentResponseAppUnsubscribe { errmsg?: string | null; success: boolean; } /** agent收到消息转发给app */ interface IPCDataAgentTurnToAggMessage { /** 话题 */ topic: string; /** 消息内容,如果是json需app自行parse */ message: string; } /** app挂载的客户端类,与agent通讯的模式,声明来自lib/appMQTTClient.ts */ class AppMQTTClient extends EventEmitter { /** 总的收到消息时触发的回调函数 在每个实例上都应该只调用一次,否则一个消息会被重复触发多次。 */ onMessage(callback: (topic: string, message: any) => void): void; /** * Creates an instance of AppMQTTClient. * * 注意同一个clientId不应该多次实例化,否则你可能多次调用onMessage方法,就会对同一个消息多次接收处理。 * * @param {Application} app app worker对象 * @param {string} clientId mqtt连接的id,由于可以多个mqtt连接,所以必须指定clientId,就是mqtt配置中的clientId * @memberof AppMQTTClient */ constructor(app: Application, clientId: string); /** 获取一个mqtt连接,进行发布订阅接收消息等操作 */ static get(app: Application, clientId: string): AppMQTTClient; /** 发布话题消息 */ publish(topic: string, message: any, options?: mqtt.IClientPublishOptions): Promise; /** 订阅一个话题 */ subscribe(topic: string, options: mqtt.IClientSubscribeOptions): Promise; /** 取消订阅一个话题 */ unsubscribe(topic: string): Promise; /** * 请求响应模型 * 通过一个特殊的messageId载荷进行路由识别 消息体必须为对象,messageId属性内部自动添加 * * @param {string} topic 发布的话题 * @param {{ [key: string]: any }} msg 发布的消息 * @param {mqtt.IClientPublishOptions} [options] mqtt发布选项 * @param {number} [timeout=20000] 等待响应的超时时长 单位ms * @return {*} {Promise<{ topic: string; message: { messageId: string; [key: string]: any } }>} */ fetch( topic: string, msg: { [key: string]: any }, options?: mqtt.IClientPublishOptions, timeout = 20000 ): Promise<{ topic: string; message: { messageId: string } & T }>; on(event: "connect", cb: (data: mqtt.IConnackPacket) => void): void; on(event: "reconnect", cb: (data: any) => void): void; on(event: "error", cb: (data: string) => void): void; on(event: "close", cb: (data: any) => void): void; on(event: "message", cb: (data: mqttClient.IPCDataAgentTurnToAggMessage) => void): void; on(event: string, cb: (data: any) => void): void; } } } export = Egg;