在.vue文件中是没有问题的,改成.nvue文件,就开始报这个错误。
TypeError: socketTask.onOpen is not a function
//MQTT连接的配置
options: {
wsOptions: {},
protocolVersion: 4, //MQTT连接协议版本
clientId: 'web_' + this.clientId,
keepalive: 60,
clean: false,
username: '',
password: '',
reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
resubscribe: true //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
},
app配置
// #ifdef APP-PLUS
hosts = 'wx://' + this.serve.host + ':' + this.serve.port + this.serve.path
//#endif
mqtt连接部分相关代码:
connect: function() {
var hosts = '',
// #ifdef MP-WEIXIN
hosts = 'wxs://' + this.serve.host + this.serve.path
//#endif
// #ifdef APP-PLUS
hosts = 'wx://' + this.serve.host + ':' + this.serve.port + this.serve.path
//#endif
if (this.client == null || this.client.connented == false) {
uni.showLoading({
title: '连接中···'
})
this.connectSuccess(false)
// console.log(hosts, this.options)
this.client = mqtt.connect(
hosts,
this.options
);
this.client.on('connect', () => {
uni.hideLoading();
this.showToast('连接成功', 1000, 'success');
this.connectSuccess(true)
this.subscribe();
});
}
this.client.on('reconnect', error => {
uni.hideLoading();
this.showToast('正在重连···', 1000)
});
this.client.on('error', error => {
uni.hideLoading();
this.showToast('连接失败!', 1000)
this.connect();
});
},
subscribe: function() {
// 判断是否已成功连接
if (!this.client || !this.client.connected) {
this.showToast('客户端未连接', 1000)
return;
}
let client = this.client;
let userId = this.$store.state.userId;
let topic = this.topic;
let carId = this.deviceInfo.id;
this.client.subscribe(topic, {
qos: this.Qos
}, error => {
if (!error) {
//this.showToast('订阅成功', 1000, 'success')
console.log('订阅成功');
client.on('message', (topic, message) => {
let data = this.Uint8ArrayToString(message);
console.log(data.vin);
if(data.vehicleId === carId) {
let position = data.border.split(',');
this.latitude = position[1];
this.longitude = position[0];
this.speed = data.speed;
this.mapContext.translateMarker({
markerId: carId,
destination: {
longitude: position[0], // 车辆即将移动到的下一个点的经度
latitude: position[1], // 车辆即将移动到的下一个点的纬度
},
duration: 200,
animationEnd: function () {
// 轨迹回放完成
},
fail(e) {
// 轨迹回放失败
},
});
}
});
}
});
},
unsubscribe: function() {
let topic = this.topic;
this.client.unsubscribe(
// topic, topic Array, topic Array-Onject
// ['one', 'two', 'three'],
topic,
err => {
console.log(err || '取消订阅成功');
this.showToast('取消订阅成功', 1000, 'success')
}
);
},
unconnect: function() {
this.client.end();
this.client = null
this.showToast('成功断开连接', 1000, 'success')
console.log('断开连接');
},
showToast: function(title, time, icon = 'none') {
uni.showToast({
title: title,
icon: icon,
});
setTimeout(function() {
uni.hideToast();
}, time);
},
getUUID(){
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
},
Uint8ArrayToString(fileData){
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return JSON.parse(dataString)
}
j***@163.com (作者)
Thanks♪(・ω・)ノ
2020-10-22 15:25