Ejemplo n.º 1
0
        private static async Task SendCloudToDeviceMessagesAsync(CloudToDeviceSenderOptions options, CancellationToken cts)
        {
            Logger.Log($"Connecting to {options.DeviceId}");
            var serviceClient = ServiceClient.CreateFromConnectionString(options.ConnectionString);

            Logger.Log("connected");

            var msgIdentifier = options.MessageSeed;

            while (!cts.IsCancellationRequested)
            {
                var msgIdentifierText = msgIdentifier.ToString(CultureInfo.InvariantCulture);
                var msg = new Message(Encoding.UTF8.GetBytes(msgIdentifierText))
                {
                    MessageId = msgIdentifierText,
                };

                await serviceClient.SendAsync(options.DeviceId, msg);

                Logger.Log($"Sent message {msgIdentifierText} to device {options.DeviceId}");

                await Task.Delay(TimeSpan.FromSeconds(Math.Max(1, options.TimeBetweenMessages)));

                ++msgIdentifier;
            }
        }
Ejemplo n.º 2
0
 static void Init(CloudToDeviceSenderOptions options, CancellationToken cts)
 {
     Task.Run(() => SendCloudToDeviceMessagesAsync(options, cts));
 }