public void ProcessEvents()
        {
            var processor = new EventStreamProcessor(_stream, _cancellationTokenSource);

            while (_stream.CanRead && !_cancellationTokenSource.IsCancellationRequested)
            {
                var eventData = processor.GetEvent();

                if (eventData == null)
                {
                    continue;
                }

                OnEventPublished(new EventPublishedArgs {EventData = eventData});
            }
        }
Example #2
0
#pragma warning disable 4014
        public async Task SubscribeEventsAsync(string deviceId)
        {
            if (string.IsNullOrEmpty(deviceId))
            {
                throw new ArgumentNullException("deviceId");
            }

            using (var client = _httpClientHelper.GetAuthorizedClient())
            {
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                var stream = await client.GetStreamAsync(string.Format("/v1/devices/{0}/events", deviceId));

                _cancellationTokenSource = new CancellationTokenSource();

                var processor = new EventStreamProcessor(stream, _cancellationTokenSource);

                Task.Factory.StartNew(() => { processor.ProcessEvents(); },
                    _cancellationTokenSource.Token).ConfigureAwait(false);
            }
        }
#pragma warning disable 4014
        public async Task SubscribeAllDevicesEventsAsync()
        {
            using (var client = _httpClientHelper.GetAuthorizedClient())
            {
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                var stream = await client.GetStreamAsync("/v1/devices/events");

                _cancellationTokenSource = new CancellationTokenSource();

                var processor = new EventStreamProcessor(stream, _cancellationTokenSource);

                Task.Factory.StartNew(() => { processor.ProcessEvents(); },
                    _cancellationTokenSource.Token).ConfigureAwait(false);
            }
        }