美文网首页vue
vue中websocket用法及封装

vue中websocket用法及封装

作者: 一只正在成长的程序猿 | 来源:发表于2019-07-25 19:10 被阅读73次

    1.先下载

    cnpm install vue-socket.io --save-dev
    

    2.在src文件夹里面新建lib文件夹,在lib文件夹里面新建socket.js

    import Vue from 'vue'
    import VueSocketIO from 'vue-socket.io'
    import axios from 'axios';
    
    
    var a;
    var wesocket = {
        debug: true,
        connection: "",
        vuex: {}
    }
    
    
    //ajax请求接口返回websocket连接
    axios({
        method: 'GET',
        url: 'http://192.168.0.110:8091/api/getpushurl',
        headers: {
            'Content-Type': 'application/json'
        },
    }).then(function(response) {
    
        wesocket.connection = response.data + '/price';
        if (wesocket.connection != "") {
            Vue.use(new VueSocketIO(wesocket))
    
        }
    
        console.log(response.data)
    
    });
    export default a;
    

    3.在main.js引入

    import VueSocketIO from './lib/socket'
    
    new Vue({
        el: '#app',
        VueSocketIO,
        components: {
            App
        },
        template: '<App/>'
    })
    

    4.在组件中的用法

    //跟methods同级 
    sockets: {
        connect() {
          this.id = this.$socket.id;
          // this.$socket.emit("sendMessageToServer", "0025520"); //监听connect事件
        },
        servermessage(data) {
          // 监听message事件,方法是后台定义和提供的
          console.log(data);
        }
      },
     methods: {
        // websocket
        clickButton: function(val) {
          //添加按钮事件向服务端发送数据
          this.$socket.emit("sendMessageToServer", val);
        },
       }
    

    参考链接vue

    参考链接ws

    参考链接NPM

    相关文章

      网友评论

        本文标题:vue中websocket用法及封装

        本文链接:https://www.haomeiwen.com/subject/hrskrctx.html