示例#1
0
        /// <summary>
        /// Indicates whether is subscribed to a channel.
        /// </summary>
        /// <param name="channel">The channel name.</param>
        /// <returns>
        ///   <c>true</c> if subscribed to the channel; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSubscribed(string channel)
        {
            bool result = false;

            if (!IsConnected)
            {
                DelegateExceptionCallback(new OrtcNotConnectedException("Not connected"));
            }
            else if (String.IsNullOrEmpty(channel))
            {
                DelegateExceptionCallback(new OrtcEmptyFieldException("Channel is null or empty"));
            }
            else if (!channel.OrtcIsValidInput())
            {
                DelegateExceptionCallback(new OrtcInvalidCharactersException("Channel has invalid characters"));
            }
            else
            {
                result = false;

                if (_client._subscribedChannels.ContainsKey(channel))
                {
                    ChannelSubscription channelSubscription = null;
                    _client._subscribedChannels.TryGetValue(channel, out channelSubscription);

                    if (channelSubscription != null && channelSubscription.IsSubscribed)
                    {
                        result = true;
                    }
                }
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// Subscribes to a channel with notifications.
        /// </summary>
        /// <param name="channel">Channel name.</param>
        /// <param name="subscribeOnReconnected">Subscribe to the specified channel on reconnect.</param>
        /// <param name="onMessage"><see cref="OnMessageDelegate"/> callback.</param>
        /// <example>
        ///   <code>
        /// ortcClient.SubscribeWithNotifications("channelName", true, OnMessageCallback);
        /// private void OnMessageCallback(object sender, string channel, string message)
        /// {
        /// // Do something
        /// }
        ///   </code>
        ///   </example>
        public void SubscribeWithNotifications(string channel, bool subscribeOnReconnected, OnMessageDelegate onMessage)
        {
            if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android)
            {
                if (!IsConnected)
                {
                    DelegateExceptionCallback(new OrtcNotConnectedException("Not connected"));
                }
                else if (String.IsNullOrEmpty(channel))
                {
                    DelegateExceptionCallback(new OrtcEmptyFieldException("Channel is null or empty"));
                }
                else if (!channel.OrtcIsValidInput())
                {
                    DelegateExceptionCallback(new OrtcInvalidCharactersException("Channel has invalid characters"));
                }
                else if (channel.Length > Constants.MAX_CHANNEL_SIZE)
                {
                    DelegateExceptionCallback(new OrtcMaxLengthException(String.Format("Channel size exceeds the limit of {0} characters", Constants.MAX_CHANNEL_SIZE)));
                }
                else if (_client._subscribedChannels.ContainsKey(channel))
                {
                    ChannelSubscription channelSubscription = null;
                    _client._subscribedChannels.TryGetValue(channel, out channelSubscription);

                    if (channelSubscription != null)
                    {
                        if (channelSubscription.IsSubscribing)
                        {
                            DelegateExceptionCallback(new OrtcSubscribedException(String.Format("Already subscribing to the channel {0}", channel)));
                        }
                        else if (channelSubscription.IsSubscribed)
                        {
                            DelegateExceptionCallback(new OrtcSubscribedException(String.Format("Already subscribed to the channel {0}", channel)));
                        }
                        else
                        {
                            _client.subscribe(channel, subscribeOnReconnected, onMessage, true);
                        }
                    }
                }
                else if (Device.OS == TargetPlatform.Android && String.IsNullOrEmpty(_googleProjectNumber))
                {
                    DelegateExceptionCallback(new OrtcGcmException("You have to provide a your Google Project ID to use the GCM notifications"));
                }
                else
                {
                    _client.subscribe(channel, subscribeOnReconnected, onMessage, true);
                }
            }
            else
            {
                DelegateExceptionCallback(new OrtcNotSupportedPlatformException("Subscribe with notifications is only available on platforms Android/iOS"));
            }
        }
示例#3
0
        /// <summary>
        /// Subscribes to a channel.
        /// </summary>
        /// <param name="channel">Channel name.</param>
        /// <param name="subscribeOnReconnected">Subscribe to the specified channel on reconnect.</param>
        /// <param name="onMessage"><see cref="OnMessageDelegate"/> callback.</param>
        /// <example>
        ///   <code>
        /// ortcClient.Subscribe("channelName", true, OnMessageCallback);
        /// private void OnMessageCallback(object sender, string channel, string message)
        /// {
        /// // Do something
        /// }
        ///   </code>
        ///   </example>
        public void Subscribe(string channel, bool subscribeOnReconnected, OnMessageDelegate onMessage)
        {
            if (!IsConnected)
            {
                DelegateExceptionCallback(new OrtcNotConnectedException("Not connected"));
            }
            else if (String.IsNullOrEmpty(channel))
            {
                DelegateExceptionCallback(new OrtcEmptyFieldException("Channel is null or empty"));
            }
            else if (!channel.OrtcIsValidInput())
            {
                DelegateExceptionCallback(new OrtcInvalidCharactersException("Channel has invalid characters"));
            }
            else if (channel.Length > Constants.MAX_CHANNEL_SIZE)
            {
                DelegateExceptionCallback(new OrtcMaxLengthException(String.Format("Channel size exceeds the limit of {0} characters", Constants.MAX_CHANNEL_SIZE)));
            }
            else if (_client._subscribedChannels.ContainsKey(channel))
            {
                ChannelSubscription channelSubscription = null;
                _client._subscribedChannels.TryGetValue(channel, out channelSubscription);

                if (channelSubscription != null)
                {
                    if (channelSubscription.IsSubscribing)
                    {
                        DelegateExceptionCallback(new OrtcSubscribedException(String.Format("Already subscribing to the channel {0}", channel)));
                    }
                    else if (channelSubscription.IsSubscribed)
                    {
                        DelegateExceptionCallback(new OrtcSubscribedException(String.Format("Already subscribed to the channel {0}", channel)));
                    }
                    else
                    {
                        _client.subscribe(channel, subscribeOnReconnected, onMessage, false);
                    }
                }
            }
            else
            {
                _client.subscribe(channel, subscribeOnReconnected, onMessage, false);
            }
        }
示例#4
0
 /// <summary>
 /// Unsubscribes from a channel.
 /// </summary>
 /// <param name="channel">Channel name.</param>
 /// <example>
 ///   <code>
 /// ortcClient.Unsubscribe("channelName");
 ///   </code>
 ///   </example>
 public void Unsubscribe(string channel)
 {
     if (!IsConnected)
     {
         DelegateExceptionCallback(new OrtcNotConnectedException("Not connected"));
     }
     else if (String.IsNullOrEmpty(channel))
     {
         DelegateExceptionCallback(new OrtcEmptyFieldException("Channel is null or empty"));
     }
     else if (!channel.OrtcIsValidInput())
     {
         DelegateExceptionCallback(new OrtcInvalidCharactersException("Channel has invalid characters"));
     }
     else if (!_client._subscribedChannels.ContainsKey(channel))
     {
         DelegateExceptionCallback(new OrtcNotSubscribedException(String.Format("Not subscribed to the channel {0}", channel)));
     }
     else if (channel.Length > Constants.MAX_CHANNEL_SIZE)
     {
         DelegateExceptionCallback(new OrtcMaxLengthException(String.Format("Channel size exceeds the limit of {0} characters", Constants.MAX_CHANNEL_SIZE)));
     }
     else if (_client._subscribedChannels.ContainsKey(channel))
     {
         ChannelSubscription channelSubscription = null;
         _client._subscribedChannels.TryGetValue(channel, out channelSubscription);
         if (channelSubscription != null && !channelSubscription.IsSubscribed)
         {
             DelegateExceptionCallback(new OrtcNotSubscribedException(String.Format("Not subscribed to the channel {0}", channel)));
         }
         else
         {
             _client.unsubscribe(channel, channelSubscription.isWithNotification, Device.OS);
         }
     }
     else
     {
         DelegateExceptionCallback(new OrtcNotSubscribedException(String.Format("Not subscribed to the channel {0}", channel)));
     }
 }
示例#5
0
        internal void subscribe(string channel, bool subscribeOnReconnected, OrtcClient.OnMessageDelegate onMessage, bool withNotifications)
        {
            var domainChannelCharacterIndex = channel.IndexOf(':');
            var channelToValidate           = channel;

            if (domainChannelCharacterIndex > 0)
            {
                channelToValidate = channel.Substring(0, domainChannelCharacterIndex + 1) + "*";
            }

            string hash = _permissions.Where(c => c.Key == channel || c.Key == channelToValidate).FirstOrDefault().Value;

            if (_permissions != null && _permissions.Count > 0 && String.IsNullOrEmpty(hash))
            {
                context.DelegateExceptionCallback(new OrtcNotConnectedException(String.Format("No permission found to subscribe to the channel '{0}'", channel)));
            }
            else
            {
                if (!_subscribedChannels.ContainsKey(channel))
                {
                    _subscribedChannels.Add(channel,
                                            new ChannelSubscription
                    {
                        IsSubscribing          = true,
                        IsSubscribed           = false,
                        SubscribeOnReconnected = subscribeOnReconnected,
                        OnMessage          = onMessage,
                        isWithNotification = withNotifications
                    });
                }

                try
                {
                    if (_subscribedChannels.ContainsKey(channel))
                    {
                        ChannelSubscription channelSubscription = null;
                        _subscribedChannels.TryGetValue(channel, out channelSubscription);

                        channelSubscription.IsSubscribing          = true;
                        channelSubscription.IsSubscribed           = false;
                        channelSubscription.SubscribeOnReconnected = subscribeOnReconnected;
                        channelSubscription.OnMessage          = onMessage;
                        channelSubscription.isWithNotification = withNotifications;
                    }

                    if (withNotifications)
                    {
                        if (Device.OS == TargetPlatform.Android)
                        {
                            if (String.IsNullOrEmpty(context._registrationId))
                            {
                                context.DelegateExceptionCallback(new OrtcGcmException("The application is not registered with GCM yet!"));
                                return;
                            }
                            else
                            {
                                string s = String.Format("subscribe;{0};{1};{2};{3};{4};GCM", context._applicationKey, context._authenticationToken, channel, hash, context._registrationId);
                                DoSend(s);
                            }
                        }
                        else if (Device.OS == TargetPlatform.iOS)
                        {
                            if (String.IsNullOrEmpty(Realtime.Messaging.Helpers.Settings.Token))
                            {
                                context.DelegateExceptionCallback(new OrtcApnsException("The application is not registered with Apns yet!"));
                                return;
                            }
                            else
                            {
                                string s = String.Format("subscribe;{0};{1};{2};{3};{4};Apns", context._applicationKey, context._authenticationToken, channel, hash, Realtime.Messaging.Helpers.Settings.Token);
                                DoSend(s);
                            }
                        }
                    }
                    else
                    {
                        string s = String.Format("subscribe;{0};{1};{2};{3}", context._applicationKey, context._authenticationToken, channel, hash);
                        DoSend(s);
                    }
                }
                catch (Exception ex)
                {
                    string exName = null;

                    if (ex.InnerException != null)
                    {
                        exName = ex.InnerException.GetType().Name;
                    }

                    switch (exName)
                    {
                    case "OrtcNotConnectedException":
                        // Server went down
                        if (context.IsConnected)
                        {
                            DoDisconnect();
                        }
                        break;

                    default:
                        context.DelegateExceptionCallback(new OrtcGenericException(String.Format("Unable to subscribe: {0}", ex)));
                        break;
                    }
                }
            }
        }