Пример #1
0
 /// <summary>
 ///     topic : /xxx/yyy
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="payload"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public Task Pub(string topic, string payload, CancellationToken token)
 {
     return(_client.PublishAsync(CreateMsg(topic.ToLower(), payload), token));
 }
Пример #2
0
        /// <summary>
        /// Publish the stream of <see cref="ManagedMqttApplicationMessage"/>s to the server and give a response of the publish result.
        /// </summary>
        /// <param name="observable">The source observable.</param>
        /// <param name="rxMqttClinet">The client to publish the messages with.</param>
        /// <returns>A observer for the publish results.</returns>
        public static IObservable <RxMqttClientPublishResult> PublishOn(this IObservable <ManagedMqttApplicationMessage> observable, IRxMqttClient rxMqttClinet)
        {
            if (observable is null)
            {
                throw new ArgumentNullException(nameof(observable));
            }
            if (rxMqttClinet is null)
            {
                throw new ArgumentNullException(nameof(rxMqttClinet));
            }

            return(observable
                   .Select(managedMqttApplicationMessage =>
            {
                return Observable.Create <RxMqttClientPublishResult>(async observer =>
                {
                    if (!rxMqttClinet.IsConnected)
                    {
                        observer.OnNext(new RxMqttClientPublishResult {
                            ReasonCode = RxMqttClientPublishReasonCode.ClientNotConnected, MqttApplicationMessage = managedMqttApplicationMessage
                        });
                        observer.OnCompleted();
                        return Disposable.Empty;
                    }
                    else
                    {
                        var subscription = rxMqttClinet.ApplicationMessageProcessedEvent
                                           .Where(@event => @event.ApplicationMessage.Id == managedMqttApplicationMessage.Id)
                                           .Select(@event => @event.HasSucceeded
                                    ? new RxMqttClientPublishResult {
                            ReasonCode = RxMqttClientPublishReasonCode.HasSucceeded, MqttApplicationMessage = @event.ApplicationMessage
                        }
                                    : new RxMqttClientPublishResult {
                            ReasonCode = RxMqttClientPublishReasonCode.HasFailed, MqttApplicationMessage = @event.ApplicationMessage, Exception = @event.Exception
                        })
                                           .Merge(rxMqttClinet.ApplicationMessageSkippedEvent
                                                  .Where(@event => @event.ApplicationMessage.Id == managedMqttApplicationMessage.Id)
                                                  .Select(@event => new RxMqttClientPublishResult {
                            ReasonCode = RxMqttClientPublishReasonCode.HasSkipped, MqttApplicationMessage = @event.ApplicationMessage
                        }))
                                           .Take(1)
                                           .SubscribeSafe(observer);

                        try
                        {
                            await rxMqttClinet.PublishAsync(managedMqttApplicationMessage).ConfigureAwait(false);
                        }
                        catch (Exception exception)
                        {
                            observer.OnNext(new RxMqttClientPublishResult {
                                ReasonCode = RxMqttClientPublishReasonCode.HasFailed, MqttApplicationMessage = managedMqttApplicationMessage, Exception = exception
                            });
                            observer.OnCompleted();
                        }

                        return subscription;
                    }
                });
            })
                   .Merge());
        }