16. Vue JS/Vue 2

11. Vue 2 - SockJS

THE HEYDAZE 2020. 9. 29. 08:45
OS Windows 10 Home 64bit 버전 1903 (OS 빌드 18362.836)
Vue 2.5.13

 

# sock (서버)와 sock-client (클라이언트)로 나뉜다

 

  메소드   설명
  onopen   서버와 연결 됐을 때 호출
  onmessage   클라이언트가 서버로부터 메시지를 받을 때 호출
  onclose   웹 소켓 연결이 종료됐을 때 호출
  onerror   에러가 발생하면 호출
  send   서버로 데이터 전송 (blob, ArrayBuffer, ArrayBufferView)
  close   클라이언트와 서버 통신 종료

 

 

HTML Standard

This section is non-normative. To enable web applications to maintain bidirectional communications with server-side processes, this specification introduces the WebSocket interface. This interface does not allow for raw access to the underlying network. Fo

html.spec.whatwg.org

 

/** 서버와 연결 됐을 때 호출 */
socket.onopen = function (event) {
    console.log(event)
    console.log(socket.readyState)
}

/** 클라이언트가 서버로부터 메시지를 받을 때 호출 */
socket.onmessage = function (message) {
    console.log(message)
}

/** 웹 소켓 연결이 종료됐을 때 호출 */
socket.onclose = function (event) {
    console.log(event)
}

/** 에러가 발생하면 호출 */
socket.onerror = function (error) {
    console.log(error)
}