示例#1
0
        internal void FallbackToHTTP1()
        {
            if (this.implementation == null)
            {
                return;
            }

            this.implementation = new OverHTTP1(this, this.implementation.Uri, this.implementation.Origin, this.implementation.Protocol);
            this.implementation.StartOpen();
        }
示例#2
0
        /// <summary>
        /// Creates a WebSocket instance from the given uri, protocol and origin.
        /// </summary>
        /// <param name="uri">The uri of the WebSocket server</param>
        /// <param name="origin">Servers that are not intended to process input from any web page but only for certain sites SHOULD verify the |Origin| field is an origin they expect.
        /// If the origin indicated is unacceptable to the server, then it SHOULD respond to the WebSocket handshake with a reply containing HTTP 403 Forbidden status code.</param>
        /// <param name="protocol">The application-level protocol that the client want to use(eg. "chat", "leaderboard", etc.). Can be null or empty string if not used.</param>
        /// <param name="extensions">Optional IExtensions implementations</param>
        public WebSocket(Uri uri, string origin, string protocol
#if !UNITY_WEBGL || UNITY_EDITOR
                         , params IExtension[] extensions
#endif
                         )

        {
            this.Context = new LoggingContext(this);

#if !UNITY_WEBGL || UNITY_EDITOR
            this.Extensions = extensions;

#if !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
            if (HTTPManager.HTTP2Settings.WebSocketOverHTTP2Settings.EnableWebSocketOverHTTP2 && HTTPProtocolFactory.IsSecureProtocol(uri))
            {
                // Try to find a HTTP/2 connection that supports the connect protocol.
                var con = BestHTTP.Core.HostManager.GetHost(uri.Host).GetHostDefinition(Core.HostDefinition.GetKeyFor(new UriBuilder("https", uri.Host, uri.Port).Uri
#if !BESTHTTP_DISABLE_PROXY
                                                                                                                      , GetProxy(uri)
#endif
                                                                                                                      )).Find(c => {
                    var httpConnection = c as HTTPConnection;
                    var http2Handler   = httpConnection?.requestHandler as Connections.HTTP2.HTTP2Handler;

                    return(http2Handler != null && http2Handler.settings.RemoteSettings[Connections.HTTP2.HTTP2Settings.ENABLE_CONNECT_PROTOCOL] != 0);
                });

                if (con != null)
                {
                    HTTPManager.Logger.Information("WebSocket", "Connection with enabled Connect Protocol found!", this.Context);

                    var httpConnection = con as HTTPConnection;
                    var http2Handler   = httpConnection?.requestHandler as Connections.HTTP2.HTTP2Handler;

                    this.implementation = new OverHTTP2(this, http2Handler, uri, origin, protocol);
                }
            }
#endif

            if (this.implementation == null)
            {
                this.implementation = new OverHTTP1(this, uri, origin, protocol);
            }
#else
            this.implementation = new WebGLBrowser(this, uri, origin, protocol);
#endif

            // Under WebGL when only the WebSocket protocol is used Setup() isn't called, so we have to call it here.
            HTTPManager.Setup();
        }