Beispiel #1
0
        private async Task ListenToServerEvents()
        {
            UrlBuilder.AppendToPath(Endpoints.Json);

            while (!CancellationToken.IsCancellationRequested)
            {
                var urlBuilderCopy = UrlBuilder.Copy();
                urlBuilderCopy.AddParam(Params.Auth, await IdTokenFactory());

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(urlBuilderCopy.Url));
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));

                HttpResponseMessage response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

                using (Stream stream = await response.Content.ReadAsStreamAsync())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        while (!CancellationToken.IsCancellationRequested)
                        {
                            var serializedEventType = await reader.ReadLineAsync();

                            if (String.IsNullOrWhiteSpace(serializedEventType))
                            {
                                continue;
                            }

                            string serializedData = await reader.ReadLineAsync();

                            ServerEvent serverEvent = ServerEvent.Parse(serializedEventType, serializedData);
                            if (serverEvent.Type == ServerEventType.AuthRevoked)
                            {
                                break;
                            }
                            if (serverEvent.Type == ServerEventType.KeepAlive)
                            {
                                continue;
                            }
                            if (serverEvent.Type == ServerEventType.Cancel)
                            {
                                throw new PremissionDeniedException();
                            }

                            if (serverEvent.Type == ServerEventType.Put)
                            {
                                HandlePut(serverEvent);
                            }
                            else if (serverEvent.Type == ServerEventType.Patch)
                            {
                                HandlePatch(serverEvent);
                            }
                        }
                    }
            }

            // We clear all the events
            ValueChanged = ChildAdded = ChildChanged = ChildRemoved = null;
        }
Beispiel #2
0
        /// <summary>
        /// Reads data stored at the specified reference.
        /// </summary>
        /// <typeparam name="T">Type of the data.</typeparam>
        /// <returns>The deserialized data.</returns>
        public async virtual Task <T> Once <T>()
        {
            UrlBuilder.AppendToPath(Endpoints.Json).AddParam(Params.Auth, await IdTokenFactory());
            var response = await Client.GetAsync(UrlBuilder.Url);

            try
            {
                return(await response.Content.ReadAsAsync <T>());
            }
            catch (JsonSerializationException) { } // Means the content is null

            return(default(T));
        }
Beispiel #3
0
 public ChildQuery Child(string name)
 {
     return(new ChildQuery(UrlBuilder.AppendToPath(name), name, IdTokenFactory));
 }
Beispiel #4
0
 public async Task Update <T>(T value)
 {
     UrlBuilder.AppendToPath(Endpoints.Json).AddParam(Params.Auth, await IdTokenFactory());
     var response = await Client.PatchAsJsonAsync(UrlBuilder.Url, value);
 }
Beispiel #5
0
 /// <summary>
 /// Deletes the data at the specified reference.
 /// </summary>
 /// <returns></returns>
 public async Task Remove()
 {
     UrlBuilder.AppendToPath(Endpoints.Json).AddParam(Params.Auth, await IdTokenFactory());
     var response = await Client.DeleteAsync(UrlBuilder.Url);
 }