コード例 #1
0
        public EventSource(Uri uri)
        {
            this.Uri = uri;
            this.ReconnectionTime = TimeSpan.FromMilliseconds(2000);

            this.ConnectionKey = new HostConnectionKey(this.Uri.Host, HostDefinition.GetKeyFor(this.Uri
#if !BESTHTTP_DISABLE_PROXY
                                                                                               , HTTPManager.Proxy
#endif
                                                                                               ));

#if !UNITY_WEBGL || UNITY_EDITOR
            this.InternalRequest = new HTTPRequest(Uri, HTTPMethods.Get, true, true, OnRequestFinished);

            // Set headers
            this.InternalRequest.SetHeader("Accept", "text/event-stream");
            this.InternalRequest.SetHeader("Cache-Control", "no-cache");
            this.InternalRequest.SetHeader("Accept-Encoding", "identity");

            this.InternalRequest.StreamChunksImmediately = true;
            this.InternalRequest.OnStreamingData         = OnData;

            // Disable internal retry
            this.InternalRequest.MaxRetries = 0;
#else
            if (!ES_IsSupported())
            {
                throw new NotSupportedException("This browser isn't support the EventSource protocol!");
            }

            this.Id = ES_Create(this.Uri.ToString(), WithCredentials, OnOpenCallback, OnMessageCallback, OnErrorCallback);

            EventSources.Add(this.Id, this);
#endif
        }