Пример #1
0
        public async Task StopAsync()
        {
            try
            {
                _cancellationTokenSource?.Cancel(false);
                _cancellationTokenSource?.Dispose();
                _cancellationTokenSource = null;

                if (_adapter != null)
                {
                    await _adapter.DisconnectAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false);

                    _adapter = null;
                }

                _logger.LogInformation("Client '{0}': Session stopped.", ClientId);
            }
            finally
            {
                var willMessage = _willMessage;
                if (willMessage != null)
                {
                    _willMessage = null; //clear willmessage so it is send just once
                    await _sessionsManager.DispatchApplicationMessageAsync(this, willMessage);
                }
            }
        }
Пример #2
0
        public async Task PublishAsync(IEnumerable <MqttApplicationMessage> applicationMessages)
        {
            if (applicationMessages == null)
            {
                throw new ArgumentNullException(nameof(applicationMessages));
            }

            if (_cancellationTokenSource == null)
            {
                throw new InvalidOperationException("The server is not started.");
            }

            foreach (var applicationMessage in applicationMessages)
            {
                await _clientSessionsManager.DispatchApplicationMessageAsync(null, applicationMessage);
            }
        }