设置一个vue应用程序以具有来自普通websockets的消息功能,因此没有socket.io等。接收消息部分使我感到困惑,因为通常不会调用它,因为一旦服务器将消息发送到客户端,它就不会被调用。以下是我想要做的,但在methods
或computed
属性中是不允许的
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 {
...
服务器推送消息而不是从客户端获取消息的方式与之不同