Skip to content

Commit 883b2c9

Browse files
committed
Added unsubscribe topic method for JS and Android
Refactored Android source code
1 parent 7a21eca commit 883b2c9

File tree

5 files changed

+138
-97
lines changed

5 files changed

+138
-97
lines changed

android/android.iml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@
6767
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6868
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6969
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
75-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
76-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
77-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
7870
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
7971
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
8072
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -83,6 +75,14 @@
8375
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
8476
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
8577
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
83+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
84+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
85+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
8787
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
8888
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />

android/src/main/java/com/tuanpm/RCTMqtt/RCTMqtt.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.tuanpm.RCTMqtt;
22

3+
import android.support.annotation.NonNull;
34
import android.util.Log;
45

56
import com.facebook.react.bridge.Arguments;
@@ -71,7 +72,7 @@ public RCTMqtt(final int ref,
7172
createClient(options);
7273
}
7374

74-
private void createClient(final ReadableMap params)
75+
private void createClient(@NonNull final ReadableMap params)
7576
{
7677
if (params.hasKey("host"))
7778
{
@@ -316,7 +317,7 @@ public void onFailure(IMqttToken asyncActionToken,
316317
}
317318
}
318319

319-
public void subscribe(final String topic,
320+
public void subscribe(@NonNull final String topic,
320321
final int qos)
321322
{
322323
try
@@ -348,14 +349,41 @@ public void onFailure(IMqttToken asyncActionToken,
348349
}
349350
}
350351

352+
public void unsubscribe(@NonNull final String topic)
353+
{
354+
try
355+
{
356+
client.unsubscribe(topic).setActionCallback(new IMqttActionListener()
357+
{
358+
@Override
359+
public void onSuccess(IMqttToken asyncActionToken)
360+
{
361+
log(new StringBuilder("Subscribed on ").append(topic).toString());
362+
}
363+
364+
@Override
365+
public void onFailure(IMqttToken asyncActionToken,
366+
Throwable exception)
367+
{
368+
log(new StringBuilder("Failed to subscribe on ").append(topic).toString());
369+
}
370+
});
371+
}
372+
catch (MqttException e)
373+
{
374+
log(new StringBuilder("Can't unsubscribe").append(" ").append(topic).toString());
375+
e.printStackTrace();
376+
}
377+
}
378+
351379
/**
352380
* @param topic
353381
* @param payload
354382
* @param qos
355383
* @param retain
356384
*/
357-
public void publish(final String topic,
358-
final String payload,
385+
public void publish(@NonNull final String topic,
386+
@NonNull final String payload,
359387
final int qos,
360388
final boolean retain)
361389
{
@@ -417,8 +445,8 @@ public void deliveryComplete(IMqttDeliveryToken token)
417445
/**
418446
* @see MqttCallback#messageArrived(String, MqttMessage)
419447
*/
420-
public void messageArrived(final String topic,
421-
final MqttMessage message) throws
448+
public void messageArrived(@NonNull final String topic,
449+
@NonNull final MqttMessage message) throws
422450
MqttException
423451
{
424452
// Called when a message arrives from the server that matches any
@@ -443,12 +471,13 @@ public void messageArrived(final String topic,
443471
*
444472
* @param message the message to log
445473
*/
446-
void log(String message)
474+
void log(@NonNull final String message)
447475
{
448476
if (!BuildConfig.DEBUG)
449477
{
450478
return;
451479
}
452-
Log.d(TAG, message);
480+
final String tag = new StringBuilder(TAG).append(" ").append(clientRef).toString();
481+
Log.d(tag, message);
453482
}
454483
}

android/src/main/java/com/tuanpm/RCTMqtt/RCTMqttModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.tuanpm.RCTMqtt;
66

77
import android.support.annotation.NonNull;
8+
import android.support.annotation.Nullable;
89
import android.util.Log;
910

1011
import com.facebook.react.bridge.Promise;
@@ -82,12 +83,19 @@ public void disconnect(final int clientRef)
8283

8384
@ReactMethod
8485
public void subscribe(final int clientRef,
85-
final String topic,
86+
final @NonNull String topic,
8687
final int qos)
8788
{
8889
clients.get(clientRef).subscribe(topic, qos);
8990
}
9091

92+
@ReactMethod
93+
public void unsubscribe(final int clientRef,
94+
final @NonNull String topic)
95+
{
96+
clients.get(clientRef).unsubscribe(topic);
97+
}
98+
9199
@ReactMethod
92100
public void publish(final int clientRef,
93101
final String topic,

index.js

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,108 @@
11
import {
2-
DeviceEventEmitter,
3-
NativeModules
2+
DeviceEventEmitter,
3+
NativeModules
44
} from 'react-native';
55

66
var Mqtt = NativeModules.Mqtt;
77

88
var MqttClient = function(options, clientRef){
9-
this.options = options;
10-
this.clientRef = clientRef;
11-
this.eventHandler = {};
12-
13-
this.dispatchEvent = function(data) {
14-
15-
if(data && data.clientRef == this.clientRef && data.event) {
16-
17-
if(this.eventHandler[data.event]) {
18-
this.eventHandler[data.event](data.message);
19-
}
20-
}
21-
}
9+
this.options = options;
10+
this.clientRef = clientRef;
11+
this.eventHandler = {};
12+
13+
this.dispatchEvent = function(data) {
14+
15+
if(data && data.clientRef == this.clientRef && data.event) {
16+
17+
if(this.eventHandler[data.event]) {
18+
this.eventHandler[data.event](data.message);
19+
}
20+
}
21+
}
2222
}
2323

2424
MqttClient.prototype.on = function (event, callback) {
25-
console.log('setup event', event);
26-
this.eventHandler[event] = callback;
25+
console.log('setup event', event);
26+
this.eventHandler[event] = callback;
2727
}
2828

2929
MqttClient.prototype.connect = function () {
30-
Mqtt.connect(this.clientRef);
30+
Mqtt.connect(this.clientRef);
3131
}
3232

3333
MqttClient.prototype.disconnect = function () {
34-
Mqtt.disconnect(this.clientRef);
34+
Mqtt.disconnect(this.clientRef);
3535
}
3636

3737
MqttClient.prototype.subscribe = function (topic, qos) {
38-
Mqtt.subscribe(this.clientRef, topic, qos);
38+
Mqtt.subscribe(this.clientRef, topic, qos);
39+
}
40+
41+
MqttClient.prototype.unsubscribe = function (topic) {
42+
Mqtt.unsubscribe(this.clientRef, topic);
3943
}
4044

4145
MqttClient.prototype.publish = function(topic, payload, qos, retain) {
42-
Mqtt.publish(this.clientRef, topic, payload, qos, retain);
46+
Mqtt.publish(this.clientRef, topic, payload, qos, retain);
4347
}
4448

4549
module.exports = {
46-
clients: [],
47-
eventHandler: null,
48-
dispatchEvents: function(data) {
49-
this.clients.forEach(function(client) {
50-
client.dispatchEvent(data);
51-
});
52-
},
53-
createClient: async function(options) {
54-
if(options.uri) {
55-
var pattern = /^((mqtt[s]?|ws[s]?)?:(\/\/)([0-9a-zA-Z_\.]*):?(\d+))$/;
56-
var matches = options.uri.match(pattern);
57-
var protocol = matches[2];
58-
var host = matches[4];
59-
var port = matches[5];
60-
61-
options.port = parseInt(port);
62-
options.host = host;
63-
options.protocol = 'tcp';
64-
65-
66-
if(protocol == 'wss' || protocol == 'mqtts') {
67-
options.tls = true;
68-
}
69-
if(protocol == 'ws' || protocol == 'wss') {
70-
options.protocol = 'ws';
71-
}
72-
73-
}
74-
75-
let clientRef = await Mqtt.createClient(options);
76-
77-
var client = new MqttClient(options, clientRef);
78-
79-
/* Listen mqtt event */
80-
if(this.eventHandler === null) {
81-
console.log('add mqtt_events listener')
82-
this.eventHandler = DeviceEventEmitter.addListener(
83-
"mqtt_events",
84-
(data) => this.dispatchEvents(data));
85-
}
86-
this.clients.push(client);
87-
88-
return client;
89-
},
90-
removeClient: function(client) {
91-
var clientIdx = this.clients.indexOf(client);
92-
93-
/* TODO: destroy client in native module */
94-
95-
if(clientIdx > -1)
96-
this.clients.splice(clientIdx, 1);
97-
98-
if(this.clients.length > 0) {
99-
this.eventHandler.remove();
100-
this.eventHandler = null;
101-
}
102-
}
103-
50+
clients: [],
51+
eventHandler: null,
52+
dispatchEvents: function(data) {
53+
this.clients.forEach(function(client) {
54+
client.dispatchEvent(data);
55+
});
56+
},
57+
createClient: async function(options) {
58+
if(options.uri) {
59+
var pattern = /^((mqtt[s]?|ws[s]?)?:(\/\/)([0-9a-zA-Z_\.]*):?(\d+))$/;
60+
var matches = options.uri.match(pattern);
61+
var protocol = matches[2];
62+
var host = matches[4];
63+
var port = matches[5];
64+
65+
options.port = parseInt(port);
66+
options.host = host;
67+
options.protocol = 'tcp';
68+
69+
70+
if(protocol == 'wss' || protocol == 'mqtts') {
71+
options.tls = true;
72+
}
73+
if(protocol == 'ws' || protocol == 'wss') {
74+
options.protocol = 'ws';
75+
}
76+
77+
}
78+
79+
let clientRef = await Mqtt.createClient(options);
80+
81+
var client = new MqttClient(options, clientRef);
82+
83+
/* Listen mqtt event */
84+
if(this.eventHandler === null) {
85+
console.log('add mqtt_events listener')
86+
this.eventHandler = DeviceEventEmitter.addListener(
87+
"mqtt_events",
88+
(data) => this.dispatchEvents(data));
89+
}
90+
this.clients.push(client);
91+
92+
return client;
93+
},
94+
removeClient: function(client) {
95+
var clientIdx = this.clients.indexOf(client);
96+
97+
/* TODO: destroy client in native module */
98+
99+
if(clientIdx > -1)
100+
this.clients.splice(clientIdx, 1);
101+
102+
if(this.clients.length > 0) {
103+
this.eventHandler.remove();
104+
this.eventHandler = null;
105+
}
106+
}
107+
104108
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-mqtt",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "MQTT client for react-native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)