IT이야기

vue 인스턴스의 websocket.onmessage 메서드를 호출하는 방법

cyworld 2022. 5. 3. 20:57
반응형

vue 인스턴스의 websocket.onmessage 메서드를 호출하는 방법

바닐라 웹세트에서 메시지 기능을 갖도록 vue 응용프로그램을 설정하여 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 {
   ...

클라이언트에서 메시지를 가져오는 대신 메시지를 푸시하는 서버는 다음 작업과 다름

참조URL: https://stackoverflow.com/questions/51804395/how-to-call-vanilla-websocket-onmessage-method-in-vue-instance

반응형