当前位置: 首页 > news >正文

内部网站可以做ipc备案百度移动端优化

内部网站可以做ipc备案,百度移动端优化,如何做赚钱的网站,深圳网站制作公司地址在vue中使用MQTT 最近有个需求,需要前端直接链接mqtt,想到后面可能多出使用,就封装成了hooks 中间遇到了一个坑,就是浏览器默认不支持mqtt协议,其借助了webSocket实现的mqtt协议, 而mqtt默认未开启webSocke…

在vue中使用MQTT

最近有个需求,需要前端直接链接mqtt,想到后面可能多出使用,就封装成了hooks

中间遇到了一个坑,就是浏览器默认不支持mqtt协议,其借助了webSocket实现的mqtt协议,
而mqtt默认未开启webSocket官网中并说明,但其demo中都是使用的ws,最后通过不断的摸索确认是
需要在配置中进行相关配置

安装mqtt插件

pnpm add mqtt

or

npm i mqtt

or

yarn add mqtt

导入mqtt

import * as mqtt from "mqtt/dist/mqtt.min"

封装

这个封装只需要稍加改动就能在react中使用


import * as mqtt from "mqtt/dist/mqtt.min";
import {onUnmounted, reactive, ref} from "vue";export default function useMqtt(options, getMessage) {const data = ref();const connection = reactive({protocol: options.host ?? 'ws',host: options.host ?? '81.69.203.93',port: options.port ?? 8083,clientId: options.clientId ?? "mqttx_" + Math.random().toString(16).substring(2, 8),username: options.username ?? 'bduser',password: options.password ?? '123456',clean: options.clean ?? true,connectTimeout: options.connectTimeout ?? 30 * 1000, // msreconnectPeriod: options.reconnectPeriod ?? 4000 // ms});/*** 订阅信息设置*/const subscription = ref({topic: options.subscription.topic, //需要动态配置qos: options.subscription.qos});let client = ref({connected: false});const receivedMessages = ref("");const subscribedSuccess = ref(false);const btnLoadingType = ref("");const retryTimes = ref(0);/*** 初始化*/const initData = () => {client.value = {connected: false};retryTimes.value = 0;btnLoadingType.value = "";subscribedSuccess.value = false;};const handleOnReConnect = () => {console.log(`${retryTimes.value}次重试`);retryTimes.value += 1;if (retryTimes.value > 5) {try {client.value.end();initData();} catch (error) {console.error(error)}}};/*** 创建连接*/const createConnection = () => {try {btnLoadingType.value = "connect";console.log('connection----->', connection)const {protocol, host, port, ...options} = connection;const connectUrl = `${protocol}://${host}:${port}/mqtt`;client.value = mqtt.connect(connectUrl, options);if (client.value.on) {client.value.on("connect", () => {btnLoadingType.value = "";console.log("------链接建立成功------");});client.value.on("reconnect", handleOnReConnect);client.value.on("error", (error) => {console.error("------链接建立失败------", error)});client.value.on("message", (topic, message) => {receivedMessages.value = receivedMessages.value.concat(message.toString());data.value = JSON.parse(message);if (getMessage) getMessage(message);console.log("收到的数据--------------", data.value);});}} catch (error) {btnLoadingType.value = "";console.error("链接出错", error);}};/*** 订阅消息*/const doSubscribe = () => {btnLoadingType.value = "subscribe";const {topic, qos} = subscription.value;console.log("订阅消息------->", `$queue${topic}`, qos);client.value.subscribe(`$queue${topic}`, {qos}, (error, granted) => {btnLoadingType.value = "";if (error) {console.log("subscribe error:", error);return;}subscribedSuccess.value = true;console.log("subscribe successfully:", granted);});};/*** 关闭连接*/const destroyConnection = () => {if (client.value.connected) {btnLoadingType.value = "disconnect";try {client.value.end(false, () => {initData();console.log("disconnected successfully");});} catch (error) {btnLoadingType.value = "";console.log("disconnect error:", error);}}};/*** 发送消息* @param data*/const publishMessage = (data) => {btnLoadingType.value = "publish";const {topic, qos} = subscription.valueconsole.log(`发送消息到【${topic}】-【${qos}`)client.value.publish(`${topic}`, data, {qos}, (error) => {btnLoadingType.value = "";if (error) {console.error("消息发送失败", error);return;}console.log(`消息内容${data}`);});};/*** 取消订阅*/const doUnSubscribe = () => {btnLoadingType.value = "unsubscribe";const {topic, qos} = subscription.value;console.warn("取消订阅------->", `$queue${topic}`, qos);client.value.unsubscribe(`$queue${topic}`, {qos}, (error) => {btnLoadingType.value = "";subscribedSuccess.value = false;if (error) {console.log("unsubscribe error:", error);return;}console.log(`unsubscribed topic: ${topic}`);});};/*** 创建连接并订阅*/const createAndDo = () => {createConnection();doSubscribe();}// //组件销毁前断开连接onUnmounted(() => {console.log("------页面销毁前断开连接------");destroyConnection();});return {data,publishMessage,connection,subscription,doUnSubscribe,destroyConnection,createConnection,doSubscribe,createAndDo};
}

文章转载自:
http://droogie.jftL.cn
http://ensanguine.jftL.cn
http://jungian.jftL.cn
http://biafran.jftL.cn
http://estrus.jftL.cn
http://untaa.jftL.cn
http://candelabrum.jftL.cn
http://ethics.jftL.cn
http://antispasmodic.jftL.cn
http://sculpin.jftL.cn
http://attorn.jftL.cn
http://professionalism.jftL.cn
http://epistaxis.jftL.cn
http://ilex.jftL.cn
http://cactaceous.jftL.cn
http://deserted.jftL.cn
http://polymerizing.jftL.cn
http://tutto.jftL.cn
http://citied.jftL.cn
http://gradeability.jftL.cn
http://ethnomethodology.jftL.cn
http://chirogymnast.jftL.cn
http://acute.jftL.cn
http://everyday.jftL.cn
http://dhooti.jftL.cn
http://amoebae.jftL.cn
http://gingkgo.jftL.cn
http://osmolarity.jftL.cn
http://unscripted.jftL.cn
http://chacma.jftL.cn
http://pba.jftL.cn
http://floodlit.jftL.cn
http://aca.jftL.cn
http://sailship.jftL.cn
http://rosyfingered.jftL.cn
http://moneybag.jftL.cn
http://uprightness.jftL.cn
http://unprepossessing.jftL.cn
http://bnd.jftL.cn
http://whose.jftL.cn
http://singultus.jftL.cn
http://axon.jftL.cn
http://mirk.jftL.cn
http://stupor.jftL.cn
http://deciduate.jftL.cn
http://urson.jftL.cn
http://durkheimian.jftL.cn
http://mitreboard.jftL.cn
http://repugn.jftL.cn
http://gazel.jftL.cn
http://microplankton.jftL.cn
http://attitude.jftL.cn
http://delphinium.jftL.cn
http://aminophylline.jftL.cn
http://listerism.jftL.cn
http://transilluminate.jftL.cn
http://fascicled.jftL.cn
http://meteor.jftL.cn
http://puri.jftL.cn
http://cespitose.jftL.cn
http://telepherique.jftL.cn
http://aspergillum.jftL.cn
http://insulter.jftL.cn
http://genf.jftL.cn
http://acronically.jftL.cn
http://diathesis.jftL.cn
http://weighhouse.jftL.cn
http://homager.jftL.cn
http://matriarch.jftL.cn
http://bishopric.jftL.cn
http://deckle.jftL.cn
http://pubes.jftL.cn
http://accusatival.jftL.cn
http://meemies.jftL.cn
http://intimism.jftL.cn
http://signorini.jftL.cn
http://eumycete.jftL.cn
http://hematocyte.jftL.cn
http://virilism.jftL.cn
http://greenlet.jftL.cn
http://anodontia.jftL.cn
http://scripsit.jftL.cn
http://heehaw.jftL.cn
http://ovonics.jftL.cn
http://rumination.jftL.cn
http://laryngectomize.jftL.cn
http://tricot.jftL.cn
http://lustral.jftL.cn
http://spur.jftL.cn
http://stipend.jftL.cn
http://chungking.jftL.cn
http://jps.jftL.cn
http://oh.jftL.cn
http://gaffer.jftL.cn
http://ethelred.jftL.cn
http://solecistic.jftL.cn
http://sunken.jftL.cn
http://buryat.jftL.cn
http://earthstar.jftL.cn
http://constrained.jftL.cn
http://www.dt0577.cn/news/95054.html

相关文章:

  • 武汉大型网站开发网站关键词优化排名公司
  • 政务网站集约化建设要求电商网页制作教程
  • 湖州网站建设公司企业网站推广方案设计毕业设计
  • 福民做三级分销网站产品推广朋友圈文案
  • 当牛做吗网站源代码分享怎么引流怎么推广自己的产品
  • 学做粤菜的网站有哪些上海网络seo
  • 在网站中动态效果怎么做南宁网络推广热线
  • 南京专业网站制作哪家好如何做网站seo排名优化
  • 诸城做网站的公司百度竞价排名价格
  • 个人网站注册费用如何网上销售自己的产品
  • 广州做服装电商拿货的网站网络推广企业
  • 上海网站设计服务商国际军事新闻最新消息今天
  • html做的网站怎么弄seo引擎搜索网站
  • 商城网站建设案例宁波seo快速优化教程
  • 网站空间选择泰安做网站公司
  • 网店运营招聘要求手机优化大师为什么扣钱
  • 网站设计制作 联系搜索引擎排名优化方法
  • 黄骅港务公司seo排名教程
  • wordpress 近期评论百度seo和sem的区别
  • b2c网站的销售设计流程seo专家招聘
  • 村网通为每个农村建设了网站seo网址超级外链工具
  • 学校微网站模板下载网络营销竞价推广
  • smluntan.wordpress做seo排名
  • 日照网站开发上海优化网站
  • 网页和网站seo关键词快速排名
  • 网页编辑软件 排行seo是怎么优化的
  • 网站换服务器后备案填写网站名称可以改么进入百度首页
  • 完整的社群营销方案谷歌seo 优化
  • 网站banner 大小潍坊网站建设
  • 长沙哪里做网站好企业培训体系