프로그램/websocket

Web Socket Details

mulderu 2013. 5. 18. 21:13

요즘 websocket 관련 기술발전이 hot  합니다.

관련된 정보를 좀 정리 했습니다. 출처는 아래와 같습니다.

ref : http://cbeams.github.io/bitcoin-rt/

---------------------------------------------------------------------

Protocol Details

  • TCP - Based protocol
  • HTTP used solely for upgrade request (Status Code 101)
  • Bi-directional, full-duplex
  • Data Frames can be Text (UTF-8) or arbitary Binary data
양방향...통신용이고 UTF-8 인코딩과 바이너리를 전송할 수 있다가 프로토콜 원칙이군요.

Schemes

  • Unencrypted : ws://
  • Encrypted : wss://
두번째 방식의 암호화 채널을 권장 한다고 합니다.. (SSL)

Handshake

  • Request : Sec-WebSocket-Key Header
  • Response - 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
  • Appended to key + SHA-1 + base64
  • Sec-WebSocket-Accept Header
요청 헤더와 응답헤더에 위와같은 패턴을 사용하는군요.

Control Frames
  • Communicate state about the WebSocket
  • Close (0x08)
  • Ping (0x9)
  • Pong (0xA)
  • More possible in future
  • 125 bytes or less

HTML5 / WebSocket

  • W3C Candidate Recommendation 20 Sep 2012
  • Binary data supported : Blob or ArrayBuffer format
  • Cna inspect extensions (read-only)
  • No support for ping/pong frames
진짜 얼마안된 기술입니다. 


readyState attribute

  • CONNECTING (0) connection not yet established
  • OPEN (1) Connection is established + communication possible
  • CLOSING (2) Connection going through closing handshake / close () method called
  • CLOSED (3) Connection is closed / could not be opened

Event Handlers

javascript 개발자들에게는 자장 많이 보는 내용이죠..
  • onopen :  말그데로 채널이 열렸다는 이벤트
  • onmessage : 메시지를 받았으니 처리하라는 이벤트
  • onerror : 문제.. 생겼으니 처리해라
  • onclose : 문닫았다... 장사 그만한다 ㅋㅋ , 채널종료.
장황한 설명보단 ... 코드 몇줄이 더욱 이해를 도울겁니다. 

example




Code Sample

var socket = new WebSocket ("ws://localhost:8080/websocket-servlet/tomcat");
...
socket.onmessage = function (event) {
    console.log (event.data);
    var trade = JSON.parse(event.data);
    ...
   };
...


Tomcat
  • WebSocketServlet
  • Since 7.0.27 (03/2012)
  • Backport to 6.0.35 Issue 52918
  • Fairly minimal, server-side only
  • http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html

Jetty
  • Since Jetty 7.x (early adoption, complex)
  • Revised in Jetty 9
  • Builds on Java 7, messages not frames, annotations

위내용 말고도 상당히 많은 이야기거리가 있더군요... 
이놈의 공부할건 왜이리 많은지, 걱정입니다.