IEnumerator ReceiveChat() { while (true) { var request = new MoveRequest(); // Same as the moveRequest, but different endpoint request.secret = UnitySingleton.secret; request.gameID = UnitySingleton.match.gameID; var msg = JsonUtility.ToJson(request); var host = Net.GetServerHost(); // Post to our api using (UnityWebRequest www = Net.GoodPost(host + "/v1/getChat", msg)) { yield return(www.SendWebRequest()); if (www.isNetworkError) { // Exponential backoff Debug.LogError("ReceiveMoves network error: " + www.error); this.failedChatConnections++; yield return(new WaitForSeconds(Mathf.Pow(2f, this.failedChatConnections) / 10f * Random.Range(.5f, 1.0f))); if (this.failedChatConnections >= 10) { // TODO OnBackPress(); } else { } } else if (www.isHttpError) { Debug.Log("Chat error: " + www.downloadHandler.text); } else { this.failedChatConnections = 0; /* TODO * var response = JsonUtility.FromJson<ChatResponse>(www.downloadHandler.text); * if (response != null) { * if (response.chat) { * // Move received! * AppendNewChat(response.chat); * } * } */ } } yield return(new WaitForSeconds(1f)); } }
IEnumerator ReceiveMoves() { while (true) { var request = new MoveRequest(); request.secret = UnitySingleton.secret; request.gameID = UnitySingleton.match.gameID; var msg = JsonUtility.ToJson(request); var host = Net.GetServerHost(); // Post to our api using (UnityWebRequest www = Net.GoodPost(host + "/v1/getMove", msg)) { yield return(www.SendWebRequest()); if (www.isNetworkError) { // Exponential backoff Debug.LogError("ReceiveMoves network error: " + www.error); this.failedReceives++; yield return(new WaitForSeconds(Mathf.Pow(2f, this.failedReceives) / 10f * UnityEngine.Random.Range(.5f, 1.0f))); if (this.failedReceives <= 10) { // TODO spinning beachball? } else { Debug.LogError("Gave up on server"); QuitGame(); } } else if (www.isHttpError) { Debug.Log("ReceiveMoves error: " + www.downloadHandler.text); this.failedReceives++; yield return(new WaitForSeconds(Mathf.Pow(2f, this.failedReceives) / 10f * UnityEngine.Random.Range(.5f, 1.0f))); } else { this.failedReceives = 0; var response = JsonUtility.FromJson <MoveResponse>(www.downloadHandler.text); if (response != null) { if (response.move != "") { Debug.Log(response.move); if (response.move.StartsWith("timeout")) { if (response.move.Contains("white")) { StartCoroutine(VictoryAnimation("White ran out of time.")); yield break; } else { StartCoroutine(VictoryAnimation("Black ran out of time.")); yield break; } } // Move received! UnitySingleton.lastMove = response; MakeReceivedMove(response.move); Debug.Log("received move " + www.downloadHandler.text); //DateTime.TryParse(response.turnStarted, out UnitySingleton.turnStarted ); if (IAmBlack) { blackTurnStarted.Invoke(); } else { whiteTurnStarted.Invoke(); } } } } } } }