private async Task <PNResult <PNPublishResult> > Signal(string channel, object message, Dictionary <string, object> metaData, Dictionary <string, object> externalQueryParam)
        {
            PNResult <PNPublishResult> ret = new PNResult <PNPublishResult>();

            if (string.IsNullOrEmpty(channel) || string.IsNullOrEmpty(channel.Trim()) || message == null)
            {
                PNStatus errStatus = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Missing Channel or Message", new ArgumentException("Missing Channel or Message"))
                };
                ret.Status = errStatus;
                return(ret);
            }

            if (string.IsNullOrEmpty(config.PublishKey) || string.IsNullOrEmpty(config.PublishKey.Trim()) || config.PublishKey.Length <= 0)
            {
                PNStatus errStatus = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid publish key", new ArgumentException("Invalid publish key"))
                };
                ret.Status = errStatus;
                return(ret);
            }

            if (string.IsNullOrEmpty(config.SubscribeKey) || string.IsNullOrEmpty(config.SubscribeKey.Trim()) || config.SubscribeKey.Length <= 0)
            {
                PNStatus errStatus = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid subscribe key", new ArgumentException("Invalid subscribe key"))
                };
                ret.Status = errStatus;
                return(ret);
            }

            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");

            Uri request = urlBuilder.BuildSignalRequest("GET", "", channel, message, metaData, externalQueryParam);

            RequestState <PNPublishResult> requestState = new RequestState <PNPublishResult>();

            requestState.Channels          = new[] { channel };
            requestState.ResponseType      = PNOperationType.PNSignalOperation;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            Tuple <string, PNStatus> JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false).ConfigureAwait(false);

            ret.Status = JsonAndStatusTuple.Item2;
            string json = JsonAndStatusTuple.Item1;

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse <PNPublishResult>(requestState, json);
                if (result != null && result.Count >= 3)
                {
                    int publishStatus;
                    Int32.TryParse(result[0].ToString(), out publishStatus);
                    if (publishStatus == 1)
                    {
                        List <object> resultList = ProcessJsonResponse(requestState, json);
                        if (resultList != null && resultList.Count > 0)
                        {
                            ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog);
                            PNPublishResult responseResult  = responseBuilder.JsonToObject <PNPublishResult>(resultList, true);
                            if (responseResult != null)
                            {
                                ret.Result = responseResult;
                            }
                        }
                        else
                        {
                            PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString());
                            PNStatus         status   = new StatusBuilder(config, jsonLibrary).CreateStatusResponse <PNPublishResult>(PNOperationType.PNSignalOperation, category, requestState, 400, new PNException(json));
                            ret.Status = status;
                            ret.Result = default(PNPublishResult);
                        }
                    }
                }
            }

            return(ret);
        }
        private void Signal(string channel, object message, Dictionary <string, object> metaData, Dictionary <string, object> externalQueryParam, PNCallback <PNPublishResult> callback)
        {
            if (string.IsNullOrEmpty(channel) || string.IsNullOrEmpty(channel.Trim()) || message == null)
            {
                PNStatus status = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Missing Channel or Message", new ArgumentException("Missing Channel or Message"))
                };
                callback.OnResponse(null, status);
                return;
            }

            if (string.IsNullOrEmpty(config.PublishKey) || string.IsNullOrEmpty(config.PublishKey.Trim()) || config.PublishKey.Length <= 0)
            {
                PNStatus status = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid publish key", new ArgumentException("Invalid publish key"))
                };
                callback.OnResponse(null, status);
                return;
            }

            if (string.IsNullOrEmpty(config.SubscribeKey) || string.IsNullOrEmpty(config.SubscribeKey.Trim()) || config.SubscribeKey.Length <= 0)
            {
                PNStatus status = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid subscribe key", new ArgumentException("Invalid subscribe key"))
                };
                callback.OnResponse(null, status);
                return;
            }

            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");

            Uri request = urlBuilder.BuildSignalRequest("GET", "", channel, message, metaData, externalQueryParam);

            RequestState <PNPublishResult> requestState = new RequestState <PNPublishResult>();

            requestState.Channels          = new[] { channel };
            requestState.ResponseType      = PNOperationType.PNSignalOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            UrlProcessRequest(request, requestState, false).ContinueWith(r =>
            {
                string json = r.Result.Item1;
                if (!string.IsNullOrEmpty(json))
                {
                    List <object> result = ProcessJsonResponse(requestState, json);

                    if (result != null && result.Count >= 3)
                    {
                        int signalStatus;
                        Int32.TryParse(result[0].ToString(), out signalStatus);
                        if (signalStatus == 1)
                        {
                            ProcessResponseCallbacks(result, requestState);
                        }
                        else
                        {
                            PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString());
                            PNStatus status           = new StatusBuilder(config, jsonLibrary).CreateStatusResponse <PNPublishResult>(PNOperationType.PNSignalOperation, category, requestState, 400, new PNException(json));
                            if (requestState.PubnubCallback != null)
                            {
                                requestState.PubnubCallback.OnResponse(default(PNPublishResult), status);
                            }
                        }
                    }
                    else
                    {
                        ProcessResponseCallbacks(result, requestState);
                    }
                }
            }, TaskContinuationOptions.ExecuteSynchronously).Wait();
        }
        private void Publish(string channel, object message, bool storeInHistory, int ttl, Dictionary <string, object> metaData, Dictionary <string, object> externalQueryParam, PNCallback <PNPublishResult> callback)
        {
            if (string.IsNullOrEmpty(channel) || string.IsNullOrEmpty(channel.Trim()) || message == null)
            {
                PNStatus status = new PNStatus();
                status.Error     = true;
                status.ErrorData = new PNErrorData("Missing Channel or Message", new ArgumentException("Missing Channel or Message"));
                callback.OnResponse(null, status);
                return;
            }

            if (string.IsNullOrEmpty(config.PublishKey) || string.IsNullOrEmpty(config.PublishKey.Trim()) || config.PublishKey.Length <= 0)
            {
                PNStatus status = new PNStatus();
                status.Error     = true;
                status.ErrorData = new PNErrorData("Invalid publish key", new MissingMemberException("Invalid publish key"));
                callback.OnResponse(null, status);
                return;
            }

            if (callback == null)
            {
                return;
            }

            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr);

            urlBuilder.PubnubInstanceId = (PubnubInstance != null) ? PubnubInstance.InstanceId : "";
            Uri request = urlBuilder.BuildPublishRequest(channel, message, storeInHistory, ttl, metaData, httpPost, null, externalQueryParam);

            RequestState <PNPublishResult> requestState = new RequestState <PNPublishResult>();

            requestState.Channels          = new [] { channel };
            requestState.ResponseType      = PNOperationType.PNPublishOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            string json = "";

            if (this.httpPost)
            {
                requestState.UsePostMethod = true;
                string postMessage = JsonEncodePublishMsg(message);
                json = UrlProcessRequest <PNPublishResult>(request, requestState, false, postMessage);
            }
            else
            {
                json = UrlProcessRequest <PNPublishResult>(request, requestState, false);
            }

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse <PNPublishResult>(requestState, json);

                if (result != null && result.Count >= 3)
                {
                    int publishStatus;
                    Int32.TryParse(result[0].ToString(), out publishStatus);
                    if (publishStatus == 1)
                    {
                        ProcessResponseCallbacks(result, requestState);
                    }
                    else
                    {
                        PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString());
                        PNStatus         status   = new StatusBuilder(config, jsonLibrary).CreateStatusResponse <PNPublishResult>(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(json));
                        if (requestState.PubnubCallback != null)
                        {
                            requestState.PubnubCallback.OnResponse(default(PNPublishResult), status);
                        }
                    }
                }
                else
                {
                    ProcessResponseCallbacks(result, requestState);
                }
            }

            CleanUp();
        }
Exemple #4
0
        private void Fire(string channel, object message, bool storeInHistory, int ttl, Dictionary <string, object> metaData, Dictionary <string, object> externalQueryParam, PNCallback <PNPublishResult> callback)
        {
            if (string.IsNullOrEmpty(channel) || string.IsNullOrEmpty(channel.Trim()) || message == null)
            {
                PNStatus status = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Missing Channel or Message", new ArgumentException("Missing Channel or Message"))
                };
                callback.OnResponse(null, status);
                return;
            }

            if (string.IsNullOrEmpty(config.PublishKey) || string.IsNullOrEmpty(config.PublishKey.Trim()) || config.PublishKey.Length <= 0)
            {
                PNStatus status = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid publish key", new ArgumentException("Invalid publish key"))
                };
                callback.OnResponse(null, status);
                return;
            }

            if (callback == null)
            {
                return;
            }

            Dictionary <string, string> urlParam = new Dictionary <string, string>();

            urlParam.Add("norep", "true");

            string             requestMethodName = (this.httpPost) ? "POST" : "GET";
            IUrlRequestBuilder urlBuilder        = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");

            Uri request = urlBuilder.BuildPublishRequest(requestMethodName, "", channel, message, storeInHistory, ttl, metaData, urlParam, externalQueryParam);

            RequestState <PNPublishResult> requestState = new RequestState <PNPublishResult>();

            requestState.Channels          = new [] { channel };
            requestState.ResponseType      = PNOperationType.PNFireOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            string json = "";

            if (this.httpPost)
            {
                requestState.UsePostMethod = true;
                Dictionary <string, object> messageEnvelope = new Dictionary <string, object>();
                messageEnvelope.Add("message", message);
                string postMessage = jsonLibrary.SerializeToJsonString(messageEnvelope);
                UrlProcessRequest(request, requestState, false, postMessage).ContinueWith(r =>
                {
                    json = r.Result.Item1;
                }, TaskContinuationOptions.ExecuteSynchronously).Wait();
            }
            else
            {
                UrlProcessRequest(request, requestState, false).ContinueWith(r =>
                {
                    json = r.Result.Item1;
                }, TaskContinuationOptions.ExecuteSynchronously).Wait();
            }

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse <PNPublishResult>(requestState, json);
                if (result != null && result.Count >= 3)
                {
                    int publishStatus;
                    Int32.TryParse(result[0].ToString(), out publishStatus);
                    if (publishStatus == 1)
                    {
                        ProcessResponseCallbacks(result, requestState);
                    }
                    else
                    {
                        PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString());
                        PNStatus         status   = new StatusBuilder(config, jsonLibrary).CreateStatusResponse <PNPublishResult>(PNOperationType.PNFireOperation, category, requestState, 400, new PNException(json));
                        if (requestState.PubnubCallback != null)
                        {
                            requestState.PubnubCallback.OnResponse(default(PNPublishResult), status);
                        }
                    }
                }
                else
                {
                    ProcessResponseCallbacks(result, requestState);
                }
            }
        }
        private void ProcessFileMessagePublish(Dictionary <string, object> externalQueryParam, PNCallback <PNPublishFileMessageResult> callback)
        {
            if (string.IsNullOrEmpty(this.channelName) || string.IsNullOrEmpty(this.channelName.Trim()))
            {
                PNStatus status = new PNStatus();
                status.Error     = true;
                status.ErrorData = new PNErrorData("Missing Channel or Message", new ArgumentException("Missing Channel or Message"));
                callback.OnResponse(null, status);
                return;
            }

            Dictionary <string, object> publishPayload = new Dictionary <string, object>();

            if (this.msg != null && !string.IsNullOrEmpty(this.msg.ToString()))
            {
                publishPayload.Add("message", this.msg);
            }
            publishPayload.Add("file", new Dictionary <string, string> {
                { "id", currentFileId },
                { "name", currentFileName }
            });

            RequestState <PNPublishFileMessageResult> requestState = new RequestState <PNPublishFileMessageResult>();

            requestState.ResponseType      = PNOperationType.PNPublishFileMessageOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;


            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");
            Uri request = urlBuilder.BuildPublishFileMessageRequest("GET", "", this.channelName, publishPayload, this.storeInHistory, this.ttl, this.userMetadata, null, externalQueryParam);

            string json = "";

            UrlProcessRequest(request, requestState, false).ContinueWith(r =>
            {
                json = r.Result.Item1;
            }, TaskContinuationOptions.ExecuteSynchronously).Wait();

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse(requestState, json);

                if (result != null && result.Count >= 3)
                {
                    int publishStatus;
                    Int32.TryParse(result[0].ToString(), out publishStatus);
                    if (publishStatus == 1)
                    {
                        ProcessResponseCallbacks(result, requestState);
                    }
                    else
                    {
                        PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString());
                        PNStatus         status   = new StatusBuilder(config, jsonLibrary).CreateStatusResponse <PNPublishFileMessageResult>(PNOperationType.PNPublishFileMessageOperation, category, requestState, 400, new PNException(json));
                        if (requestState.PubnubCallback != null)
                        {
                            requestState.PubnubCallback.OnResponse(default(PNPublishFileMessageResult), status);
                        }
                    }
                }
                else
                {
                    ProcessResponseCallbacks(result, requestState);
                }
            }
        }