websocket handshake



我正在使用c++编写一个websocket服务器,我在握手中挂起了使用chrome 17作为客户端。

当服务器向客户端发送握手响应时,chrome总是在控制台中显示以下错误:

WebSocket握手期间出错:Sec-Webocket Accept 不匹配

chrome中的事件如下:

t=1328796971951 [st= 5]    WEB_SOCKET_SEND_REQUEST_HEADERS  
--> GET / HTTP/1.1   
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:38950
Origin: null     
Sec-WebSocket-Key: zMb+UCeRb+2OmMp9fpbxHw==
Sec-WebSocket-Version: 13
t=1328796971951 [st= 5]    SOCKET_STREAM_SENT     
t=1328796971971 [st=25]    SOCKET_STREAM_RECEIVED  
t=1328796971971 [st=25]    WEB_SOCKET_READ_RESPONSE_HEADERS  
--> HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: 4emBYsdkl0SxeUMGLNc0dFsI1/E=

t=1328796971972 [st=26] -REQUEST_ALIVE

请帮忙。

未正确计算Sec-WebSocket-Accept值。

如何计算值(伪代码):

// Getting the Sec-WebSocket-Key from the Request header
var sec_Websocket_Key = requestHeader["Sec-WebSocket-Key"];
// Adding the magic string to sec_Websocket_key
// sha1 hash this new value
var sec_Websocket_Key_Hash = (sec_Websocket-Key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").sha1();
// Get the Base64 String of the hash
var sec_Websocket_Accept = sec_Websocket_Key_Hash.toBase64String();
// sec_Websocket_Accept now is the correct value to set in the Header

在此处获取更多信息

最新更新