public async Task <ConnectAckCode> ConnectAsync(string clientId, string username, string password, int keepaliveSeconds) { code = null; ConnectMessage msg = new ConnectMessage(clientId, username, password, keepaliveSeconds, true); if (!Channel.IsConnected) { try { await Channel.OpenAsync(); } catch (Exception ex) { OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex)); return(ConnectAckCode.ServerUnavailable); } try { Receive(Channel); } catch (Exception ex) { OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex)); return(ConnectAckCode.ServerUnavailable); } } try { await Channel.SendAsync(msg.Encode()); DateTime expiry = DateTime.UtcNow.AddMilliseconds(timeoutMilliseconds); while (!code.HasValue) { await Task.Delay(10); if (DateTime.UtcNow > expiry) { throw new TimeoutException("MQTT connection timed out."); } } return(code.Value); } catch (Exception ex) { OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex)); return(ConnectAckCode.ServerUnavailable); } }
private void Channel_OnClose(object sender, ChannelCloseEventArgs args) { try { code = null; this.channel.Dispose(); } catch (Exception ex) { Trace.TraceWarning("Piraeus MQTT client fault disposing channel."); Trace.TraceError(ex.Message); } }
public async Task ConnectAsync(string clientId, string username, string password, int keepaliveSeconds, bool cleanSession) { code = null; ConnectMessage msg = new ConnectMessage(clientId, username, password, keepaliveSeconds, cleanSession); if (!channel.IsConnected) { await channel.OpenAsync(); Task task = channel.ReceiveAsync(); await Task.WhenAll(task); } await channel.SendAsync(msg.Encode()); }
private void Session_OnConnect(object sender, MqttConnectionArgs args) { code = args.Code; }
private void Channel_OnClose(object sender, ChannelCloseEventArgs args) { MqttConnectCode = null; }