same-orgins:瀏覽器同源策略的安全模型
持久化協(xié)議
雙向雙工 多路復(fù)用, 同時發(fā)信息
http只能由客戶端發(fā)起,一個request對應(yīng)一個response
http每次通信需要發(fā)起連接,或者keepalived只能服用連接
http 頭信息繁重
單一的TCP連接,采用全雙工通信 vs 單向傳送,需要多條連接
對代理,防火墻和路由透明: 基于http協(xié)議結(jié)構(gòu),使用相同80 443端口
連接建立后,信息傳輸無頭部信息,減少網(wǎng)絡(luò)帶寬消耗、cookie和身份驗證
無安全開銷
通過ping/pong幀保持鏈路激活
服務(wù)器可以推消息。
websocket本質(zhì)上是一個TCP連接
建立連接:發(fā)送http請求(upgrade:websocket升級),TCP三次握手,建立連接。
請求:
GET / HTTP/1.1 Host: Upgrade: websocket Connection: Upgrade Sec-Websocket-Key: BASE-64編碼(randomly selected 16-byte value) Origin: cross-origin check Sec-Websocket-Protocal: chat,superchat //客戶端希望使用的協(xié)議,one or more comma-separated subprotocol the client wishes to speak, ordered by preference Sec-Websocket-Version: 13(must be)
回復(fù): HTTP/1.1 101 Switching Protocals //協(xié)議轉(zhuǎn)換 Upgrade: websocket Connection: Upgrade Sec-Websocket-Accept: BASE-64編碼(SHA-1加密(Sec-Websocket-Key==GUID(Globally Unique Identifier)("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))) Sec-Websocket-protocal: chat
傳送數(shù)據(jù):文本、二進(jìn)制、控制幀等
handshake and then data transfer
基于TCP的獨立協(xié)議;
由http server解析握手連接(Upgrade request) 80 443
ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ] wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
host = <host, defined in [RFC3986], Section 3.2.2> port = <port, defined in [RFC3986], Section 3.2.3> path = <path-abempty, defined in [RFC3986], Section 3.3> query = <query, defined in [RFC3986], Section 3.4>
if the client is a web browser, it supplies /origin/.
數(shù)據(jù)傳輸:一連串的幀(frames)
二進(jìn)制:
協(xié)議:
|Opcode | Meaning | Reference | -+--------+-------------------------------------+-----------| | 0 | Continuation Frame | RFC 6455 | -+--------+-------------------------------------+-----------| | 1 | Text Frame | RFC 6455 | -+--------+-------------------------------------+-----------| | 2 | Binary Frame | RFC 6455 | -+--------+-------------------------------------+-----------| | 8 | Connection Close Frame | RFC 6455 | -+--------+-------------------------------------+-----------| | 9 | Ping Frame | RFC 6455 | -+--------+-------------------------------------+-----------| | 10 | Pong Frame | RFC 6455 | -+--------+-------------------------------------+-----------|
fin:標(biāo)識消息的最后一部分 opcode:消息體說明
%xB-F are reserved for further control frames
mask: 1 masked,0 unmasked
當(dāng)為1時,mask-key賦值,用于服務(wù)器端 unmask payload data
客戶端發(fā)送到服務(wù)端的數(shù)據(jù)都必須mask
payload length:7 bits, 7+16 bits, or 7+64 bits
7 bits:0-125 payload length
7+16 bits:16-bit unsigned integer are the payload length
7+64 bits:64-bit unsigned integer (the most significant bit MUST be 0) are the payload length
The payload length is the length of the "Extension data" + the length of the "Application data".
masking-key:0 | 4 bytes, chosen at random by the client,不影響payload data的數(shù)據(jù)長度
payload data:Extension data + Application data
數(shù)據(jù)分片:fragmentation:
main purpose:允許在不緩存完整消息的情況下發(fā)送未知大小的消息。使用數(shù)據(jù)分片,服務(wù)器或者中間媒介可以選擇一個合適大小的緩存,來緩存及發(fā)送fragment
sub:multiplexing,多路分發(fā),
Control frames MAY be injected in the middle of a fragmented message. Control frames themselves MUST NOT be fragmented.
delivered in the order send by the sender
不同消息不允許交叉存取分片
基本傳輸結(jié)構(gòu):
A fragmented message consists of a single frame with the FIN bit clear and an opcode other than 0, followed by zero or more frames with the FIN bit clear and the opcode set to 0, and terminated by a single frame with the FIN bit set and an pcode of 0.
control frames:
0x8 (Close), 0x9 (Ping), 0xA (Pong), 0xB-0xF reserved
communicate state with websocket
can be interjected in the middle of a fragmented message: 減少因為大體量消息控制幀的延遲
payload length < 125
must not be fragmented
連接斷開 close frame:
客戶端發(fā)送close frame
客戶端等待服務(wù)端斷開連接,
當(dāng)服務(wù)端收到close frame, 會等待消息發(fā)送完畢再返回close frame
服務(wù)端立即斷開tcp連接,
客戶端可以在任何時刻關(guān)閉連接。
ping frame: opcode---0x9 serve as a keepalive or as means to verify remote endpoint
pong frame:opcode---0xA
必須包含對應(yīng)ping的 Application data
回復(fù)最近的 ping frame
可能的服務(wù)器端單向主動的pong frame,不需要回復(fù)。
data frame:
opcode---0x1:text data encoded as UTF-8
opcode---0x2:binary
websocket 可以使用任何http使用的安全驗證方式。
websocket機(jī)密性和完整性通過使用 websocket protocal over tls 即 wss
底層網(wǎng)絡(luò)協(xié)議,
傾向于使瀏覽器成為一個與操作系統(tǒng)相仿的應(yīng)用程序平臺。
低級網(wǎng)絡(luò)API無法處理源安全模型。
實時通訊;支持二進(jìn)制和字符串傳輸。
SPDY:擴(kuò)充了Http,通過壓縮http首部標(biāo)識和多路復(fù)用,工作管道等手段改進(jìn)http請求性能。改進(jìn)網(wǎng)頁性能。http的修改形式,修改了許多http非本質(zhì)問題,
Websocket API是完全事件驅(qū)動的,自動發(fā)送數(shù)據(jù)和通知。遵循異步編程模式。監(jiān)聽事件。open message error close。String Blob ArrayBuffer var blob = new Blob("blob data"); ws.send(blob); var array = new Unit8Array([2,3,4,5,6]); ws.send(array.buffer);
readyState
websocket send()時立即生效的,但是瀏覽器對于發(fā)送的數(shù)據(jù)會進(jìn)行緩存;使用bufferedAmount檢查已經(jīng)進(jìn)入隊列但是尚未發(fā)送到服務(wù)器的字節(jié)數(shù),不包括協(xié)議幀組或者操作系統(tǒng),網(wǎng)絡(luò)硬件所進(jìn)行的緩存。
關(guān)閉前檢查bufferedAmount是否有為發(fā)送數(shù)據(jù)。
傳輸層協(xié)議。
websocket pingpong機(jī)制,保持連接存活。
更多建議: