入门C#websocket应用程序

时间:2013-03-03 16:51:27

标签: c# websocket .net-4.5

我需要为基于javascript的crm应用程序启动websocket服务器。我研究这个例子:Create "Hello World" WebSocket example 但我无法建立联系。程序在

中引发异常
var key = headerResponse.Replace("ey:", "`")
                              .Split('`')[1]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                              .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                              .Trim();

从客户端发送帧如下:

  

GET / HTTP / 1.1   主持人:localhost:9801   连接:保持活力   缓存控制:max-age = 0   接受:text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8   User-Agent:Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.22   (KHTML,与Gecko一样)Chrome / 25.0.1364.97 Safari / 537.22   Accept-Encoding:gzip,deflate,sdch   Accept-Language:pl-PL,pl; q = 0.8,en-US; q = 0.6,en; q = 0.4   Accept-Charset:ISO-8859-2,utf-8; q = 0.7,*; q = 0.3

任何浏览器都缺少框架的关键部分。如何解决?

1 个答案:

答案 0 :(得分:0)

你做这件事的方式在例外方面似乎有些不自然。 尝试通过验证您收到的值来防止这种情况。 我无法说出问题的确切位置,但这应该可以解决您的问题

  

索引超出了数组的范围

    string key = "";
    if (string.IsNullOrEmpty(headerResponse)) 
    {
        //No header response... handle it ;)
    }
    var replacedString = headerResponse.Replace("ey:", "`");
    string[] splitted = replacedString.Split('`');
    if (splitted.Length > 1)
    {
        string replaced2 = splitted[1].Replace("\r", "");
        string[] splitted2 = replaced2.Split('\n');
        if (splitted2.Length > 0)
        {
            key = splitted2[0].Trim();
        }
        else 
        {
            // '\n' not found
        }
    }
    else 
    {
        // '`' not found
    }

    if (string.IsNullOrEmpty(key))
    {
        //do error correction
    }
    else 
    {
        //everything is fine
    }
相关问题