@@ -47,60 +47,60 @@ new RCTMqttPackage() // for newest version of react-native
47
47
48
48
- Append the following lines to ` android/settings.gradle ` before ` include ':app' ` :
49
49
50
- ``` java
50
+ ```
51
51
include ':react-native-mqtt'
52
52
project(':react-native-mqtt').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-mqtt/android')
53
+
53
54
```
54
55
56
+
55
57
- Insert the following lines inside the dependencies block in ` android/app/build.gradle ` , don't missing ` apply plugin:'java' ` on top:
56
58
57
- ``` java
59
+ ```
58
60
compile project(':react-native-mqtt')
59
61
```
60
62
61
63
Notes:
62
64
63
- ``` java
65
+ ```
64
66
dependencies {
65
67
compile project(':react-native-mqtt')
66
68
}
67
69
```
68
70
69
71
70
- But not like this
71
-
72
- ``` java
73
- buildscript {
74
- ...
75
- dependencies {
76
- compile project(' :react-native-mqtt' )
77
- }
78
- }
79
- ```
80
-
81
72
## Usage
82
73
83
74
``` javascript
84
75
var mqtt = require (' react-native-mqtt' );
85
76
86
- var client = mqtt .Client ({
87
- uri: ' mqtt://mqtt.yourhost.com:1883'
88
- });
89
-
90
- client .on (' connect' , function () {
91
- client .subscribe (' /topic/qos0' );
92
- client .subscribe (' /topic/qos1' , 1 );
93
- client .subscribe (' /topic/qos2' , 2 );
94
-
95
- client .publish (' /topic/qos0' , ' publish with string' );
96
- client .publish (' /topic/qos1' , new ArrayBuffer (10 )); // publish with arraybuffer
97
- });
98
-
99
- client .on (' message' , function (topic , message ) {
100
- // message is ArrayBuffer
101
- // topic is string
102
- // console.log(message.toString());
103
- client .disconnect ();
77
+ /* create mqtt client */
78
+ mqtt .createClient ({
79
+ uri: ' mqtt://test.mosquitto.org:1883' ,
80
+ clientId: ' your_client_id'
81
+ }).then (function (client ) {
82
+
83
+ client .on (' closed' , function () {
84
+ console .log (' mqtt.event.closed' );
85
+
86
+ });
87
+
88
+ client .on (' error' , function (msg ) {
89
+ console .log (' mqtt.event.error' , msg);
90
+
91
+ });
92
+
93
+ client .on (' message' , function (msg ) {
94
+ console .log (' mqtt.event.message' , msg);
95
+ });
96
+
97
+ client .on (' connect' , function () {
98
+ console .log (' connected' );
99
+ client .subscribe (' /device_00059E18/data' , 1 );
100
+ client .publish (' /device_00059E18/data' , " test" , 1 , false );
101
+ });
102
+
103
+ client .connect ();
104
104
});
105
105
106
106
```
0 commit comments