Chrome Websocket 握手

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;

希望这对面临类似问题的任何人有所帮助!

您缺少 Sec-WebSocket-Accept 响应 header。

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

The client sends a Sec-WebSocket-Key which is a random value that has been base64 encoded. To form a response, the GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 is appended to this base64 encoded key. The base64 encoded key will not be decoded first.[10] The resulting string is then hashed with SHA-1, then base64 encoded. Finally, the resulting reply occurs in the header Sec-WebSocket-Accept.

阅读:

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

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

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.