阿里云物联网的alibabacloud-iot-device-sdk引入后运行到浏览器正常,运行到真机WS错误?哪位大佬能提点一下。非常感谢!错误提示:[Vue warn]: Error in mounted hook: "TypeError: WS is not a constructor"
邮箱:bug_me_cjx@163.com
main.js
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App,
data() {
return {
;
},
mounted() {
const iot = require('alibabacloud-iot-device-sdk');
// 新版公共实例和企业版实例,必须填写实例ID,旧版公实例无需填写。
const instanceId = '**';
// 当前产品和设备所属地域的ID。
const region = 'cn-shanghai';
const brokerUrl = instanceId
? `wss://iot-06*****gbo6u0.mqtt.iothub.aliyuncs.com:443`
: `wss://****.iot-as-mqtt.cn-shanghai.aliyuncs.com:443`;
// 创建iot.device对象将会发起到阿里云IoT的连接
Vue.prototype.device = iot.device({
productKey: "________", // 将<productKey>修改为实际产品的ProductKey
deviceName: "_______", // 将<deviceName>修改为实际设备的DeviceName
deviceSecret: "________", // 将<deviceSecret>修改为实际设备的DeviceSecret
brokerUrl,
tls: true,
});
// 监听connect事件
this.device.on('connect', () => {
// 将<productKey> <deviceName>修改为实际值
this.device.subscribe('/ProductKey/deviceName/user/get');
console.log('connect successfully!');
// device.publish('/ProductKey/deviceName/user/update', 'Hello world!');
});
}
})
app.$mount()
// #endif
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
// #endif
1 个回复
1***@163.com
我也遇到了,浏览器正常,真机不正常