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响应标头。

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

客户端发送一个 Sec-WebSocket-Key,这是一个随机值,具有 已进行 base64 编码。若要形成响应,GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11附加到此底座64 编码的密钥。不会先解码 base64 编码的密钥。[10] 然后使用 SHA-1 对生成的字符串进行哈希处理,然后进行 base64 编码。 最后,生成的回复出现在标头中 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。

相关内容

  • 没有找到相关文章

最新更新