为什么一定要用vue-socket.io,原生socket.io-client实现动态url不香么

前段时间公司项目要用到socket实现推送功能,接到的任务就是使用vue-socket.io,这.....当时作为socket小白的我,就这么翻呀翻它的文档,搞了许久,效果不理想(当时的我不能实现组件内new......)

辗转了几天,实在无果,看看了他的封装,也是基于socket.io-client,那我为啥不直接用socket.io-client来实现哩,不多说,直接上代码,记录下socketio的初次学习

 // .vue

 import SocketIO from "socket.io-client";
 import axios from "@/router/axios";


 data() {
    return {
        socket: null
      }
    },

 mounted() {
   this.newSocket()
 },

 beforeDestroy() {
      this.socket && this.socket.ondisconnect()
    },

 methods: {
    async newSocket () {
        const token = this.access_token
        const res = await axios ( {
          method: "get",
          url: "/socket/socket/ws",
          headers: {'X-Requested-With': 'XMLHttpRequest',
            'Authorization':  'Bearer ' + token
          },
        })
        this.socket = SocketIO(res.data.data.url,{transports:['websocket','xhr-polling','jsonp-polling']})
        this.socket.on('connect', () => {
          console.log('连接成功')
        })
        this.socket.on('response', (res) => {
          this.$message.success(res.msg)
        })
        this.socket.on('chat_message', (msg) => {
          this.messages.push(msg)
        })
        this.socket.on('broadcast', (data) => {
          let res = this.$refs.audio.play()
          const h = this.$createElement;
          this.$notify({
            title: '新消息',
            duration:0,
            message: h('i', { style: 'color: teal'}, data.message)
          })
        })
        this.socket.on('disconnect', () => {
          console.log('断开连接')
        })
      },
 }

 

Vue2使用vue-socket.io可以实现Socket.io的集成,实现实时聊天等功能。首先,需要在项目中引入vue-socket.io扩展,可以通过npm安装vue-socket.io并引入。 在Vue实例中,使用Vue.use()来注册并使用Vue-socket.io。在注册时,需要传入相应的配置参数,包括debug、connection、vuex等。其中,debug用于调试,可以选择关闭;connection用于指定Socket.io的连接地址;vuex用于在Vuex中使用Socket.io的事件监听。 具体实现步骤如下: 1. 首先,安装vue-socket.io扩展:npm install vue-socket.io 2. 在Vue实例中引入VueVueSocketIO,并配置相应的参数。 3. 创建Vuex store,并在配置参数中指定vuex为store的实例。 4. 在Vue实例中使用Vue.use()注册Vue-socket.io,并传入配置参数。 这样,Vue2就可以使用vue-socket.io扩展来实现Socket.io的集成了。可以使用Vue-socket.io提供的方法来监听Socket事件,并实现相应的功能。 总结一下,Vue2使用vue-socket.io扩展可以实现Socket.io的集成,通过注册Vue-socket.io并传入相应的配置参数,可以在Vue实例中实现Socket事件的监听和处理。这样就可以实现实时聊天等功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue 使用 Vue-socket.io 实现即时聊天应用(vuex管理)](https://blog.csdn.net/weixin_47746452/article/details/121330186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue-socket.io-extended:Vue.jsVuex的Socket.io绑定(受Vue-Socket.io启发)](https://download.csdn.net/download/weixin_42160425/18170249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue 使用 Vue-socket.io 实现即时聊天应用(实战篇 一)](https://blog.csdn.net/weixin_47746452/article/details/121359940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值