示例#1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="clientId">Client identifier</param>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <param name="willRetain">Will retain flag</param>
        /// <param name="willQosLevel">Will QOS level</param>
        /// <param name="willFlag">Will flag</param>
        /// <param name="willTopic">Will topic</param>
        /// <param name="willMessage">Will message</param>
        /// <param name="cleanSession">Clean sessione flag</param>
        /// <param name="keepAlivePeriod">Keep alive period</param>
        /// <param name="protocolVersion">MQTT Protocol version</param>
        public MqttMsgConnect(string clientId,
                              string username,
                              string password,
                              bool willRetain,
                              MqttQoSLevel willQosLevel,
                              bool willFlag,
                              string willTopic,
                              string willMessage,
                              bool cleanSession,
                              ushort keepAlivePeriod,
                              MqttProtocolVersion protocolVersion
                              )
        {
            Type = MqttMessageType.Connect;

            ClientId        = clientId;
            Username        = username;
            Password        = password;
            WillRetain      = willRetain;
            WillQosLevel    = (byte)willQosLevel;
            WillFlag        = willFlag;
            WillTopic       = willTopic;
            WillMessage     = willMessage;
            CleanSession    = cleanSession;
            KeepAlivePeriod = keepAlivePeriod;
            // [v.3.1.1] added new protocol name and version valid for 5.0
            ProtocolVersion = protocolVersion;
            ProtocolName    = (ProtocolVersion == MqttProtocolVersion.Version_3_1_1 || ProtocolVersion == MqttProtocolVersion.Version_5) ? PROTOCOL_NAME_V3_1_1 : PROTOCOL_NAME_V3_1;
        }
        public bool Connect(string Host, string AccessToken, int Port = 1883)
        {
            if (Host == null || AccessToken == null)
            {
                return(false);
            }

            string clientId = Guid.NewGuid().ToString();;

            client = new MqttClient(Host, Port, false, null, null, MqttSslProtocols.None);

            client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
            client.MqttMsgSubscribed      += Client_MqttMsgSubscribed;
            client.ConnectionClosed       += Client_ConnectionClosed;

            var result = client.Connect(clientId, AccessToken, null);

            if (result != 0)
            {
                return(false);
            }

            Connected = true;

            this.QoS = MqttQoSLevel.AtLeastOnce;

            client.Subscribe(new string[] { ATTRIBUTES_TOPIC }, new MqttQoSLevel[] { QoS });
            client.Subscribe(new string[] { ATTRIBUTES_TOPIC + "/response/+" }, new MqttQoSLevel[] { QoS });
            client.Subscribe(new string[] { RPC_REQUEST_TOPIC + "+" }, new MqttQoSLevel[] { QoS });
            client.Subscribe(new string[] { RPC_RESPONSE_TOPIC + "+" }, new MqttQoSLevel[] { QoS });

            return(true);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="topic">Message topic</param>
 /// <param name="message">Message data</param>
 /// <param name="dupFlag">Duplicate delivery flag</param>
 /// <param name="qosLevel">Quality of Service level</param>
 /// <param name="retain">Retain flag</param>
 public MqttMsgPublishEventArgs(string topic,
                                byte[] message,
                                bool dupFlag,
                                MqttQoSLevel qosLevel,
                                bool retain)
 {
     Topic    = topic;
     Message  = message;
     DupFlag  = dupFlag;
     QosLevel = qosLevel;
     Retain   = retain;
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="topic">Message topic</param>
        /// <param name="message">Message data</param>
        /// <param name="dupFlag">Duplicate flag</param>
        /// <param name="qosLevel">Quality of Service level</param>
        /// <param name="retain">Retain flag</param>
        public MqttMsgPublish(string topic,
                              byte[] message,
                              bool dupFlag,
                              MqttQoSLevel qosLevel,
                              bool retain) : base()
        {
            Type = MqttMessageType.Publish;

            Topic     = topic;
            Message   = message;
            DupFlag   = dupFlag;
            QosLevel  = qosLevel;
            Retain    = retain;
            MessageId = 0;
        }
 public static ushort Publish(this MqttClient mqttClient, string topic, byte[] message, MqttQoSLevel qosLevel, bool retain)
 {
     return(mqttClient.Publish(topic, message, (byte)qosLevel, retain));
 }