Chrome Websocket Handshake

时间:2015-01-20 13:35:05

标签: c# google-chrome websocket handshake

我正在构建一个Web套接字服务器(对我来说非常头疼),它的结果比我想象的要困难得多。

目前,我遇到了让chrome接受握手的问题。出于某种原因,它在Firefox中运行良好,但在chrome中 - 连接立即关闭。

客户握手

    GET /chat HTTP/1.1
GET /chat HTTP/1.1
    Host: localhost:8181
Host: localhost:8181
    Connection: Upgrade
Connection: Upgrade
    Pragma: no-cache
Pragma: no-cache
    Cache-Control: no-cache
Cache-Control: no-cache
    Upgrade: websocket
Upgrade: websocket
    Origin: http://localhost:8080
Origin: http://localhost:8080
    Sec-WebSocket-Version: 13
Sec-WebSocket-Version: 13
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
    Accept-Encoding: gzip, deflate, sdch
Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8
Accept-Language: en-US,en;q=0.8
    Sec-WebSocket-Key: ANzq7Z0GL4lfvw518WOnig==
Sec-WebSocket-Key: ANzq7Z0GL4lfvw518WOnig==
    Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

我的回答如下:

String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
                              "Upgrade: websocket" + Environment.NewLine +
                              "Connection: Upgrade" + Environment.NewLine +
                              "Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
                              "Sec-WebSocket-Protocol: " + protocol + Environment.NewLine +
                              Environment.NewLine;

其中wsAccept = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))

protocol = "chat";

是否有任何网站或任何我可以用作参考的东西?我觉得我在这里画一个空白。

非常感谢任何帮助。

由于

解决

我终于得到了怎么做。我发送了很多参数,最后一个

"Sec-WebSocket-Protocol: " + protocol 

多余了!所以握手应该是这样的:

String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
                              "Upgrade: websocket" + Environment.NewLine +
                              "Connection: Upgrade" + Environment.NewLine +
                              "Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
                               Environment.NewLine;

希望这可以帮助任何面临类似问题的人!

2 个答案:

答案 0 :(得分:1)

您缺少Sec-WebSocket-Accept响应标题。

查看http://en.wikipedia.org/wiki/WebSocket#WebSocket_protocol_handshake

  

客户端发送一个Sec-WebSocket-Key,它是一个随机值   已经base64编码。要形成响应,GUID   258EAFA5-E914-47DA-95CA-C5AB0DC85B11附加到此base64   编码密钥。 base64编码密钥不会被首先解码。[10]该   然后使用SHA-1对结果字符串进行散列,然后对base64进行编码。   最后,结果回复发生在标题中   仲的WebSocket - 接受。

读:

https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_servers

https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_server#Handshaking

答案 1 :(得分:0)

String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
                              "Upgrade: websocket" + Environment.NewLine +
                              "Connection: Upgrade" + Environment.NewLine +
                              "Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
                               Environment.NewLine;

之前发送了太多属性。适用于iOS / 8.1,Chrome / 40和Firefox / 35。