• websock


    1. import mqtt from 'mqtt/dist/mqtt'
    2. function initData (that) {
    3. that.client = {
    4. connected: false
    5. }
    6. that.retryTimes = 0
    7. that.connecting = false
    8. that.subscribeSuccess = false
    9. }
    10. function handleOnReConnect (that){
    11. return () => {
    12. that.retryTimes += 1
    13. if (that.retryTimes > 5) {
    14. try {
    15. that.client.end()
    16. initData(that)
    17. that.$message.error('Connection maxReconnectTimes limit, stop retry')
    18. } catch (error) {
    19. that.$message.error(error.toString())
    20. }
    21. }
    22. }
    23. }
    24. function createConnection (that) {
    25. try {
    26. that.connecting = true
    27. const { protocol, host, port, endpoint, ...options } = that.connection
    28. const connectUrl = `${protocol}://${host}:${port}${endpoint}`
    29. // ws://172.16.66.164:1883/mqtt
    30. that.client = mqtt.connect(connectUrl, options)
    31. // oooooooooooooooooooooooooooo
    32. // console.log(connectUrl, options)
    33. console.log(that.client.on, 'ppppp')
    34. if (that.client.on) {
    35. that.client.on('connect', () => {
    36. that.connecting = false
    37. console.log('Connection succeeded!')
    38. })
    39. that.client.on('reconnect', handleOnReConnect(that))
    40. that.client.on('error', error => {
    41. console.log('6666666666666666666666')
    42. console.log('Connection failed', error)
    43. })
    44. that.client.on('message', (topic, message) => {
    45. that.receiveNews = that.receiveNews.concat(message)
    46. // return `Received message ${message} from topic ${topic}`
    47. // console.log(`Received message ${message} from topic ${topic}`)
    48. })
    49. }
    50. } catch (error) {
    51. that.connecting = false
    52. console.log('mqtt.connect error', error)
    53. }
    54. }
    55. function doSubscribe (that){
    56. const { topic, qos } = that.subscription
    57. that.client.subscribe(topic, { qos }, (error, res) => {
    58. if (error) {
    59. console.log('Subscribe to topics error', error)
    60. return
    61. }
    62. that.subscribeSuccess = true
    63. console.log('Subscribe to topics res', res)
    64. })
    65. }
    66. export {createConnection, doSubscribe}

    引入页面

    1. <template>
    2. <div class="home-container">
    3. div>
    4. template>
    5. <script>
    6. import {createConnection, doSubscribe} from '@/utils/websock'
    7. // import Websocket from '@/components/websock.vue'
    8. export default {
    9. name: 'Home',
    10. // components: { Websocket },
    11. data () {
    12. return {
    13. connection: {
    14. protocol: 'wss',
    15. host: 'test.mosquitto.org',
    16. // server less服务器只有两种协议:mqtts: 8883; wss: 8084
    17. port: 8081,
    18. endpoint: '/mqtt',
    19. // endpoint: '/999/999/0007/scu/+/pressure1',
    20. // for more options, please refer to https://github.com/mqttjs/MQTT.js#mqttclientstreambuilder-options
    21. clean: true,
    22. connectTimeout: 30 * 1000, // ms
    23. reconnectPeriod: 4000, // ms
    24. clientId:
    25. 'emqx_vue_' +
    26. Math.random()
    27. .toString(16)
    28. .substring(2, 8),
    29. // auth
    30. username: '',
    31. password: ''
    32. },
    33. subscription: {
    34. topic: '/+',
    35. qos: 0
    36. },
    37. receiveNews: ''
    38. }
    39. },
    40. watch: {
    41. receiveNews: {
    42. handler (newValue, _oldValue) {
    43. console.log(newValue)
    44. }}
    45. },
    46. mounted () {
    47. this.getdata()
    48. },
    49. methods: {
    50. getdata (){
    51. createConnection(this)
    52. doSubscribe(this)
    53. }
    54. }
    55. }
    56. script>
    57. <style lang="scss">style>

     

  • 相关阅读:
    Linux常用命令
    Arcgis pro通过渔网工具生成规则采样点,并对栅格数据进行采样
    安装tldr
    运维 | 如何在 Linux 系统中删除软链接 | Linux
    PDF编辑工具Acrobat Pro DC 2023中文
    ES6 类
    QT-opengl编译错误
    【LeetCode】——链式二叉树经典OJ题详解
    可以直接打开小皮面板中的网站运行php文件,昨天下载了数据库插件,一直提示“服务器连接错误,如何解决?(相关搜索:建立数据库)
    异步编程集
  • 原文地址:https://blog.csdn.net/weixin_42327537/article/details/134078915