/// <summary>
 /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
 /// </summary>
 /// <param name="firebase">Firebase.</param>
 /// <param name="refreshRate">Refresh rate (in seconds).</param>
 /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
 public SimpleFirebaseObserver(SimpleFirebase _firebase, float _refreshRate, SimpleFirebaseParam _getParam)
 {
     active       = false;
     lastSnapshot = null;
     firebase     = _firebase;
     refreshRate  = _refreshRate;
     getParam     = _getParam.Parameter;
     target       = _firebase.Copy();
 }
 /// <summary>
 /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
 /// </summary>
 /// <param name="firebase">Firebase.</param>
 /// <param name="refreshRate">Refresh rate (in seconds).</param>
 /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
 public SimpleFirebaseObserver(SimpleFirebase _firebase, float _refreshRate, string _getParam = "")
 {
     active       = false;
     lastSnapshot = null;
     firebase     = _firebase;
     refreshRate  = _refreshRate;
     getParam     = _getParam;
     target       = _firebase.Copy();
     routine      = null;
 }
        /// <summary>
        /// Stop the observer.
        /// </summary>
        public void Stop()
        {
            active = false;
            target.OnGetSuccess -= CompareSnapshot;
            lastSnapshot         = null;

            if (routine != null)
            {
                target.root.StopCoroutine(routine);
                routine = null;
            }
        }
        void CompareSnapshot(SimpleFirebase target, SimpleDataSnapshot snapshot)
        {
            if (firstTime)
            {
                firstTime    = false;
                lastSnapshot = snapshot;
                return;
            }

            if (!snapshot.RawJson.Equals(lastSnapshot.RawJson))
            {
                if (OnChange != null)
                {
                    OnChange(firebase, snapshot);
                }
            }

            lastSnapshot = snapshot;
        }
        protected IEnumerator RequestCoroutine(string url, byte[] postData, Dictionary <string, string> headers, Action <SimpleFirebase, SimpleDataSnapshot> OnSuccess, Action <SimpleFirebase, SimpleFirebaseError> 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 SimpleFirebaseError(status, errMessage));
                    }

#if UNITY_EDITOR
                    Debug.LogWarning(www.error + " (" + (int)status + ")\nResponse Message: " + errMessage);
#endif
                }
                else
                {
                    SimpleDataSnapshot snapshot = new SimpleDataSnapshot(www.text);
                    if (OnSuccess != null)
                    {
                        OnSuccess(this, snapshot);
                    }
                }
            }
        }
예제 #6
0
 protected void OnSuccess(SimpleFirebase sender, SimpleDataSnapshot snapshot)
 {
     --count;
     StartNextCommand();
     ClearCallbacks(sender);
 }