npm i --save nativescript-ichi-mqtt
- 版本: 0.1.3
- GitHub: https://github.com/mozhju/nativescript-ichi-mqtt
- NPM: https://npmjs.net.cn/package/nativescript-ichi-mqtt
- 下载
- 昨天: 0
- 上周: 0
- 上个月: 0
nativeScript-ichi-mqtt
NativeScript 的 mqtt 客户端。
支持的平台
- Android (任何搭载 Android 4.4 及以上版本的设备)
目前尚不支持 iOS!
安装
tns plugin add nativescript-ichi-mqtt
用法
以下是一个 TypeScript 示例
import {MqttClient} from "nativescript-ichi-mqtt";
// new Mqtt Client
var mqttClient = new MqttClient("productKey", "deviceName", "deviceSecret",
"serverUri", "subscriptionTopic", "publishTopic");
// LogLevel : 1 DEBUG, 2 INFO, 3 WARN, 4 ERROR
mqttClient.setLogLevel(3);
var stateListener = {
onConnectFail: function(message) {
console.log("stateListener onConnectFail: ", message);
},
onConnected: function() {
console.log("stateListener onConnected");
},
onDisconnect: function() {
console.log("stateListener onDisconnect");
}
}
mqttClient.setConnectionStateListener(stateListener);
var subscribeListener = {
onSuccess: function(topic) {
console.log("subscribeListener onSuccess: ", topic);
},
onFailed: function(topic, error) {
console.log("subscribeListener onFailed: ", topic);
},
needUISafety: function() {
console.log("subscribeListener needUISafety");
return true;
}
}
mqttClient.setSubscribeListener(subscribeListener);
var pushListener = {
onCommand: function(topic, data) {
console.log("pushListener onCommand: ", topic);
},
shouldHandle: function(topic) {
console.log("pushListener shouldHandle", topic);
return true;
}
}
mqttClient.setPushListener(pushListener);
var callListener = {
onSuccess: function(request, response) {
console.log("callListener onSuccess: ");
},
onFailed: function(request, error) {
console.log("callListener onFailed: ");
},
needUISafety: function() {
console.log("callListener needUISafety");
return true;
}
}
mqttClient.setCallListener(callListener);
mqttClient.startListener();
// mqttClient.subscribeTopic("subscribeTopic2");
// mqttClient.unSubscribeTopic("subscribeTopic");
mqttClient.publishMessage("publishMessage", "publishTopic2");
// mqttClient.stopListener();