/// <summary> /// Makes the API call to get a path given a start node, end node, and algorithm to use /// </summary> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <param name="algo"></param> private void GetPath(int startIndex, int endIndex, int algo) { string url = "http://localhost:5000/get_path"; string inputJson = "{\"algorithm\":" + algo.ToString() + ",\"source\":" + startIndex.ToString() + ",\"target\":" + endIndex.ToString() + "}"; PostDataCallback callback = OnGetPathComplete; UnityEngine.Debug.Log("Sending Post request to Get Path from API..."); API.PostData(inputJson, url, callback, JsonPaths); }
public void PostData(string json, string url, PostDataCallback callback, List <string> paths) { Hashtable headers = new Hashtable(); headers.Add("Content-Type", "application/json"); byte[] body = Encoding.UTF8.GetBytes(json); WWW www = new WWW(url, body, HashtableToDictionary <string, string>(headers)); StartCoroutine(PostdataEnumerator(www, callback, paths)); }
/// <summary> /// Used to Set/Update the graph and make an API call to update the Adj Matrix /// </summary> /// <returns></returns> private IEnumerator SetGraph() { while (true) { string url = "http://localhost:5000/init_graph_unity"; string inputJson = AdjMatrixToJSON(); PostDataCallback callback = StartGetPaths; UnityEngine.Debug.Log("Sending Post request to init graph in API...\n" + "input json: \n" + inputJson); API.PostData(inputJson, url, callback); yield return(new WaitForSeconds(SetGraphInterval)); } }
private IEnumerator PostdataEnumerator(WWW www, PostDataCallback callback) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); yield return(www); stopwatch.Stop(); UnityEngine.Debug.Log("Post request completed" + "\nCompleted in " + stopwatch.ElapsedMilliseconds + "ms."); if (www.error != null) { UnityEngine.Debug.Log("Error returned from API: \n" + www.error + "\nCompleted in "); } else { UnityEngine.Debug.Log("Response returned from API: \n" + www.text); callback(); } }