물론 스펙을 정독하고 이해하는 것이 정석이겠습니다만, 항상 쉬운 길을 찾게되네요.
하지만, 그 길을 걷다 보면 무슨 내용인지 전혀 파악이 되지 않을 때도 많습니다. ㅠㅠ
몇가지 reference link들을 정리하면서, websocket에 대해서 파악해 봅니다.
1. https://en.wikipedia.org/wiki/WebSocket
- WebSocket is located at layer 7 in the OSI model and depend on TCP at layer 4.
- WebSocket enables streams of messages on top of TCP.
2. https://www.mischianti.org/2020/12/07/websocket-on-arduino-esp8266-and-esp32-client-1/
- WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.
- The big difference from REST server is that in an http request you send request and you must wait response to have the data and start new request on the same connection, with the WS you can stream requests and stream responses than operate when you want.
그리고, 가장 중요한 그림이 나옵니다.
3. https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
- A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific protocol.
- WebSocket servers are often separate and specialized servers (for load-balancing or other practical reasons), so you will often use a reverse proxy (such as a regular HTTP server) to detect WebSocket handshakes, pre-process them, and send those clients to a real WebSocket server. This means that you don't have to bloat your server code with cookie and authentication handlers (for example).
- First, the server must listen for incoming socket connections using a standard TCP socket.
- The Sec-WebSocket-Accept header is important in that the server must derive it from the Sec-WebSocket-Key that the client sent to it. To get it, concatenate the client's Sec-WebSocket-Key and the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" together (it's a "magic string"), take the SHA-1 hash of the result, and return the base64 encoding of that hash.
Frame format
내용을 정리하자면 다음과 같습니다.
- HTTP와 유사하게 OSI 모델의 7 layer에 위치하는 application
- Full duplex로, client의 요청 없이도 서버가 데이터의 전송을 시작할 수 있음
결론적으로 "TCP protocol 상에서 돌아가는 application으로 서버와 clients간에 full duplex를 지원하는 프로토콜" 정도로 이해할 수 있어 보입니다.
- End -
반응형
'프로그래밍 > Network' 카테고리의 다른 글
[Network] Websocket - RFC6455 (0) | 2021.11.24 |
---|