コード例 #1
0
        public EngineIOWebSocket(EngineIOClientOption Option) : base(Option)
        {
            StringBuilder URL = new StringBuilder();

            URL.Append(string.Format("{0}://{1}:{2}{3}", (EngineIOScheme)(Option.Scheme + 2), Option.Host, Option.Port, Option.Path)).Append('?');

            foreach (string Key in new List <string>(Option.Query.Keys))
            {
                URL.Append(Key).Append('=').Append(Option.Query[Key]).Append('&');
            }

            URL.Append("transport=websocket");

            if (Option.TimestampRequests ?? false)
            {
                URL.Append(string.Format("&{0}={1}", Option.TimestampParam, EngineIOTimestamp.Generate()));
            }

            WebSocket = new WebSocket(URL.ToString(), Option.WebSocketSubprotocols)
            {
                Compression   = CompressionMethod.Deflate,
                CustomHeaders = Option.ExtraHeaders,
            };

            if (Option.WithCredentials && WebSocket.IsSecure)
            {
                WebSocket.SslConfiguration.ServerCertificateValidationCallback = Option.ServerCertificateValidationCallback;
                WebSocket.SslConfiguration.ClientCertificateSelectionCallback  = Option.ClientCertificateSelectionCallback;

                if (Option.ClientCertificates != null)
                {
                    WebSocket.SslConfiguration.ClientCertificates = Option.ClientCertificates;
                }
            }

            WebSocket.OnOpen    += OnWebSocketOpen;
            WebSocket.OnClose   += OnWebSocketClose;
            WebSocket.OnMessage += OnWebSocketMessage;
            WebSocket.OnError   += OnWebSocketError;
            WebSocket.Log.Output = EngineIOLogger.WebSocket;
        }
コード例 #2
0
        private HttpWebRequest CreateRequest(EngineIOHttpMethod Method)
        {
            StringBuilder URL = new StringBuilder();

            URL.Append(string.Format("{0}://{1}:{2}{3}", Option.Scheme, Option.Host, Option.Port, Option.Path)).Append('?');

            foreach (string Key in new List <string>(Option.Query.Keys))
            {
                URL.Append(Key).Append('=').Append(Option.Query[Key]).Append('&');
            }

            if (Option.ForceBase64)
            {
                URL.Append("b64=1&");
            }

            URL.Append("transport=polling");

            if (Option.TimestampRequests ?? true)
            {
                URL.Append(string.Format("&{0}={1}", Option.TimestampParam, EngineIOTimestamp.Generate()));
            }

            HttpWebRequest Request = WebRequest.Create(URL.ToString()) as HttpWebRequest;

            Request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            Request.Timeout = Option.PollingTimeout == 0 ? Timeout.Infinite : Option.PollingTimeout;
            Request.ServicePoint.Expect100Continue = false;
            Request.Method          = Method.ToString();
            Request.CookieContainer = Cookies;
            Request.KeepAlive       = false;

            if (Option.WithCredentials)
            {
#if !NET40
                Request.ServerCertificateValidationCallback = Option.ServerCertificateValidationCallback;
#endif

                if (Option.ClientCertificates != null)
                {
                    Request.ClientCertificates = Option.ClientCertificates;
                }
            }

            if (Option.ExtraHeaders.Count > 0)
            {
                foreach (string Key in new List <string>(Option.ExtraHeaders.Keys))
                {
                    try
                    {
                        bool IsAutorization = Key.ToLower().Trim().Equals("authorization");

                        if (!IsAutorization || (IsAutorization && Option.WithCredentials))
                        {
                            Request.Headers.Add(Key, Option.ExtraHeaders[Key]);
                        }
                    }
                    catch
                    {
                    }
                }
            }

            return(Request);
        }