Esempio n. 1
0
 void Run()
 {
     snapshot = new DataSnapshot(json);
     isDone   = true;
 }
Esempio n. 2
0
        protected IEnumerator RequestCoroutine(string url, byte[] postData, Dictionary <string, string> headers, Action <Firebase, DataSnapshot> OnSuccess, Action <Firebase, FirebaseError> OnFailed)
        {
            using (WWW www = (headers != null) ? new WWW(url, postData, headers) : (postData != null) ? new WWW(url, postData) : new WWW(url))
            {
                // Wait until load done
                yield return(www);

                if (!string.IsNullOrEmpty(www.error))
                {
                    HttpStatusCode status     = 0;
                    string         errMessage = "";

                    // Parse status code
                    if (www.responseHeaders.ContainsKey("STATUS"))
                    {
                        string   str        = www.responseHeaders["STATUS"] as string;
                        string[] components = str.Split(' ');
                        int      code       = 0;
                        if (components.Length >= 3 && int.TryParse(components[1], out code))
                        {
                            status = (HttpStatusCode)code;
                        }
                    }

                    if (www.error.Contains("crossdomain.xml") || www.error.Contains("Couldn't resolve"))
                    {
                        errMessage = "No internet connection or crossdomain.xml policy problem";
                    }
                    else
                    {
                        // Parse error message

                        try
                        {
                            if (!string.IsNullOrEmpty(www.text))
                            {
                                Dictionary <string, object> obj = Json.Deserialize(www.text) as Dictionary <string, object>;

                                if (obj != null && obj.ContainsKey("error"))
                                {
                                    errMessage = obj["error"] as string;
                                }
                            }
                        }
                        catch
                        {
                        }
                    }



                    if (OnFailed != null)
                    {
                        if (string.IsNullOrEmpty(errMessage))
                        {
                            errMessage = www.error;
                        }

                        if (errMessage.Contains("Failed downloading"))
                        {
                            errMessage = "Request failed with no info of error.";
                        }

                        OnFailed(this, new FirebaseError(status, errMessage));
                    }

#if UNITY_EDITOR
                    Debug.LogWarning(www.error + " (" + (int)status + ")\nResponse Message: " + errMessage);
#endif
                }
                else
                {
                    ParserJsonToSnapshot parser = new ParserJsonToSnapshot(www.text);

                    while (!parser.isDone)
                    {
                        yield return(null);
                    }

                    DataSnapshot snapshot = parser.snapshot;
                    if (OnSuccess != null)
                    {
                        OnSuccess(this, snapshot);
                    }
                }
            }
        }