public TrackItem BuildTrackItem(string action, int level, bool persist, params string[] v) { TrackItem item = new TrackItem(); item.action = action; item.v = v; item.appVersion = Application.version; item.confVersion = _confVersion; item.time = (int)TimeUtil.GetUTCTimestamp(); item.level = level; item.persist = persist; return(item); }
public Dictionary <string, object> BuildTrackMessage(List <TrackItem> items) { if (items == null || items.Count == 0) { return(null); } Dictionary <string, object> json = new Dictionary <string, object>(7); // common parameters json.Add("app", appId); json.Add("client", GetClientType()); json.Add("uid", TrackId.id); json.Add("confVersion", items[0].confVersion); json.Add("ver", items[0].appVersion); json.Add("fbid", _facebookId); // add events List <object> events = new List <object>(items.Count); for (int i = 0; i < items.Count; i++) { TrackItem item = items [i]; Dictionary <string, object> jItem = new Dictionary <string, object>(3 + item.v.Length); jItem.Add("action", item.action); jItem.Add("level", item.level); jItem.Add("time", "" + item.time); for (int j = 0; j < item.v.Length; j++) { string v = item.v [j]; if (v == null || v.Length == 0) { continue; } jItem.Add("v" + (j + 1), v); } events.Add(jItem); } json.Add("events", events); return(json); }
public void SaveQueue() { // get items that need persisting List <TrackItem> items = new List <TrackItem> (); for (int i = 0; i < _queue.Count; i++) { TrackItem item = _queue [i]; if (item.persist) { items.Add(item); } } BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + cacheFile); bf.Serialize(file, items); file.Close(); _dirty = false; }
IEnumerator sendLoop() { while (true) { yield return(new WaitForSeconds(_lastSucc ? sendInterval : retryDelay)); if (_queue.Count == 0) { continue; } // get list of items to send List <TrackItem> items = new List <TrackItem> (); items.Add(_queue[0]); for (int i = 1; i < _queue.Count && items.Count < maxBatch; i++) { TrackItem item = _queue [i]; if (item.appVersion != items[0].appVersion || item.confVersion != items[0].confVersion) // batch items with the same app version and conf version { continue; } items.Add(item); } string jsonStr; string sign; BuildMessage(items, out jsonStr, out sign); if (debug) { Debug.Log("Sending track: " + jsonStr); } Dictionary <string, string> headers = new Dictionary <string, string> (); headers ["sign"] = sign; headers ["Content-Type"] = "application/json"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(jsonStr); WWW www = new WWW(baseUrl, bytes, headers); yield return(www); if (!string.IsNullOrEmpty(www.error)) { if (debug) { Debug.Log("Error when sending track: " + www.error); } _lastSucc = false; continue; } else // success, remove from queue { if (debug) { Debug.Log("Successfully sent track"); } for (int i = 0; i < items.Count; i++) { if (items[i].persist) { _dirty = true; } _queue.Remove(items[i]); } _lastSucc = true; } } }