forked from hawkingnetwork/react-native-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
133 lines (103 loc) · 3.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
NativeEventEmitter,
NativeModules
} from 'react-native';
var Mqtt = NativeModules.Mqtt;
var MqttClient = function(options, clientRef){
this.options = options;
this.clientRef = clientRef;
this.eventHandler = {};
this.dispatchEvent = function(data) {
if(data && data.clientRef == this.clientRef && data.event) {
if(this.eventHandler[data.event]) {
this.eventHandler[data.event](data.message);
}
}
}
}
MqttClient.prototype.on = function (event, callback) {
console.log('setup event', event);
this.eventHandler[event] = callback;
}
MqttClient.prototype.connect = function () {
Mqtt.connect(this.clientRef);
}
MqttClient.prototype.disconnect = function () {
Mqtt.disconnect(this.clientRef);
}
MqttClient.prototype.subscribe = function (topic, qos) {
Mqtt.subscribe(this.clientRef, topic, qos);
}
MqttClient.prototype.unsubscribe = function (topic) {
Mqtt.unsubscribe(this.clientRef, topic);
}
MqttClient.prototype.publish = function(topic, payload, qos, retain) {
Mqtt.publish(this.clientRef, topic, payload, qos, retain);
}
MqttClient.prototype.reconnect = function() {
Mqtt.reconnect(this.clientRef);
};
MqttClient.prototype.isConnected = function() {
return Mqtt.isConnected(this.clientRef);
};
MqttClient.prototype.getTopics = function() {
return Mqtt.getTopics(this.clientRef);
};
MqttClient.prototype.isSubbed = function(topic) {
return Mqtt.isSubbed(this.clientRef, topic);
};
const emitter = new NativeEventEmitter(Mqtt)
module.exports = {
clients: [],
eventHandler: null,
dispatchEvents: function(data) {
this.clients.forEach(function(client) {
client.dispatchEvent(data);
});
},
createClient: async function(options) {
if(options.uri) {
var pattern = /^((mqtt[s]?|ws[s]?)?:(\/\/)([0-9a-zA-Z_\.\-]*):?(\d+))$/;
var matches = options.uri.match(pattern);
if (!matches) {
throw new Error(`Uri passed to createClient ${options.uri} doesn't match a known protocol (mqtt:// or ws://).`);
}
var protocol = matches[2];
var host = matches[4];
var port = matches[5];
options.port = parseInt(port);
options.host = host;
options.protocol = 'tcp';
if(protocol == 'wss' || protocol == 'mqtts') {
options.tls = true;
}
if(protocol == 'ws' || protocol == 'wss') {
options.protocol = 'ws';
}
}
let clientRef = await Mqtt.createClient(options);
var client = new MqttClient(options, clientRef);
/* Listen mqtt event */
if(this.eventHandler === null) {
console.log('add mqtt_events listener')
this.eventHandler = emitter.addListener(
"mqtt_events",
(data) => this.dispatchEvents(data));
}
this.clients.push(client);
return client;
},
removeClient: function(client) {
var clientIdx = this.clients.indexOf(client);
if(clientIdx > -1)
this.clients.splice(clientIdx, 1);
if(this.clients.length === 0) {
this.eventHandler.remove();
this.eventHandler = null;
}
Mqtt.removeClient(client.clientRef);
},
disconnectAll: function () {
Mqtt.disconnectAll();
},
};