public async Task AddAsync(IMqttMessage item, CancellationToken cancellationToken = default)
        {
            if (_mqttConnection.ConnectionState != ConnectionState.Connected)
            {
                IProcesMqttMessage messageProcessor = null; // this is only for publising, we dont expect incoming messages
                await _mqttConnection.StartAsync(messageProcessor).ConfigureAwait(false);

                for (var i = 0; i < 100; i++)
                {
                    if (_mqttConnection.ConnectionState != ConnectionState.Connected)
                    {
                        await Task.Delay(50).ConfigureAwait(false);
                    }
                }
            }
            var qos = (MQTTnet.Protocol.MqttQualityOfServiceLevel)Enum.Parse(typeof(MQTTnet.Protocol.MqttQualityOfServiceLevel), item.QosLevel.ToString());
            var mqttApplicationMessage = new MqttApplicationMessage
            {
                Topic   = item.Topic,
                Payload = item.GetMessage(),
                QualityOfServiceLevel = qos,
                Retain = item.Retain
            };
            await _mqttConnection.PublishAsync(mqttApplicationMessage).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
 public async Task AddAsync(IMqttMessage item, CancellationToken cancellationToken = default(CancellationToken))
 {
     if (_mqttConnection.ConnectionState != ConnectionState.Connected)
     {
         await _mqttConnection.StartAsync().ConfigureAwait(false);
     }
     var qos = (MQTTnet.Protocol.MqttQualityOfServiceLevel)Enum.Parse(typeof(MQTTnet.Protocol.MqttQualityOfServiceLevel), item.QosLevel.ToString());
     var mqttApplicationMessage = new MqttApplicationMessage(item.Topic, item.GetMessage(), qos, item.Retain);
     await _mqttConnection.PublishAsync(mqttApplicationMessage).ConfigureAwait(false);
 }
        /// <summary>
        /// Sends a message into the queue.
        /// </summary>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        /// <param name="message">a message envelop to be sent.</param>
        public override async Task SendAsync(string correlationId, MessageEnvelope message)
        {
            CheckOpen(correlationId);

            _counters.IncrementOne("queue." + Name + ".sent_messages");
            _logger.Debug(message.CorrelationId, "Sent message {0} via %s", message, Name);

            var msg = FromMessage(message);

            var topic = !string.IsNullOrEmpty(Name) ? Name : _topic;

            await _connection.PublishAsync(topic, msg);
        }