• Vue-Socket.io


    github地址:https://github.com/MetinSeylan/Vue-Socket.io

    安装:

    npm install vue-socket.io -S

    注册:

    import Vue from 'vue'
    import VueSocketio from 'vue-socket.io'
    
    Vue.use(VueSocketio, 'http://socketserver.com:1923')

    实例应用:

    var vm = new Vue({
      sockets:{ //将(socket.on)绑定事件放在sockets中
        connect: function(){
          console.log('socket connected')
        },
        customEmit: function(val){
          console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
        }
      },
      methods: {
        clickButton: function(val){
            // $socket is socket.io-client instance
            this.$socket.emit('emit_method', val);
        }
      }
    })

    创建一个新的监听器:

    this.$options.sockets.event_name = (data) => {
        console.log(data)
    }

    删除监听器:

    delete this.$options.sockets.event_name;

    触发服务端事件:

    this.$socket.emit('event_name', msg1,msg2,...);

    配合VUEX使用:

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex);
    
    export default new Vuex.Store({
        state: {
            connect: false,
            message: null
        },
        mutations:{
            SOCKET_CONNECT: (state,  status ) => {
                state.connect = true;
            },
            SOCKET_USER_MESSAGE: (state,  message) => {
                state.message = message;
            }
        },
        actions: {
            otherAction: (context, type) => {
                return true;
            },
            socket_userMessage: (context, message) => {
                context.dispatch('newMessage', message);
                context.commit('NEW_MESSAGE_RECEIVED', message);
                if (message.is_important) {
                    context.dispatch('alertImportantMessage', message);
                }
                ...
            }
        }
    })
  • 相关阅读:
    For each db / table
    转---网络上来的,做一个数组样的结构
    JAVA 相关资料
    转--也不知是哪位大侠写的了
    T-SQL切割字符串方法小结 2
    OPENQUERY
    行集函数专题
    行列转换
    第一章 SQL基础
    解释型语言与编译型语言的区别
  • 原文地址:https://www.cnblogs.com/fanlinqiang/p/7816764.html
Copyright © 2020-2023  润新知