Open uniquejava opened 6 years ago
注意那个clientId格式的坑.
let clientOptions = {
host: "xxx.messaging.internetofthings.ibmcloud.com",
port: 1883,
clientId: "a:xxx:nativescript-vue",
useSSL: false
};
let client = (this.client = new MQTTClient(clientOptions));
client.connect("a-xxx-fwdfcoq0kn", "afafdfdf");
灵感来自: In IOS 11, DeviceMotion in background stopped working
main.js, 虽然在suspendEvent
中调用了相关的service, coremotion内部还是停止了工作.
import * as application from "tns-core-modules/application";
const Vue = require("nativescript-vue");
const App = require("./components/App");
const application = require("tns-core-modules/application");
const motionService = require("~/service/motion-service");
mounted() {
console.log("mounted");
if (application.android) {
console.log("We are running on Android device!");
} else if (application.ios) {
console.log("We are running on iOS device");
}
launchListener = application.on(application.launchEvent, args => {
// The root view for this Window on iOS or Activity for Android.
// If not set a new Frame will be created as a root view in order to maintain backwards compatibility.
console.log("Root View: ", args.root);
console.log("The appication was launched!");
});
suspendListener = application.on(application.suspendEvent, args => {
console.log("The appication was suspended!");
motionService.restart();
});
resumeListener = application.on(application.resumeEvent, args => {
console.log("The appication was resumed!");
});
exitListener = application.on(application.exitEvent, args => {
console.log("The appication was closed!");
});
displayedListener = application.on(application.displayedEvent, args => {
console.log("NativeScript displayedEvent");
});
lowMemoryListener = application.on(application.lowMemoryEvent, args => {
// the instance that has raidsed the event
console.log("Instance: ", args.object);
});
orientationChangedListener = application.on(application.orientationChangedEvent, args => {
// orientationChangedEventData.newValue: "portrait" | "landscape" | "unknown"
console.log("Orientation: ", args.newValue);
});
uncaughtErrorListener = application.on(application.uncaughtErrorEvent, args => {
// UnhandledErrorEventData.error: NativeScriptError
console.log("NativeScript Error: ", args.error);
});
}
npm i event-emitter
然后
const ee = require("event-emitter");
class MotionService { ...}
ee(MotionService.prototype);
module.exports = new MotionService();
将手机自带传感器的motion/acceleration数据通过mqtt发到macbook上的local mqtt broker 终于跑通了, 先备份一下, 晚点再整理.
nativescript-mqtt库有bug, 还好是开源的, 定位到错误
修改了
nativescript-mqtt/index.js
中的publish方法: 修改前:修改后
最终是这么安装的
主要的命令
主要的Home.vue
References
mosquitto enable websockets