TypeError: WS is not a constructor
at WebSocketStream (mqtt.min.js? [sm]:1)
at createWebSocket (mqtt.min.js? [sm]:1)
at Object.buildBuilderBrowser (mqtt.min.js? [sm]:1)
at MqttClient.wrapper [as streamBuilder] (mqtt.min.js? [sm]:1)
at MqttClient._setupStream (mqtt.min.js? [sm]:1)
at new MqttClient (mqtt.min.js? [sm]:1)
at Function.connect (mqtt.min.js? [sm]:1)
at li.onLoad (home.js? [sm]:43)
at li.<anonymous> (VM15444 WASubContext.js:1)
at li.c.__callPageLifeTime__ (VM15444 WASubContext.js:1)(env: Windows,mp,1.06.2504010; lib: 3.8.11)
下述代码出现上述报错:
var mqtt = require('../../utils/mqtt.min.js')
const crypto = require('../../utils/crypto');
Page({
data: {
motionData: [
{ actionType: '大幅度动作', count: 19},
{ actionType: '小幅度动作', count: 39 },
{ actionType: '上半身动作', count: 15},
{ actionType: '下半身动作', count: 24 }
],
vitalSigns: {
breath: 18, // 呼吸数值
heartRate: 76 // 心率数值
},
sleepStageDescription: '', // 睡眠阶段描述
currentAction: null, // 当前动作
client: null,
// mqttAccountInfo: {
// productKey: "0ee6d6edd8",
// deviceName: "6876033432771f177b4c49bb_mini_app",
// deviceSecret: "88888888",
// },
},
// sendCommand() {
// this.data.client.publish('v1/M2M/#',function (err) {
// if (!err) {
// console.log('订阅成功!');
// }
// }); // 发布消息
// },
onUnload() {
this.data.client.end(); // 断开连接
},
onLoad: function() {
const client = mqtt.connect('wss://0ee6d6edd8.st1.iotda-app.cn-north-4.myhuaweicloud.com:8883/mqtt', {
clientId: '6876033432771f177b4c49bb_vibrating_sensor_0_0_2025072314',
username: '6876033432771f177b4c49bb_vibrating_sensor',
password: '115f940c0c261063d7238461e9de2ee065c1464cbff685d4eb488114425a97de',
protocolVersion: 4
});
client.on('connect', () => {
console.log('MQTT连接成功');
client.subscribe('v1/M2M/#',function (err) {
if (!err) {
console.log('订阅成功!');
}
});
// 如果这些方法依赖于MQTT连接后的数据
this.updateSleepStageDescription();
this.updateCurrentAction();
});
client.on('message', (topic, message) => {
console.log(`收到消息: ${message.toString()} 主题: ${topic}`);
});
this.setData({ client });
},
updateSleepStageDescription: function () {
const { motionData } = this.data;
// 假设根据动作次数判断睡眠阶段
let sleepStage = '';
// 简单逻辑来判断睡眠阶段(可以根据动作数据的具体情况调整)
if (motionData[0].count > 20 && motionData[1].count > 20) {
sleepStage = '深度睡眠(动作较少)';
} else if (motionData[0].count < 20 && motionData[1].count < 20) {
sleepStage = '浅度睡眠(动作较多)';
} else {
sleepStage = '浅睡眠';
}
// 更新睡眠阶段描述
this.setData({
sleepStageDescription: sleepStage
});
},
updateCurrentAction: function () {
// 模拟外部数据,动态更新当前动作
const actions = [
{ name: '平躺', image: '../../images/lie-flat.jpg' },
{ name: '侧翻', image: '../../images/roll-over.jpg' },
{ name: '曲腿', image: '../../images/bend-leg.jpg' },
{ name: '曲臂', image: '../../images/bend-arm.jpg' },
{ name: '转头', image: '../../images/turn-head.jpg' }
];
// 假设根据某种逻辑选择当前动作
const currentAction = actions[Math.floor(Math.random() * actions.length)];
this.setData({
currentAction
});
},
// doConnect() {
// let that = this
// const options = this.initMqttOptions();
// this.data.client = mqtt.connect('wss://' + this.data.mqttAccountInfo.productKey + '.st1.iotda-device.cn-north-4.myhuaweicloud.com',options)
// this.data.client.on('connect', function () {
// console.log('连接服务器成功')
// that.data.client.subscribe('/v1/M2M/#', function (err) {
// if (!err) {
// console.log('订阅成功!');
// }
// })
// })
// //接收消息监听
// that.data.client.on('message', function (topic, message) {
// let msg = message.toString();
// console.log('收到消息:' + msg);
// that.setData({
// sensorList: JSON.parse(message)
// })
// })
// },
// unconnect() {
// this.data.client.end();
// this.data.client = null;
// console.log("断开连接")
// },
// //IoT平台mqtt连接参数初始化
// initMqttOptions() {
// const params = {
// productKey: this.data.mqttAccountInfo.productKey,
// deviceName: this.data.mqttAccountInfo.deviceName,
// deviceSecret: this.data.mqttAccountInfo.deviceSecret,
// timestamp: Date.now(),
// }
// //生成clientId,username,password
// const contentStr = "clientId" + params.clientId + "deviceName" + params.deviceName + "productKey" + params.productKey + "timestamp" + params.timestamp;
// const clientId = `${params.clientId}|securemode=2,signmethod=hmacsha1,timestamp=${params.timestamp}|`;
// const username = `${params.deviceName}&${params.productKey}`;
// //const password = 'dff0d64ad1bdf0651ed744d2b75b885fc1675a41d0ab26fe7ddd03722bb4db03';
// const password = crypto.HmacSHA1(contentStr, params.deviceSecret).toString();
// const options = {
// wsOptions: {},
// protocolVersion: 4, //MQTT连接协议版本
// clientId: clientId,
// keepalive: 60,
// clean: false,
// username: username,
// password: password,
// reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
// connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
// resubscribe: true //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
// }
// return options;
// },
// buttonChange: function (e) {
// this.data.sensorList[e.currentTarget.dataset.param].value = !this.data.sensorList[e.currentTarget.dataset.param].value
// console.log(this.data.sensorList)
// this.data.sendData = this.data.sensorList
// this.publish()
// }
});
最新发布