-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
85 lines (79 loc) · 2.12 KB
/
App.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, {useEffect, useState} from 'react';
import {SafeAreaView, Text} from 'react-native';
import mqtt from 'mqtt';
const App = () => {
const [mqttState, useMqttState] = useState('');
useEffect(() => {
mqttConnection();
}, []);
const mqttConnection = () => {
// const client = mqtt.connect('mqtt://test.mosquitto.org', {
// clientId: 'mqttjs_12dsjfbsdkj',
// clean: true,
// keepalive: 120,
// reconnectPeriod: 5000,
// port: 1883
// });
const client = mqtt.connect({
port: 1883,
host: 'test.mosquitto.org',
protocol: 'mqtt',
clientId: 'mqttjs_12dsjfbsdkj',
clean: true,
keepalive: 120,
reconnectPeriod: 5000,
});
client.on('connect', () => {
console.log('connected with mqtt');
});
client.on('error', () => {
console.log('error with mqtt');
});
client.on('reconnect', () => {
console.log('reconnect with mqtt');
});
client.on('offline', () => {
console.log('offline with mqtt');
});
client.on('close', () => {
console.log('close with mqtt');
});
client.on('disconnect', () => {
console.log('disconnect with mqtt');
});
client.on('message', (topic, message) => {
console.log('message with mqtt');
// client.end();
});
// .on('message', (topic, message, packet) => {
// console.log('message with mqtt');
// // onMessage(topic, message)
// })
// console.log('client', client);
// client.on('connect', () => {
// console.log('connected with mqtt');
// // client.subscribe('presence', err => {
// // if (!err) {
// // client.publish('presence', 'Hello mqtt');
// // }
// // });
// });
// client.on('message', (topic, message) => {
// // message is Buffer
// console.log(message.toString());
// client.end();
// });
};
return (
<SafeAreaView style={{flex: 1}}>
<Text>MQTT state is: {mqttState}</Text>
</SafeAreaView>
);
};
export default App;