@ray-js/wechat-mqtt 0.0.14 → 0.0.15-beta-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.d.ts CHANGED
@@ -1,50 +1,9 @@
1
- declare enum CONNECT_STATE {
2
- CONNECT_BREAK = 0,
3
- CONNECTED = 1,
4
- CONNECTING = 2
5
- }
6
- export interface ConnectOptions {
7
- clientId: string;
8
- username: string;
9
- password: string;
10
- subscribeTopics: string;
11
- keepalive: number;
12
- url: string;
13
- }
14
- export interface MessageData {
15
- topic: string;
16
- payload: {
17
- data: {
18
- bizCode?: string;
19
- devId: string;
20
- status?: Record<string, any>;
21
- bizData?: Record<string, any>;
22
- };
23
- protocol: number;
24
- pv: string;
25
- };
26
- }
27
- export declare type MQTTEventType = 'connect' | 'message' | 'packetsend' | 'packetreceive' | 'error' | 'close' | 'offline' | 'disconnect' | 'end';
1
+ import { Options, Client } from './lib/interface';
2
+ import './lib/adapter';
3
+ export * from './lib/interface';
28
4
  /**
29
- * 连接 mqtt
30
- */
31
- declare const connect: (needReopen?: boolean) => Promise<void>;
32
- /**
33
- * 销毁连接
34
- * @param client
35
- */
36
- declare const destroy: () => void;
37
- /**
38
- * 消息监听
39
- * @param cb
40
- */
41
- declare const onMessage: (cb: (data: MessageData) => void) => void;
42
- declare const offMessage: (cb: (data: MessageData) => void) => void;
43
- declare const onStateChange: (cb: (data: CONNECT_STATE) => void) => void;
44
- declare const offStateChange: (cb: (data: CONNECT_STATE) => void) => void;
45
- /**
46
- * 获取连接状态
5
+ * 创建一个MQTT实例
6
+ * @param options
47
7
  * @returns
48
8
  */
49
- declare const getState: () => boolean;
50
- export { onMessage, offMessage, destroy, connect, getState, onStateChange, offStateChange };
9
+ export declare const createMQTT: (options: Options) => Promise<Client>;
@@ -0,0 +1,2 @@
1
+ import './blowser';
2
+ import './wechat';
@@ -0,0 +1,5 @@
1
+ import { ClientConntroller, AuthOptions } from "./interface";
2
+ /**
3
+ * 取消订阅
4
+ */
5
+ export declare const auth: (client: ClientConntroller, options: AuthOptions) => void;
@@ -1,7 +1,7 @@
1
- import { Client, Options } from "./interface";
1
+ import { ClientConntroller, Options } from "./interface";
2
2
  /**
3
3
  * mqtt 连接
4
4
  * @param client
5
5
  * @param options
6
6
  */
7
- export declare const connect: (client: Client, options: Options) => Promise<unknown>;
7
+ export declare const connect: (client: ClientConntroller, options: Options) => Promise<unknown>;
@@ -1,7 +1,7 @@
1
- import { Client, DisconnectOptions } from "./interface";
1
+ import { ClientConntroller, DisconnectOptions } from "./interface";
2
2
  /**
3
3
  * mqtt 连接
4
4
  * @param client
5
5
  * @param options
6
6
  */
7
- export declare const disconnect: (client: Client, options: DisconnectOptions) => Promise<void>;
7
+ export declare const disconnect: (client: ClientConntroller, options: DisconnectOptions) => Promise<void>;
@@ -1,6 +1,6 @@
1
- import { Adapter, Client, Options } from "./interface";
1
+ import { Adapter, ClientConntroller, Options } from "./interface";
2
2
  declare type Judge = () => boolean;
3
- declare type Connect = (url: string) => Adapter;
3
+ declare type Connect = (url: string, version?: number) => Promise<Adapter>;
4
4
  /**
5
5
  * 添加 Websocket 适配器
6
6
  * 用于适配不同平台下 WebSocket
@@ -13,5 +13,5 @@ export declare const addAdapter: (judge: Judge, creater: Connect, priority?: boo
13
13
  * 创建适配器
14
14
  * @returns
15
15
  */
16
- export declare const createClient: (options: Options) => Promise<Client>;
16
+ export declare const createClient: (options: Options) => Promise<ClientConntroller>;
17
17
  export {};
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export interface Adapter {
3
2
  /**
4
3
  * 关闭连接
@@ -17,7 +16,9 @@ export interface Adapter {
17
16
  /**
18
17
  * 接收到消息监听
19
18
  */
20
- onMessage(cb: (data: ArrayBuffer) => void): void;
19
+ onMessage(cb: (res: {
20
+ data: ArrayBuffer;
21
+ }) => void): void;
21
22
  /**
22
23
  * 网络连接成功
23
24
  * @param cb
@@ -29,9 +30,47 @@ export interface Adapter {
29
30
  */
30
31
  onError(cb: (ev: any) => void): void;
31
32
  }
32
- export interface Client extends Adapter {
33
+ export interface MQTTMessage {
34
+ topic: string;
35
+ payload: Uint8Array;
36
+ }
37
+ export interface ClientConntroller {
38
+ /**
39
+ * 配置
40
+ */
41
+ options: Options;
42
+ /**
43
+ * 协议版本
44
+ */
33
45
  protocolVersion: number;
46
+ /**
47
+ * 服务端是否保存了会话
48
+ * 值为0|1
49
+ */
50
+ sessionPresent: number;
51
+ connect(isReconnect?: boolean): Promise<any>;
34
52
  isClose(): boolean;
53
+ isOnline(): boolean;
54
+ getStatus(): 0 | 1 | 2;
55
+ /**
56
+ * 关闭连接
57
+ */
58
+ close: () => void;
59
+ /**
60
+ * 监听连接关闭
61
+ * @param cb
62
+ */
63
+ onClose(cb: (ev: any) => void): void;
64
+ /**
65
+ * 发送消息
66
+ * @param data
67
+ */
68
+ send(data: ArrayBuffer | ArrayBufferView): void;
69
+ /**
70
+ * 连接出错
71
+ * @param cb
72
+ */
73
+ onError(cb: (ev: any) => void): void;
35
74
  /**
36
75
  * 接收到消息监听
37
76
  */
@@ -39,6 +78,34 @@ export interface Client extends Adapter {
39
78
  offMessage(cb: (data: Uint8Array) => void): void;
40
79
  offClose(cb: (ev: any) => void): void;
41
80
  offError(cb: (ev: any) => void): void;
81
+ emitError(data: any): void;
82
+ /**
83
+ * mqtt 收到消息触发
84
+ * @param cb
85
+ */
86
+ emitMqttMessage(data: MQTTMessage): void;
87
+ /**
88
+ * 注册 mqtt 收到消息事件
89
+ * @param cb
90
+ */
91
+ onMqttMessage(cb: (data: MQTTMessage) => void): void;
92
+ /**
93
+ * 移除 mqtt 收到消息事件
94
+ * @param cb
95
+ */
96
+ offMqttMessage(cb: (data: MQTTMessage) => void): void;
97
+ /**
98
+ * topic 别名处理
99
+ */
100
+ applyTopic: (topicAlias: number, topicName: string) => void;
101
+ /**
102
+ * 通过别名获取 Topic
103
+ */
104
+ getTopic: (topicAlias: number) => string;
105
+ /**
106
+ * 验证主题正确性
107
+ */
108
+ checkTopic: (topicName: string) => boolean;
42
109
  }
43
110
  export declare type QoS = 0 | 1 | 2;
44
111
  export declare type UserProperties = Record<string, string | string[]>;
@@ -70,7 +137,7 @@ export interface Options {
70
137
  /**
71
138
  * 待订阅主题
72
139
  */
73
- subscribeTopics?: string | string[];
140
+ subscribeTopics: string | string[];
74
141
  /**
75
142
  * 是否为新会话
76
143
  */
@@ -101,7 +168,7 @@ export interface Options {
101
168
  sessionExpiryInterval?: number;
102
169
  contentType?: string;
103
170
  responseTopic?: string;
104
- correlationData?: Buffer;
171
+ correlationData?: Uint8Array;
105
172
  userProperties?: UserProperties;
106
173
  };
107
174
  };
@@ -117,23 +184,50 @@ export interface Options {
117
184
  requestProblemInformation?: boolean;
118
185
  userProperties?: UserProperties;
119
186
  authenticationMethod?: string;
120
- authenticationData?: Buffer;
187
+ authenticationData?: Uint8Array;
121
188
  };
122
189
  /**
123
190
  *
124
191
  * 连接超时时间
125
192
  * 单位秒,默认 10 秒
126
193
  */
127
- connectTimeout: number;
194
+ connectTimeout?: number;
195
+ /**
196
+ * 认证处理方法
197
+ * 在收到auth报文时触发
198
+ */
199
+ onAuth?: (options: AuthOptions) => AuthOptions;
200
+ /**
201
+ * 当接收到消息且 qos 为 1 时的处理函数
202
+ * 如果不定义,则默认返回原因码 0
203
+ */
204
+ onPuback?: (options: Message) => PuplishResponse;
205
+ /**
206
+ * 在收到 Pubrec 报文时用于返回 Pubrel 的处理函数,
207
+ * 如果不定义,则默认返回原因码 0
208
+ *
209
+ */
210
+ onPubrel?: (options: PuplishResponse) => PuplishResponse;
211
+ /**
212
+ * 在收到 publish 报文时触发,用于返回 Pubrec 的处理函数
213
+ * 如果不定义,则默认返回原因码 0
214
+ */
215
+ onPubrec?: (options: Message) => PuplishResponse;
216
+ /**
217
+ * 在接收到Pubrel时,用于处理 Pubcomp 返回
218
+ * 如果不定义,则默认返回原因码 0
219
+ */
220
+ onPubcomp?: (options: PuplishResponse) => PuplishResponse;
128
221
  }
222
+ export declare type QOS = 0 | 1 | 2;
129
223
  export interface Message {
130
224
  /**
131
225
  * 消息标识
132
226
  * qos > 0时必填
133
227
  */
134
228
  id?: number;
135
- dup: boolean;
136
- qos: 0 | 1 | 2;
229
+ dup?: boolean;
230
+ qos?: QOS;
137
231
  /**
138
232
  * 主题名
139
233
  */
@@ -153,27 +247,19 @@ export interface Message {
153
247
  };
154
248
  payload: Uint8Array;
155
249
  }
156
- export interface SubscribeOptions {
157
- id: number;
158
- properties?: {
159
- subscriptionIdentifier: number;
160
- userProperties?: UserProperties;
161
- };
162
- payload: {
163
- topic: string;
164
- qos: number;
165
- nl?: 0 | 1;
166
- rap?: 0 | 1;
167
- rh?: number;
168
- }[];
169
- }
170
- export interface UnSubscribeOptions {
171
- id: number;
250
+ export interface MessageOption {
251
+ dup?: boolean;
252
+ qos?: QOS;
172
253
  properties?: {
173
- subscriptionIdentifier: number;
254
+ payloadFormatIndicator?: boolean;
255
+ messageExpiryInterval?: number;
256
+ topicAlias?: number;
257
+ responseTopic?: string;
258
+ correlationData?: string;
174
259
  userProperties?: UserProperties;
260
+ subscriptionIdentifier?: number;
261
+ contentType?: string;
175
262
  };
176
- topics: string[];
177
263
  }
178
264
  export interface DisconnectOptions {
179
265
  /**
@@ -190,3 +276,91 @@ export interface DisconnectOptions {
190
276
  serverReference?: string;
191
277
  };
192
278
  }
279
+ export interface AuthOptions {
280
+ /**
281
+ * 原因码
282
+ */
283
+ reasonCode: number;
284
+ /**
285
+ * 属性,协议版本5支持
286
+ */
287
+ properties?: {
288
+ authenticationMethod: string;
289
+ authenticationData: string;
290
+ reasonString?: string;
291
+ userProperties?: UserProperties;
292
+ };
293
+ }
294
+ export interface PuplishResponse {
295
+ id: number;
296
+ /**
297
+ * 原因码
298
+ */
299
+ reasonCode?: number;
300
+ /**
301
+ * 属性,协议版本5支持
302
+ */
303
+ properties?: {
304
+ reasonString?: string;
305
+ userProperties?: UserProperties;
306
+ };
307
+ }
308
+ export interface Client {
309
+ getStatus(): 0 | 1 | 2;
310
+ /**
311
+ * 接收消息
312
+ */
313
+ onMessage(cb: (data: MQTTMessage) => void): void;
314
+ offMessage(cb: (data: MQTTMessage) => void): void;
315
+ /**
316
+ * 发送消息
317
+ */
318
+ publish(topic: string, message: string | Uint8Array, option?: MessageOption): void;
319
+ /**
320
+ * 重新认证
321
+ */
322
+ reAuth(option: AuthOptions): void;
323
+ /**
324
+ * 断开连接
325
+ */
326
+ disconnect(option?: DisconnectOptions): void;
327
+ /**
328
+ * 关闭事件
329
+ * @param cb
330
+ */
331
+ onClose(cb: () => void): void;
332
+ /**
333
+ * 错误监听
334
+ * @param cb
335
+ */
336
+ onError(cb: (e: any) => void): void;
337
+ /**
338
+ * 订阅
339
+ */
340
+ subscribe: (topic: string, options?: SubscribeOption) => Promise<{
341
+ reasonCode: Uint8Array;
342
+ properties?: any;
343
+ }>;
344
+ /**
345
+ * 取消订阅
346
+ */
347
+ unsubscribe: (topic: string, options?: UnsubscribeOption) => Promise<{
348
+ reasonCode: Uint8Array;
349
+ properties?: any;
350
+ }>;
351
+ }
352
+ export interface SubscribeOption {
353
+ qos: number;
354
+ nl?: 0 | 1;
355
+ rap?: 0 | 1;
356
+ rh?: number;
357
+ properties?: {
358
+ subscriptionIdentifier: number;
359
+ userProperties?: UserProperties;
360
+ };
361
+ }
362
+ export interface UnsubscribeOption {
363
+ properties?: {
364
+ userProperties?: UserProperties;
365
+ };
366
+ }
@@ -53,5 +53,5 @@ export declare const writeProperty: {
53
53
  * @param properies
54
54
  * @param write
55
55
  */
56
- export declare const packetProperties: (properies: Record<string, any>, write: WriteData) => void;
56
+ export declare const packetProperties: (properies: any, write: WriteData) => void;
57
57
  export {};
@@ -1,3 +1,4 @@
1
+ import { AuthOptions, ClientConntroller, Message, PuplishResponse } from "./interface";
1
2
  /**
2
3
  * 属性值类型
3
4
  */
@@ -26,3 +27,15 @@ export declare const parseProperty: {
26
27
  * @returns
27
28
  */
28
29
  export declare const parseProperties: (data: Uint8Array) => Record<string, any>;
30
+ /**
31
+ * 解析认证授权数据
32
+ * @param data
33
+ * @returns
34
+ */
35
+ export declare const parseAuth: (data: Uint8Array) => AuthOptions;
36
+ /**
37
+ * 解析消息数据
38
+ * @param data
39
+ */
40
+ export declare const parsePublish: (data: Uint8Array, client: ClientConntroller) => Message;
41
+ export declare const parsePublishResponse: (data: Uint8Array, client: ClientConntroller) => PuplishResponse;
package/src/lib/ping.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Client } from "./interface";
1
+ import { ClientConntroller } from "./interface";
2
2
  /**
3
3
  * 取消订阅
4
4
  */
5
- export declare const ping: (client: Client) => Promise<true>;
5
+ export declare const ping: (client: ClientConntroller) => Promise<true>;
@@ -1,7 +1,11 @@
1
- import { Client, Message } from "./interface";
1
+ import { ClientConntroller, Message, PuplishResponse } from "./interface";
2
2
  /**
3
3
  * 发布消息
4
4
  * @param client
5
5
  * @param data
6
6
  */
7
- export declare const publish: (client: Client, message: Message) => Promise<unknown>;
7
+ export declare const publish: (client: ClientConntroller, message: Message) => Promise<void>;
8
+ export declare const puback: (client: ClientConntroller, message: PuplishResponse) => Promise<void>;
9
+ export declare const pubrel: (client: ClientConntroller, message: PuplishResponse) => Promise<void>;
10
+ export declare const pubrec: (client: ClientConntroller, message: PuplishResponse) => Promise<void>;
11
+ export declare const pubcomp: (client: ClientConntroller, message: PuplishResponse) => Promise<void>;
@@ -0,0 +1,2 @@
1
+ import { ClientConntroller } from "./interface";
2
+ export declare const handleResponse: (data: Uint8Array, client: ClientConntroller) => void;
@@ -1,17 +1,39 @@
1
- import { Client, SubscribeOptions, UnSubscribeOptions } from "./interface";
1
+ import { ClientConntroller, UserProperties } from "./interface";
2
+ interface SubscribeOptions {
3
+ id: number;
4
+ properties?: {
5
+ subscriptionIdentifier: number;
6
+ userProperties?: UserProperties;
7
+ };
8
+ payload: {
9
+ topic: string;
10
+ qos?: number;
11
+ nl?: 0 | 1;
12
+ rap?: 0 | 1;
13
+ rh?: number;
14
+ }[];
15
+ }
16
+ interface UnSubscribeOptions {
17
+ id: number;
18
+ properties?: {
19
+ userProperties?: UserProperties;
20
+ };
21
+ topics: string[];
22
+ }
2
23
  /**
3
24
  * 订阅消息
4
25
  * @param client
5
26
  * @param options
6
27
  */
7
- export declare const subscribe: (client: Client, subscribe: SubscribeOptions) => Promise<{
8
- qos: number;
28
+ export declare const subscribe: (client: ClientConntroller, options: SubscribeOptions) => Promise<{
29
+ reasonCode: Uint8Array;
9
30
  properties?: any;
10
31
  }>;
11
32
  /**
12
33
  * 取消订阅
13
34
  */
14
- export declare const unsubscribe: (client: Client, subscribe: UnSubscribeOptions) => Promise<{
15
- qos: number;
35
+ export declare const unsubscribe: (client: ClientConntroller, subscribe: UnSubscribeOptions) => Promise<{
36
+ reasonCode: Uint8Array;
16
37
  properties?: any;
17
38
  }>;
39
+ export {};
package/src/new.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { Options } from './lib/interface';
2
- export declare const connectMqtt: (options: Options) => Promise<void>;