fastdo  0.6.16
ws 命名空间参考

WebSocket封装 更多...

struct  CloseFramePayloadData
 
struct  FrameBase
 WS帧基类 更多...
 
struct  FrameBasic
 
struct  FrameBasicMasked
 
struct  FrameExtended16
 
struct  FrameExtended16Masked
 
struct  FrameExtended64
 
struct  FrameExtended64Masked
 
class  WsHttpRequestCtx
 WebSocket客户请求场景 更多...
 
class  WsHttpServer
 WebSocket服务器 更多...
 

枚举

enum  OpCode : winux::uint {
  dataContinued = 0x0U, dataText = 0x1U, dataBinary = 0x2U, dataRsv3 = 0x3U,
  dataRsv4 = 0x4U, dataRsv5 = 0x5U, dataRsv6 = 0x6U, dataRsv7 = 0x7U,
  ctrlClose = 0x8U, ctrlPing = 0x9U, ctrlPong = 0xAU, ctrlRsvB = 0xBU,
  ctrlRsvC = 0xCU, ctrlRsvD = 0xDU, ctrlRsvE = 0xEU, ctrlRsvF = 0xFU
}
 操作码 更多...
 
enum  WebSocketErrorCode { errNothing, errCnnUnexpectedInvalid }
 WebSocket错误码 更多...
 
enum  FrameType {
  ftBasic = 0b0010, ftBasicMasked = 0b0011, ftExtended16 = 0b0100, ftExtended16Masked = 0b0101,
  ftExtended64 = 0b1000, ftExtended64Masked = 0b1001
}
 数据帧类型 更多...
 
enum  ReadyState {
  stateNone, stateConnecting, stateOpen, stateClosing,
  stateClosed
}
 就绪状态 更多...
 

函数

winux::Buffer BuildFrame (OpCode opcode, bool fin, bool mask, winux::byte *payloadData=nullptr, size_t payloadDataSize=0)
 
std::vector< winux::BufferBuildFramesFromData (OpCode opcode, bool mask, winux::Buffer payloadData=winux::Buffer(), size_t perFrameMaxPayloadSize=-1)
 

详细描述

WebSocket封装

-> 32 bits  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
-> 4 bytes  0               1               2               3               
bit order   7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
           +-+-+-+-+-------+-+-------------+-------------------------------+
           |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, if MASK set to 1 |
           +-------------------------------+-------------------------------+
           |    Masking-key (continued)    |         Payload data          |
           +-------------------------------- - - - - - - - - - - - - - - - +
           :                    Payload data continued ...                 :
           + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
           |                    Payload data continued ...                 |
           +---------------------------------------------------------------+
一个为未分片的消息(FIN为1,opcode非0)
一个分片的消息:
起始帧(FIN为0,opcode非0),
分片帧(0个或多个)(FIN为0,opcode为0),
结束帧(FIN为1,opcode为0)


HTTP升级为websocket连接的请求
GET /xxx HTTP/1.1
Host: hostname[:port]
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: 随机生成的16字节的值用BASE64编码后的字符串
Origin: 其值为请求发起页面的scheme://hostname (因为中间可能经过代理才到达服务器,服务器可以根据该字段选择是否和客户端建立连接)
Sec-WebSocket-Version: 13
[Sec-WebSocket-Protocol: 其值为由逗号分隔的子协议的名字,按优先度排序,每个名字必须唯一]
[Sec-WebSocket-Extension: 表示协议级别的扩展]


连接建立的合法响应
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 其值为 Base64( SHA1( RequestHeader{Sec-WebSocket-Key} + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ) )
[Sec-WebSocket-Protocol: 其值为请求发来的子协议中的一个或空]

WebSocket数据帧根据Payload-len来决定帧头部大小
Payload-len < 126: 2bytes
Payload-len ==126: 4bytes
Payload-len ==127: 10bytes
除此之外,如果mask位为1,则包含4bytes的掩码Key,即
Payload-len < 126: 2+4=6bytes
Payload-len ==126: 4+4=8bytes
Payload-len ==127: 10+4=14bytes

枚举类型说明

操作码

枚举值
dataContinued 

continuation frame

dataText 

text frame

dataBinary 

binary frame

dataRsv3 
dataRsv4 
dataRsv5 
dataRsv6 
dataRsv7 
ctrlClose 

close frame

ctrlPing 

ping frame

ctrlPong 

pong frame

ctrlRsvB 
ctrlRsvC 
ctrlRsvD 
ctrlRsvE 
ctrlRsvF 

在文件 ws_websocket.hpp65 行定义.

WebSocket错误码

枚举值
errNothing 

无错误

errCnnUnexpectedInvalid 

连接意外无效

在文件 ws_websocket.hpp86 行定义.

数据帧类型

枚举值
ftBasic 
ftBasicMasked 
ftExtended16 
ftExtended16Masked 
ftExtended64 
ftExtended64Masked 

在文件 ws_websocket.hpp94 行定义.

就绪状态

枚举值
stateNone 

没有状态,尚未触发WS功能

stateConnecting 

连接中

stateOpen 

已打开

stateClosing 

关闭中

stateClosed 

已关闭

在文件 ws_websocket.hpp105 行定义.

函数说明

winux::Buffer ws::BuildFrame ( OpCode  opcode,
bool  fin,
bool  mask,
winux::byte payloadData = nullptr,
size_t  payloadDataSize = 0 
)
std::vector<winux::Buffer> ws::BuildFramesFromData ( OpCode  opcode,
bool  mask,
winux::Buffer  payloadData = winux::Buffer(),
size_t  perFrameMaxPayloadSize = -1 
)