Esempio n. 1
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;

            mqttBridge.OnSubscribe(client, e);

            for (int i = 0; i < e.Topics.Length; i++)
            {
                // TODO : business logic to grant QoS levels based on some conditions ?
                //        now the broker granted the QoS levels requested by client

                // subscribe client for each topic and QoS level requested
                this.subscriberManager.Subscribe(e.Topics[i], e.QoSLevels[i], client);
            }

            try
            {
                // send SUBACK message to the client
                client.Suback(e.MessageId, e.QoSLevels);

                for (int i = 0; i < e.Topics.Length; i++)
                {
                    // publish retained message on the current subscription
                    this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                }
            }
            catch (MqttCommunicationException)
            {
                this.CloseClient(client);
            }
        }
Esempio n. 2
0
 public virtual void OnSubscribe(MqttClient client, MqttMsgSubscribeEventArgs message)
 {
     Composable.GetExport<IXLogger>().Verbose("Mqtt Subscribe {@m}", message);
     return;
 }
Esempio n. 3
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;

            // Since a client may subscribe to multiple topics, we must check each one
            var topicAuth = new Dictionary<string, bool>();

            for (int i = 0; i < e.Topics.Length; i++)
            {
                // Authenticate this client & topic.
                var verified = this.uacManager.PubSubAuthentication(false, client.ClientId, e.Topics[i]);
                topicAuth[e.Topics[i]] = verified;
                if (verified)
                {
                    // TODO : business logic to grant QoS levels based on some conditions ?
                    //        now the broker granted the QoS levels requested by client

                    // subscribe client for each topic and QoS level requested
                    this.subscriberManager.Subscribe(e.Topics[i], e.QoSLevels[i], client);
                }
            }

            bool closeClient = false;
            try
            {
                // If any have been allowed, send the suback
                if (topicAuth.Any(x => x.Value))
                {
                    // send SUBACK message to the client
                    client.Suback(e.MessageId, e.QoSLevels);

                    for (int i = 0; i < e.Topics.Length; i++)
                    {
                        // Send any retained message to the subscriber if verified
                        var verified = topicAuth[e.Topics[i]];
                        if (verified)
                        {
                            // publish retained message on the current subscription
                            this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                        }
                    }
                }
                else
                {
                    closeClient = true;
                }
            }
            catch (MqttCommunicationException)
            {
                closeClient = true;
            }

            if (closeClient)
            {
                this.CloseClient(client);
            }
        }