Beispiel #1
0
    private string ValidateResponse(HttpWebRequest request, HttpWebResponse response)
    {
        if (response.StatusCode != HttpStatusCode.SwitchingProtocols)
        {
            throw new WebSocketException("net_WebSockets_Connect101Expected");
        }
        string text = response.Headers["Upgrade"];

        if (!string.Equals(text, "websocket", StringComparison.OrdinalIgnoreCase))
        {
            throw new WebSocketException("net_WebSockets_InvalidResponseHeader");
        }
        string text2 = response.Headers["Connection"];

        if (!string.Equals(text2, "Upgrade", StringComparison.OrdinalIgnoreCase))
        {
            throw new WebSocketException("net_WebSockets_InvalidResponseHeader");
        }
        string text3 = response.Headers["Sec-WebSocket-Accept"];
        string secWebSocketAcceptString = ClientWebSocketOptions2.GetSecWebSocketAcceptString(request.Headers["Sec-WebSocket-Key"]);

        if (!string.Equals(text3, secWebSocketAcceptString, StringComparison.OrdinalIgnoreCase))
        {
            throw new WebSocketException("net_WebSockets_InvalidResponseHeader");
        }
        string text4 = response.Headers["Sec-WebSocket-Protocol"];

        if (!string.IsNullOrWhiteSpace(text4) && this.options.RequestedSubProtocols.Count > 0)
        {
            bool flag = false;
            foreach (string current in this.options.RequestedSubProtocols)
            {
                if (string.Equals(current, text4, StringComparison.OrdinalIgnoreCase))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                throw new WebSocketException("net_WebSockets_AcceptUnsupportedProtocol");
            }
        }
        if (!string.IsNullOrWhiteSpace(text4))
        {
            return(text4);
        }
        return(null);
    }
Beispiel #2
0
 public CustomWebsocket()
 {
     this.state   = 0;
     this.options = new ClientWebSocketOptions2();
     this.cts     = new CancellationTokenSource();
 }