Example #1
0
        /// <summary>
        /// Removes a client from the memebership list.
        /// </summary>
        /// <param name="client">The client to remove.</param>
        /// <returns>The current membership.</returns>
        public void Unsubscribe(IMqttSender client, SubscriptionInterest interest)
        {
            lock (this)
            {
                // Remove from the membership
                if (interest.HasFlag(SubscriptionInterest.Messages) && ArrayUtils.Remove(ref this.MessageSubscribers, client) >= 0)
                {
                    // Remove the client from the presence
                    if (client.Id != ConnectionId.Empty)
                    {
                        Console.WriteLine("Removing from presence: " + client.Id);
                        this.Presence.Remove(client.Id.ToString());
                    }

                    if (this.Listeners == 0)
                    {
                        // Broadcast unsubscribe if there's no more clients left
                        this.BroadcastUnsubscribe(client);
                    }
                }

                if (interest.HasFlag(SubscriptionInterest.Presence))
                {
                    // Unregister the client presence as well
                    ArrayUtils.Remove(ref this.PresenceSubscribers, client);
                }

                // Dispose the subscription if empty
                if (this.Empty)
                {
                    this.Dispose();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Subscribes a client by adding it to an appropriate membership list.
        /// </summary>
        /// <param name="client">The client to subscribe.</param>
        /// <param name="interest">The interests to register for.</param>
        /// <returns></returns>
        public void Subscribe(IMqttSender client, SubscriptionInterest interest)
        {
            lock (this)
            {
                if (interest.HasFlag(SubscriptionInterest.Messages))
                {
                    // Add to the membership list
                    ArrayUtils.AddUnique(ref this.MessageSubscribers, client);

                    // Add to the presence. Since the presence is inside the registry, it will be automatically
                    // synchronized and eventually consistent across the cluster.
                    if (client.Id != ConnectionId.Empty)
                    {
                        this.Presence.Add(client.Id.ToString(), new SubscriptionPresenceInfo(client));
                    }
                }

                if (interest.HasFlag(SubscriptionInterest.Presence))
                {
                    // Subscribe to presence events
                    ArrayUtils.AddUnique(ref this.PresenceSubscribers, client);
                }
            }
        }