Exemple #1
0
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp, int clientPort, string negotiatedSubprotocol)
        {
            var info = new WebSocketConnectionInfo
            {
                Origin                = request["Origin"] ?? request["Sec-WebSocket-Origin"],
                Host                  = request["Host"],
                SubProtocol           = request["Sec-WebSocket-Protocol"],
                Path                  = request.Path,
                ClientIpAddress       = clientIp,
                ClientPort            = clientPort,
                NegotiatedSubProtocol = negotiatedSubprotocol,
                Headers               = new Dictionary <string, string>(request.Headers, System.StringComparer.InvariantCultureIgnoreCase)
            };
            var cookieHeader = request["Cookie"];

            if (cookieHeader != null)
            {
                var match  = CookieRegex.Match(cookieHeader);
                var fields = match.Groups["cookie_name"].Captures;
                var values = match.Groups["cookie_value"].Captures;
                for (var i = 0; i < fields.Count; i++)
                {
                    var name  = fields[i].ToString();
                    var value = values[i].ToString();
                    info.Cookies[name] = value;
                }
            }

            return(info);
        }
        public void CreateHandler(IEnumerable <byte> data)
        {
            var request = _parseRequest(data.ToArray());

            if (request == null)
            {
                return;
            }
            Handler = _handlerFactory(request);
            if (Handler == null)
            {
                return;
            }
            var subProtocol = _negotiateSubProtocol(request.SubProtocols);

            ConnectionInfo = WebSocketConnectionInfo.Create(request, Socket.RemoteIpAddress, Socket.RemotePort, subProtocol);

            _initialize(this);

            var handshake = Handler.CreateHandshake(subProtocol);

            SendBytes(handshake, OnOpen);
        }