/// <summary> /// Subscribes to information from a set of topics. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param> /// <returns>Packet identifier assigned to subscription.</returns> public async Task <ushort> SUBSCRIBE(params KeyValuePair <string, MqttQualityOfService>[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (KeyValuePair <string, MqttQualityOfService> Pair in Topics) { Payload.WriteString(Pair.Key); Payload.WriteByte((byte)Pair.Value); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
private async Task CONNECT(int KeepAliveSeconds) { this.State = MqttState.Authenticating; this.keepAliveSeconds = KeepAliveSeconds; this.nextPing = DateTime.Now.AddMilliseconds(KeepAliveSeconds * 500); this.secondTimer = new Timer(this.SecondTimer_Elapsed, null, 1000, 1000); BinaryOutput Payload = new BinaryOutput(); Payload.WriteString("MQTT"); Payload.WriteByte(4); // v3.1.1 byte b = 2; // Clean session. if (this.will) { b |= 4; b |= (byte)(((int)this.willQoS) << 3); if (this.willRetain) { b |= 32; } } if (!string.IsNullOrEmpty(this.userName)) { b |= 128; if (!string.IsNullOrEmpty(this.password)) { b |= 64; } } Payload.WriteByte(b); Payload.WriteByte((byte)(KeepAliveSeconds >> 8)); Payload.WriteByte((byte)KeepAliveSeconds); Payload.WriteString(this.clientId); if (this.will) { Payload.WriteString(this.willTopic); int l = this.willData.Length; Payload.WriteByte((byte)(l >> 8)); Payload.WriteByte((byte)l); Payload.WriteBytes(this.willData); } if (!string.IsNullOrEmpty(this.userName)) { Payload.WriteString(this.userName); if (!string.IsNullOrEmpty(this.password)) { Payload.WriteString(this.password); } } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.CONNECT << 4); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, 0, null); this.inputState = 0; }
/// <summary> /// Subscribes to information from a set of topics. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param> /// <returns>Packet identifier assigned to subscription.</returns> public async Task <ushort> SUBSCRIBE(params KeyValuePair <string, MqttQualityOfService>[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (KeyValuePair <string, MqttQualityOfService> Pair in Topics) { Payload.WriteString(Pair.Key); Payload.WriteByte((byte)Pair.Value); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); if (this.HasSniffers) { StringBuilder sb = new StringBuilder(); bool First = true; sb.Append("SUBSCRIBE("); foreach (KeyValuePair <string, MqttQualityOfService> P in Topics) { if (First) { First = false; } else { sb.Append(", "); } sb.Append(P.Value.ToString()); sb.Append(':'); sb.Append(P.Key); } sb.Append(')'); this.Information(sb.ToString()); } await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }