【问题标题】:Chrome Websocket HandshakeChrome Websocket 握手
【发布时间】:2015-01-20 13:35:04
【问题描述】:

我正在构建一个 Web-socket 服务器(对我来说非常头疼),结果证明它比我预期的要困难得多。

目前,我无法让 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;

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

【问题讨论】:

    标签: c# google-chrome websocket handshake


    【解决方案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 编码。 最后,结果回复出现在标头中 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

    【讨论】:

    • 感谢您的回答,但是,我没有给您正确的答复。我已经用真实的回应更新了答案。
    • 你确定问题是握手吗?您在 Google Chrome 控制台中看到任何错误吗?
    • 我已经尝试阅读网络内部 - 但我在套接字下找到的都是 UDP 套接字 - 我应该寻找什么?我仍然相信握手时出现问题,因为它在套接字“打开”时立即失败。奇怪的是,它在 Firefox 中运行良好。
    • WebSockets 使用 TCP。没有看到你的代码很难说。我用 C# .NET 开发了一个 WebSocket 服务器,看看它可能对你感兴趣:github.com/vtortola/WebSocketListener
    【解决方案2】:
    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2018-10-16
      • 1970-01-01
      • 2015-03-16
      • 2015-01-04
      相关资源
      最近更新 更多