Example #1
0
        private static void SubscribePreviousConnection(Guid key, MqttClientAuthenticateResult result)
        {
            if (!result.IsSessionPresent)
            {
                return;
            }

            for (int i = 0; i < result.UserProperties.Count; i++)
            {
                MqttUserProperty prop = result.UserProperties[i];
                if (prop.Name == "subscriptions")
                {
                    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(prop.Value)))
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SubscriptionList));
                        SubscriptionList           list       = (SubscriptionList)serializer.ReadObject(ms);
                        foreach (PreviousSubscription subscription in list)
                        {
                            MqttClient client = GetClient(key);
                            string     gxproc = client.GetProc(subscription.topic);
                            if (!string.IsNullOrEmpty(gxproc))
                            {
                                Subscribe(key, subscription.topic, gxproc, subscription.qos);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Adds the user property to the unsubscribe options.
        /// Hint: MQTT 5 feature only.
        /// </summary>
        /// <param name="userProperty">The user property.</param>
        /// <returns>A new instance of the <see cref="MqttClientUnsubscribeOptionsBuilder"/> class.</returns>
        public MqttClientUnsubscribeOptionsBuilder WithUserProperty(MqttUserProperty userProperty)
        {
            if (userProperty is null)
            {
                throw new ArgumentNullException(nameof(userProperty));
            }

            if (_unsubscribeOptions.UserProperties is null)
            {
                _unsubscribeOptions.UserProperties = new List <MqttUserProperty>();
            }

            _unsubscribeOptions.UserProperties.Add(userProperty);

            return(this);
        }