C.W.K.
Stream
Lesson 08 of 08 · published

Connection Lifecycle & Frame Format

~13 min · foundations, frames, close-codes

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

State machine 으로 보는 lifecycle

WebSocket connection 은 네 상태 사이를 움직여: CONNECTING (handshake 중) → OPEN (frame 흘러) → CLOSING (한쪽이 close 보냈고 다른 쪽 ack 중) → CLOSED (terminal). 브라우저가 WebSocket.readyState 로 0/1/2/3 노출해.

Frame

WebSocket data 는 byte stream 아니라 frame 의 sequence 야. 각 frame 에 작은 header (2-14 byte) 가 붙어: FIN bit (message 의 마지막 frame?), opcode (text, binary, ping, pong, close, continuation), mask bit, payload length. Client → server frame 은 항상 mask 되어있고 (per-frame XOR key), server → client 는 mask 안 됨. 이 masking 은 anti-cache-poisoning 이지 보안 아냐 — encryption 이랑 헷갈리지 마.

Close code

모든 깨끗한 close 에 code 붙어. 1000 은 normal close. 1001 은 'going away' (서버 shutdown, page 이동). 1006 은 'connection 죽었고 close frame 못 받음' 의 특수 abnormal-close code. 4000-4999 는 application-defined — 'session expired', 'kicked from room' 같은 거에 써.

Code

RFC 6455 frame 레이아웃·text
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |            (16/64)            |
|N|V|V|V|       |S|             |   (if payload len==126/127)   |
| |1|2|3|       |K|             |                               |
+-+-+-+-+-------+-+-------------+-------------------------------+
|     Extended payload length continued, if payload len == 127  |
+---------------------------------------------------------------+
|                          Masking-key                          |
+---------------------------------------------------------------+
|                          Payload data ...                     |
+---------------------------------------------------------------+
Opcode 참고표·text
0x0  continuation  - more of a fragmented message
0x1  text          - UTF-8 payload
0x2  binary        - bytes
0x8  close         - graceful shutdown
0x9  ping          - heartbeat request (RFC-level)
0xA  pong          - heartbeat response (RFC-level)
흔한 close code·text
1000  Normal closure        - clean shutdown
1001  Going away            - server shutting down or tab navigated
1002  Protocol error        - malformed frame
1003  Unsupported data      - text on a binary endpoint, e.g.
1006  Abnormal closure      - no close frame received (drop)
1008  Policy violation      - server-defined refusal
1009  Message too big
1011  Internal server error
4000-4999  Application use  - your codes (auth expired, room full, ...)

External links

Exercise

작은 WebSocket 클라 띄우고 Wireshark 나 브라우저 devtools 로 frame 캡처. text message 보내봐 — masked client→server frame 봐. echo 받아봐 — unmasked server→client frame 봐. 그 다음 대화 중간에 탭 갑자기 닫아 — 서버 로그에 close 1000 아니라 1006 떨어지는 거 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.