如何在vue实例中调用(香草)websocket.onmessage方法

时间:2018-08-11 22:57:33

标签: vue.js websocket vuex

设置一个vue应用程序以具有来自普通websockets的消息功能,因此没有socket.io等。接收消息部分使我感到困惑,因为通常不会调用它,因为一旦服务器将消息发送到客户端,它就不会被调用。以下是我想要做的,但在methodscomputed属性中是不允许的

this.$store.state.SocketModule.webSocket.onmessage = function (msg) {
  this.$store.dispatch('SocketModule/onmessage', msg)
  // handle the msg in vuex
}

所以我应该做类似的事情;

computed: {
  onmessage: this.$store.state.SocketModule.webSocket.onmessage = function (msg) 
    {
      this.$store.dispatch('SocketModule/onmessage', msg)
      // handle the msg in vuex
     }
}

那还能行吗? (已完成的套接字服务器)或类似的东西

<script>
   this.$store.state.SocketModule.webSocket.onmessage = function (msg) {
     this.$store.dispatch('SocketModule/onmessage', msg)
     // handle the msg in vuex
   }
export default {
   ...

服务器推送消息而不是从客户端获取消息的方式与之不同

0 个答案:

没有答案