fastdo
0.6.16
|
WebSocket封装 更多...
类 | |
struct | CloseFramePayloadData |
struct | FrameBase |
WS帧基类 更多... | |
struct | FrameBasic |
struct | FrameBasicMasked |
struct | FrameExtended16 |
struct | FrameExtended16Masked |
struct | FrameExtended64 |
struct | FrameExtended64Masked |
struct | WebSocketWrapper |
WebSocket场景包装 更多... | |
class | WsHttpClientCtx |
WsHTTP客户场景 更多... | |
class | WsHttpServer |
WsHTTP服务端 更多... | |
枚举 | |
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 { stateConnecting, stateOpen, stateClosing, stateClosed } |
就绪状态 更多... | |
函数 | |
bool | SendWebSocketFrame (eiennet::Socket *sock, OpCode opcode, bool fin, bool mask, winux::byte *payloadData=nullptr, size_t payloadDataSize=0) |
bool | SendWebSocketBuffer (eiennet::Socket *sock, OpCode opcode, bool mask, winux::Buffer payloadData=winux::Buffer(), size_t perFrameMaxPayloadSize=-1) |
bool | SendWebSocketAnsi (eiennet::Socket *sock, OpCode opcode, bool mask, winux::AnsiString payloadData=winux::AnsiString(), 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
操作码
在文件 http_old.hpp 第 158 行定义.
数据帧类型
枚举值 | |
---|---|
ftBasic | |
ftBasicMasked | |
ftExtended16 | |
ftExtended16Masked | |
ftExtended64 | |
ftExtended64Masked |
在文件 http_old.hpp 第 187 行定义.
就绪状态
枚举值 | |
---|---|
stateConnecting |
连接中 |
stateOpen |
已打开 |
stateClosing |
关闭中 |
stateClosed |
已关闭 |
在文件 http_old.hpp 第 198 行定义.
bool http::old_v1::ws::SendWebSocketFrame | ( | eiennet::Socket * | sock, |
OpCode | opcode, | ||
bool | fin, | ||
bool | mask, | ||
winux::byte * | payloadData = nullptr , |
||
size_t | payloadDataSize = 0 |
||
) |
bool http::old_v1::ws::SendWebSocketBuffer | ( | eiennet::Socket * | sock, |
OpCode | opcode, | ||
bool | mask, | ||
winux::Buffer | payloadData = winux::Buffer() , |
||
size_t | perFrameMaxPayloadSize = -1 |
||
) |
bool http::old_v1::ws::SendWebSocketAnsi | ( | eiennet::Socket * | sock, |
OpCode | opcode, | ||
bool | mask, | ||
winux::AnsiString | payloadData = winux::AnsiString() , |
||
size_t | perFrameMaxPayloadSize = -1 |
||
) |