Example #1
0
        /// <summary>
        /// Long running task to listen for events
        /// </summary>
        /// <param name="path">Relative path to Particle Cloud event endpoint</param>
        /// <param name="particleEventGroup">ParticleEventGroup to send new events to</param>
        /// <returns>Returns Task of long running event task</returns>
        protected override async Task ListenForEventAsync(string path, ParticleEventGroup particleEventGroup)
        {
            var    httpListener = new HttpListener();
            string eventName    = "";

            try
            {
                var url     = new Uri($"https://api.particle.io/{path}/?access_token={AccessToken}");
                var request = WebRequest.Create(url);
                request.Method = "GET";

                var task = Task.Factory.StartNew(() => httpListener.Start());

                using (var response = await request.GetResponseAsync())
                {
                    using (var stream = response.GetResponseStream())
                        using (var reader = new StreamReader(stream))
                        {
                            while (!reader.EndOfStream && particleEventGroup.HasHandlers)
                            {
                                var outputString = reader.ReadLine();

                                if (outputString.StartsWith("event:"))
                                {
                                    eventName = outputString.Substring(6).Trim();
                                }
                                else if (outputString.StartsWith("data:") && !string.IsNullOrWhiteSpace(eventName))
                                {
                                    var jsonSerializerSettings = new JsonSerializerSettings()
                                    {
                                        DateTimeZoneHandling = DateTimeZoneHandling.Local
                                    };
                                    var particleEventResponse = JsonConvert.DeserializeObject <ParticleEventResponse>(outputString.Substring(5), jsonSerializerSettings);
                                    particleEventResponse.Name = eventName;
                                    eventName = "";

                                    SynchronizationContextPost(a =>
                                    {
                                        particleEventGroup.NewMessage(this, particleEventResponse);
                                    }, null);
                                }
                            }
                        }
                }
            }
            catch
            {
            }

            httpListener.Stop();
        }
Example #2
0
        /// <summary>
        /// Long running task to listen for events
        /// </summary>
        /// <param name="path">Relative path to Particle Cloud event endpoint</param>
        /// <param name="particleEventGroup">ParticleEventGroup to send new events to</param>
        /// <returns>Returns Task of long running event task</returns>
        protected override async Task ListenForEventAsync(string path, ParticleEventGroup particleEventGroup)
        {
            string eventName = "";

            try
            {
                using (var client = new HttpClient())
                {
                    var url     = new Uri($"https://api.particle.io/{path}/?access_token={AccessToken}");
                    var request = new HttpRequestMessage(HttpMethod.Get, url);

                    using (var response = await client.SendRequestAsync(request, HttpCompletionOption.ResponseHeadersRead))
                    {
                        using (var stream = await response.Content.ReadAsInputStreamAsync())
                            using (var reader = new StreamReader(stream.AsStreamForRead()))
                            {
                                while (!reader.EndOfStream && particleEventGroup.HasHandlers)
                                {
                                    var outputString = reader.ReadLine();

                                    if (outputString.StartsWith("event:"))
                                    {
                                        eventName = outputString.Substring(6).Trim();
                                    }
                                    else if (outputString.StartsWith("data:") && !string.IsNullOrWhiteSpace(eventName))
                                    {
                                        var jsonSerializerSettings = new JsonSerializerSettings()
                                        {
                                            DateTimeZoneHandling = DateTimeZoneHandling.Local
                                        };
                                        var particleEventResponse = JsonConvert.DeserializeObject <ParticleEventResponse>(outputString.Substring(5), jsonSerializerSettings);
                                        particleEventResponse.Name = eventName;
                                        eventName = "";

                                        SynchronizationContextPost(a =>
                                        {
                                            particleEventGroup.NewMessage(this, particleEventResponse);
                                        }, null);
                                    }
                                }
                            }
                    }
                }
            }
            catch
            {
            }
        }