這個錯誤讓我的頁面顯示不出來,白屏,錯誤信息如下:
TypeError: WebSocket is not a constructor
首先要確認mqtt版本,我之前是4.+,后來改成3.0.0解決一部分問題
npm ls mqtt
npm i mqtt@^3.0.0
白屏是因為要條件編譯,app端是“wx://”,h5端是"ws://“”
這樣白屏解決,但是app端一直連接不上,可能是clientId的問題,我改了之后就完美接收到信息
代碼如下:
const mqtt = require('mqtt/dist/mqtt.js')
var client;
// #ifdef H5
const options = {
port: 8083,
connectTimeout: 4000,
clientId: "mqtt_" + Math.random().toString(16).substr(2, 8),
};
client = mqtt.connect("ws://xxx.xxx.x.xx/mqtt", options);
//#endif
// #ifdef APP-PLUS
// 獲取終端標識
var pinf = plus.push.getClientInfo();
var clientId = pinf.clientid; //客戶端標識
const options = {
port: 8083,
connectTimeout: 4000,
clientId,
};
client = mqtt.connect("wx://xxx.xxx.x.xx/mqtt", options);
//#endif
參考:文章來源:http://www.zghlxwxcb.cn/news/detail-603092.html
https://blog.csdn.net/qq_31754523/article/details/109511628
https://ask.dcloud.net.cn/question/106630
https://ask.dcloud.net.cn/article/id-38673文章來源地址http://www.zghlxwxcb.cn/news/detail-603092.html
到了這里,關于解決 uniapp報錯:TypeError: WebSocket is not a constructor的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!