2012-02-09 56 views
1

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

当服务器发送客户端的握手回应铬始终显示在下面的控制台错误:

期间的WebSocket握手

错误:二段的WebSocket,接受不匹配

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 

请帮忙。

+1

您发送的接受报头中的值是错误的。你需要显示你的代码来计算accept值,否则我们不知道你在做什么错误。 – kanaka 2012-02-09 15:17:42

+0

谢谢,你说得对,服务器发送错误的密钥。 – 2012-02-10 03:19:43

+0

当计算密钥时,输入密钥以'/ r/n'结束,这就是我总是输出错误的原因。 – 2012-02-10 03:20:49

回答

1

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 

获取更多的信息here