Example #1
0
        private void ReadCallBack(IAsyncResult asyncResult)
        {
            RequestState myRequestState = (RequestState)asyncResult.AsyncState;
            Stream       responseStream = myRequestState.streamResponse;

            try
            {
                int read = responseStream.EndRead(asyncResult);
                // Read the HTML page and then do something with it
                if (read > 0)
                {
                    myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
                    IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, ReadCallBack, myRequestState);
                    if (myRequestState.respType == ResponseType.History)
                    {
                        history = JsonConvert.DeserializeObject <List <object> >(myRequestState.requestData.ToString());
                        myRequestState.cb(history);
                    }
                    else if (myRequestState.respType == ResponseType.Subscribe)
                    {
                        List <object> lstObj = JsonConvert.DeserializeObject <List <object> >(myRequestState.requestData.ToString());
                        lstObj.Add(myRequestState.channel);
                        foreach (Channel_status cs in subscriptions)
                        {
                            if (cs.channel == myRequestState.channel && !cs.connected && !is_disconnect)
                            {
                                callback.disconnectCallback(myRequestState.channel);
                                is_disconnect = true;
                                break;
                            }
                        }
                        if (is_disconnect)
                        {
                            return;
                        }

                        subscribe = lstObj;
                        callback.responseCallback(myRequestState.channel, subscribe[0]);
                    }
                    else
                    {
                        myRequestState.cb(JsonConvert.DeserializeObject <List <object> >(myRequestState.requestData.ToString()));
                    }
                }
            }
            catch (Exception)
            {
                List <object> error = new List <object>();
                if (myRequestState.respType == ResponseType.Time)
                {
                    error.Add("0");
                }
                else if (myRequestState.respType == ResponseType.History)
                {
                    error.Add("Error: Failed JSONP HTTP Request.");
                }
                else if (myRequestState.respType == ResponseType.Publish)
                {
                    error.Add("0");
                    error.Add("Error: Failed JSONP HTTP Request.");
                }
                else if (myRequestState.respType == ResponseType.Subscribe)
                {
                    error.Add("0");
                    error.Add("0");
                }
                myRequestState.cb(error);
            }
        }