WebSocket客户端握手失败



我最近开始玩HTML5 WebSockets,我有一个简单的jQuery web客户端和一个控制台应用服务器。在阅读了关于握手的内容之后,以下是我目前为客户准备的内容:

var socket = new WebSocket("ws://127.0.0.1:100", ["chat"]);
socket.onopen = function() {
    console.log(socket.readyState);
}

这将发送以下内容到我的服务器:

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:100
Origin: http://localhost:57944
Sec-WebSocket-Protocol: chat
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: dBQ0epP7wmgHDEJc6Me95Q==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame

现在,我的服务器代码。

首先,我通读上面从客户端发送的行,找到Sec-WebSocket-Key值并将其保存到变量key

然后我在这里附加了维基百科文章中的幻数,即258EAFA5-E914-47DA-95CA-C5AB0DC85B11。这会产生以下代码:
key = string.Concat(key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
using (SHA1 sha1 = SHA1.Create())
{
    byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(key));
    responseKey = Convert.ToBase64String(hash);
}

最后,我的响应键用于构造我发送回客户机的消息,它是:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: u9guLPph1FeCPuOpFNs4F1b+UqY=
Sec-WebSocket-Protocol: chat
[blank line, as requested in spec]

然而,我的socket.readyState从0到3,然后失败,出现以下错误WebSocket connection to 'ws://127.0.0.1:100/' failed: .

我已经读到,这可能是我的服务器使用不兼容的协议到客户端,但我不完全确定在哪里开始寻找,如果是这种情况。如果没有,有没有人知道如何解决这个问题,或者我可以去哪里获得更多的信息,因为我有点难住了。

2013年3月21日

在阅读了@djc下面的评论后,我修改了我的服务器代码,以以下方式发送回确认:

using (StreamWriter sw = new StreamWriter(ns, Encoding.UTF8))
{
    sw.NewLine = "rnrn";
    // get response and calculate responseKey here (getting bytes as UTF-8 from client)
    sw.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
    sw.WriteLine("Upgrade: websocket");
    sw.WriteLine("Connection: Upgrade");
    sw.WriteLine("WebSocket-Origin: http://localhost:57944");
    sw.WriteLine("WebSocket-Location: 127.0.0.1:100");
    sw.WriteLine("Sec-WebSocket-Accept: " + responseKey);
    sw.WriteLine("");
    sw.Flush();
}

好吧,我不再得到一个错误在客户端-但'套接字。readyState'保持为0,我的socket.onopen块从未被触发-尽管我看到客户端连接尝试通过。

基于这个规范,我终于设法使它工作。下面是我的客户端代码:

var socket = new WebSocket("ws://localhost:8181/websession", ['chat','superchat']);
            
socket.onopen = function()
{
    $("div#messages").append("<p class="event">Socket is connected.</p>");
    socket.send("Thanks!");
};

似乎需要包含websession用于cookie管理和['chat','superchat']协议。

服务器:

using (TcpClient client = listener.AcceptTcpClient())
using (NetworkStream stream = client.GetStream())
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
{
    Dictionary<string, string> headers = new Dictionary<string, string>();
    string line = string.Empty;
    while ((line = sr.ReadLine()) != string.Empty)
    {
        string[] tokens = line.Split(new char[] { ':' }, 2);
        if (!string.IsNullOrWhiteSpace(line) && tokens.Length > 1)
        {
            headers[tokens[0]] = tokens[1].Trim();
        }
    }
    string responseKey = "";
    string key = string.Concat(headers["Sec-WebSocket-Key"], "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
    using (SHA1 sha1 = SHA1.Create())
    {
        byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(key));
        responseKey = Convert.ToBase64String(hash);
    }
    sw.WriteLine("HTTP/1.1 101 Switching Protocols");
    sw.WriteLine("Upgrade: websocket");
    sw.WriteLine("Connection: Upgrade");
    sw.WriteLine("Sec-WebSocket-Accept: " + responseKey);
    sw.WriteLine("Sec-WebSocket-Protocol: chat");
    sw.WriteLine("");
    sw.Flush();
}

现在我得到一个有效的连接,但不能发送/接收数据与服务器-但这是另一个故事,我猜。

相关内容

  • 没有找到相关文章

最新更新