IEnumerator SendMessageAsync(UnityTask <bool> task)
        {
            var connectionUrl = Url;

            if (IsCluster)
            {
                var cTask = ClusterClient.GetClusterServerWithRetry(Url, ApplicationKey);
                yield return(TaskManager.StartRoutine(cTask.WaitRoutine()));

                if (cTask.IsFaulted)
                {
                    task.Exception = cTask.Exception;
                    task.Status    = TaskStatus.Faulted;
                    yield break;
                }

                connectionUrl = cTask.Result;
            }

            connectionUrl = connectionUrl.Last() == '/' ? connectionUrl : connectionUrl + "/";

            var postParameters = String.Format("AT={0}&AK={1}&PK={2}&C={3}&M={4}", AuthenticationToken, ApplicationKey, PrivateKey, Channel, HttpUtility.UrlEncode(Message));

            var hTask = Client.PostAsync(String.Format("{0}send", connectionUrl), postParameters);

            yield return(TaskManager.StartRoutine(hTask.WaitRoutine()));

            if (hTask.IsFaulted)
            {
                task.Exception = hTask.Exception;
                task.Status    = TaskStatus.Faulted;
                yield break;
            }

            task.Result = hTask.StatusCode == HttpStatusCode.Created;
        }
        /// <summary>
        /// Does the get cluster server logic.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="applicationKey"></param>
        /// <returns></returns>
        public static UnityTask <string> GetClusterServerWithRetry(string url, string applicationKey)
        {
            var client = new ClusterClient(url, applicationKey);

            return(client.GetClusterServerWithRetry());
        }
        /// <summary>
        /// Does the get cluster server logic.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="applicationKey"></param>
        /// <returns></returns>
        public static UnityTask<string> GetClusterServerWithRetry(string url, string applicationKey)
        {
            var client = new ClusterClient(url, applicationKey);

            return client.GetClusterServerWithRetry();
        }
        IEnumerator PostAuthenticationAsync(UnityTask <bool> task)
        {
            var connectionUrl = Url;

            if (IsCluster)
            {
                var cTask = ClusterClient.GetClusterServerWithRetry(Url, ApplicationKey);
                yield return(TaskManager.StartRoutine(cTask.WaitRoutine()));

                if (cTask.IsFaulted)
                {
                    task.Exception = cTask.Exception;
                    task.Status    = TaskStatus.Faulted;
                    yield break;
                }

                connectionUrl = cTask.Result;
            }

            connectionUrl = connectionUrl[connectionUrl.Length - 1] == '/' ? connectionUrl : connectionUrl + "/";

            var postParameters = String.Format("AT={0}&PVT={1}&AK={2}&TTL={3}&PK={4}", AuthenticationToken, AuthenticationTokenIsPrivate
                ? 1 : 0, ApplicationKey, TimeToLive, PrivateKey);

            if (Permissions != null && Permissions.Count > 0)
            {
                postParameters += String.Format("&TP={0}", Permissions.Count);
                foreach (var permission in Permissions)
                {
                    var permissionItemText = String.Format("{0}=", permission.Key);
                    var list = new List <ChannelPermissions>(permission.Value);
                    foreach (var permissionItemValue in list)
                    {
                        permissionItemText += ((char)permissionItemValue).ToString();
                    }

                    postParameters += String.Format("&{0}", permissionItemText);
                }
            }

            var hTask = Client.PostAsync(String.Format("{0}authenticate", connectionUrl), postParameters);

            yield return(TaskManager.StartRoutine(hTask.WaitRoutine()));

            if (hTask.IsFaulted)
            {
                task.Exception = hTask.Exception;
                task.Status    = TaskStatus.Faulted;
                yield break;
            }

            if (hTask.StatusCode == HttpStatusCode.Unauthorized)
            {
                task.Exception = new OrtcException(OrtcExceptionReason.Unauthorized, String.Format("Unable to connect to the authentication server {0}.", connectionUrl));
                task.Status    = TaskStatus.Faulted;
                yield break;
            }


            task.Result = hTask.IsSuccess;
        }