private void AmqpService_OnStatusChange(IBusService.ConnectionStatus obj, IBusService.ConnectionInfo info) { Info = info.BrokerUri.AbsoluteUri; if (obj.Equals(IBusService.ConnectionStatus.Error)) { ToastService.ShowToast($"Erro: {GetError().Message}", Microsoft.AspNetCore.Components.Web.ToastLevel.Error); } OnAmqpStatusChange?.Invoke(obj); }
public async Task ConnectionStartAsync(Uri uri) { await Task.Run(() => { try { _info = new IBusService.ConnectionInfo(uri, MqttConfiguration.TopicMatcher(uri)); _error = null; _client = new MqttClient(uri.Host, uri.Port, false, null, null, MqttSslProtocols.None) { ProtocolVersion = MqttProtocolVersion.Version_3_1 }; var code = default(byte); if (string.IsNullOrEmpty(uri.UserInfo)) { code = _client.Connect(Guid.NewGuid().ToString()); } else { var bUri = new UriBuilder(uri); code = _client.Connect(Guid.NewGuid().ToString(), bUri.UserName, bUri.Password); } _client.Subscribe(new[] { _info.Topic }, new[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); _client.MqttMsgPublishReceived += DefaultClient_MqttMsgPublishReceived; OnConnection?.Invoke(code); } catch (Exception e) { _error = e; } finally { OnStatusChange?.Invoke(GetConnectionStatus(), _info); } }); }