示例#1
0
        /**
         * Request URL
         *
         * @param List<string> request of url directories.
         * @respCallback is a delegate to get async JSON response back
         */
        private void _request(List<string> url_components, ResponseCallback respCallback, ResponseType type)
        {
            try
            {
                StringBuilder url = new StringBuilder();

                // Add Origin To The Request
                url.Append(this.ORIGIN);

                // Generate URL with UTF-8 Encoding
                foreach (string url_bit in url_components)
                {
                    url.Append("/");
                    url.Append(_encodeURIcomponent(url_bit));
                }
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
                request.Method = "GET";
                request.UserAgent = "C#-WP7";
                request.Headers["V"] = "3.1";
                request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
                RequestState myRequestState = new RequestState();
                myRequestState.request = request;
                myRequestState.cb = respCallback;
                myRequestState.respType = type;
                if (type == ResponseType.Subscribe)
                {
                    myRequestState.channel = url_components[2];
                }
                IAsyncResult result = (IAsyncResult)request.BeginGetResponse(requestCallBack, myRequestState);
            }
            catch (Exception)
            {

            }
        }
示例#2
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);
            }
        }