✍️
ZiXiang-Blog
  • Jerry Wang Blog
  • 使用 TorchServe 部署 Model
  • How do AppRTC work in WebRTC mechanism ?
  • [系統設計]- 容易產生設計盲點
  • Golang
    • [Golang] Goroutine Concurrency多執行緒淺談
    • [Golang]: 進階用法
    • Golang go mod 入門
    • [Golang] 讓 Goroutine Debug 變得更簡單
  • Security
    • HTTP Token 使用方式: Basic Token v.s Bearer Token
    • 從 RFC 規格書觀點解析 OAuth 2.0
    • 區塊鏈物聯網架構 解決哪些安全性議題?
  • Broker
    • [深入淺出MQTT]: v3.1.1與v5 的差異
    • Broker 到底是什麼?
  • patterns
    • Patterns for Organizing Domain Logic
    • Domain Model: 從無到有規劃新的服務
    • 淺談CQRS
    • Backend System in Microservice Architecture: Where Does data store?
    • Ambassador pattern with Shared Database Pattern
    • Microservice Pattern 犧牲了什麼?
  • K8s
    • 分散式系統: 現代軟體架構與設計考量
    • Kubernetes 超入門
    • Kubernetes 架構
    • K8s 在 DevOps 中的作用
  • Database
    • Relation Database Index Overview
    • Draft: RDBMS(MySQL) v.s NoSQL (Monogo)
    • [淺談]- How Do RDBMS Thread work ?
    • [淺談]-NoSQL資料庫怎麼選?
    • How do pick the database more correctly?
    • Draft: 關聯式資料庫需要知道的幾件事
  • HTTP
    • Overview The WebSocket Mechanism
Powered by GitBook
On this page
  • The Design of Websockets
  • WebSocket handshake
  • Three Control Frame: Ping, Pong, Close
  • Conclusion

Was this helpful?

  1. HTTP

Overview The WebSocket Mechanism

This article introduces What’s the Websocket and how it works.

PreviousDraft: 關聯式資料庫需要知道的幾件事

Last updated 4 years ago

Was this helpful?

WebSocket is a popular tool in HTTP 1.1, that realized full-duplex on HTTP model. It upgrades HTTP 1.1 to let the TCP channel still open until the request or response side closes the communication channel.

In the past without the WebSocket age, there are two mechanisms to achieve duplex. One is "Long Polling" the other one is "HTTP stream", they manipulate the request channel time to simulate duplex[1]. On the other worlds, that just don't close HTTP Request. But it have a drawback that caused server big loading but WebSocket can solve these problem

Detail in attachment about " Long Polling" and " HTTP Stream"

The Design of Websockets

The WebSocket build connection have two steps. Client should start TCP connection and then use HTTP GET to verify HTTP upgrade process (Websocket HandShake) before staring transmit data.

WebSocket handshake

WebScoket handshake base on HTTP 1.1 to do this. The main purpose is client side tells HTTP Server we want to upgrade to WebSocket.(Figure WebSocket handshake sequence diagram)

Something Important: ● Sec-WebSocket-key, Sec-Websocket-Accept: The purpose is that proves the handshake was received \1. Sec-Websocket-key is a UUID that encoded by Base64. \2. Sec-Websocket-Accept is the string that Sec-Websocket-key concatenates with decode of Sec-Websocket-key. The string will encode SHA-1 and encode Base64 before send back it.

● Sec-WebSocket-Version: The value of this header field MUST be 13.

Three Control Frame: Ping, Pong, Close

● Ping Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame.

● Pong (not only reply ping but it can serve as a unidirectional heartbeat) If an endpoint receives not only one ping, that May elect the most recent ping to reply a Pong.

● Close If the endpoint receives a close control frame, it MUST send a Close frame in response.

Sometimes endpoint receive a close frame, but some data still transmit. In that situation, endpoint can send a close frame after this transmit.

Conclusion

WebSocket is so popular on Web event implementation but seldom do people realize how it works. In this article, have some essential knowledge to help people develop the WebSocket Application. Especially WebSocket handshake, so many modules should set these arguments.

Attachment: ● Long Polling: When a client sends a request but the server does not close this connection a period. if it is closed, the client will still resend a request to do it again.

● HTTP Stream: When the client sends a request but the server never closes this connection.

In both of them, the Server response still has an HTTP header and no mechanism to manage the endpoint connection status.

Reference [1]Loreto, S., et al. “RFC 6202-Known Issues and Best Practices for the Use of Long Polling and Streaming in Bidirectional HTTP.” IETF, Duben (2011). [2] Skvorc, Dejan, Matija Horvat, and Sinisa Srbljic. “Performance evaluation of Websocket protocol for implementation of full-duplex web streams.” 2014 37th International Convention on Information and Communication Technology, Electronics and Microelectronics (MIPRO). IEEE, 2014. [3]Fette, Ian, and Alexey Melnikov. “Rfc 6455: The websocket protocol.” IETF, December (2011).

WebSocket handshake sequence diagram
Sec-WebSocket-key, Sec-Websocket-Accept activity diagram

● Origin: The Origin header field is used to protect against unauthorized cross-origin use of a WebSocket server by scripts using the WebSocket API in a web browser. example: if code downloaded from attempts to establish a connection to ww2.example.com, the value of the header field would be ""

Ping and Pong example
when receive close control frame, endpoint can done it before send close back
www.example.com
http://www.example.com