// Conecta ao servidor assincronamente public override IEnumerator startConnection(WWWForm form = null, object state = null) { WWW conn = setUpConnection(form); if (!Application.isPlaying) { #if UNITY_EDITOR ContinuationManager.Add(() => conn.isDone, () => { // Verifica se houve erro if (!string.IsNullOrEmpty(conn.error)) { //Debug.Log("WWW failed: " + conn.error); sendToCallback(conn.error, ExtensionJSon.Empty(), state); } JSonReader reader = new JSonReader(); IJSonObject data = null; bool er = false; // Faz o parse da resposta try { data = reader.ReadAsJSonObject(conn.text); } catch (JSonReaderException) { Debug.LogError("Error parsing Json: " + conn.text); sendToCallback("Error parsing json", null, state); er = true; } if (!er) { sendToCallback(null, data, state); } // Debug.Log("WWW result : " + conn.text); }); #endif } else { yield return(conn); // Verifica se houve erro if (conn.error != null) { sendToCallback(conn.error, ExtensionJSon.Empty(), state); yield break; } JSonReader reader = new JSonReader(); IJSonObject data = null; // Faz o parse da resposta try { data = reader.ReadAsJSonObject(conn.text); } catch (JSonReaderException) { Debug.LogError("Error parsing Json: " + conn.text); sendToCallback("Error parsing json", null, state); yield break; } sendToCallback(null, data, state); } }
// Obtem o resultado da conexao private void handleConnection(WWW conn, string id, object state, byte[] form_data, Hashtable headers) { //Debug.Log(conn.text); // Verifica se houve erro if (conn.error != null) { Debug.LogError("Internal server error (" + url + "): " + conn.error); string error = ""; if (!messages.TryGetValue("server_error", out error)) { error = DEFAULT_ERROR_MESSAGE; } sendToCallbackPersistent(error, null, state, id, conn.url, form_data, headers); return; } JSonReader reader = new JSonReader(); IJSonObject data = null; // Faz o parse da resposta try { data = reader.ReadAsJSonObject(conn.text); } catch (JSonReaderException) { string fbError = conn.text; if (fbError.Contains("ask") && fbError.Contains("fb_token")) { new GameFacebook().login(); sendToCallbackPersistent(DEFAULT_FB_TOKEN_ERROR_MESSAGE, null, state, id, conn.url, form_data, headers); return; } Debug.LogError("Error parsing Json: " + conn.text); string error = ""; if (!messages.TryGetValue("json_error", out error)) { error = DEFAULT_ERROR_MESSAGE; } sendToCallbackPersistent(error, null, state, id, conn.url, form_data, headers); return; } // Verifica se nao houve resultado if (data == null) { sendToCallbackPersistent(null, null, state, id, conn.url, form_data, headers); return; } if (data.Contains("ask")) { // Caso peca senha,s avisa o erro if (data["ask"].ToString() == "password") { sendToCallbackPersistent(DEFAULT_ERROR_MESSAGE + "j", null, state, id, conn.url, form_data, headers); Save.Delete(PlayerPrefsKeys.TOKEN.ToString()); // Invalid token //Scene.Load("Login", Info.firstScene); } // Obtem o token do Facebook, caso necessario else if (data["ask"].ToString() == "fb_token") { new GameFacebook().login(); sendToCallbackPersistent(DEFAULT_FB_TOKEN_ERROR_MESSAGE, null, state, id, conn.url, form_data, headers); } // Refaz a conexao caso necessario e possivel //if (redo && GameGUI.components != null) GameGUI.components.StartCoroutine(startConnection(form, state, false)); //else Application.LoadLevel(GameGUI.components.login_scene); return; } // Salva o token enviado pelo caso necessario if (data.Contains("token")) { if (!Save.HasKey(PlayerPrefsKeys.TOKEN_EXPIRATION.ToString())) { Save.Set(PlayerPrefsKeys.TOKEN_EXPIRATION.ToString(), data["expiration"].ToString(), true); Save.Set(PlayerPrefsKeys.TOKEN.ToString(), data["token"].ToString(), true); } else { System.DateTime old_date = System.DateTime.Parse(Save.GetString(PlayerPrefsKeys.TOKEN_EXPIRATION.ToString())); System.DateTime new_date = System.DateTime.Parse(data["expiration"].ToString()); if (new_date > old_date) { Save.Set(PlayerPrefsKeys.TOKEN.ToString(), data["token"].ToString(), true); } } } // Trata a mensagem de erro if (data.Contains("error")) { Debug.LogError("Server error: " + data["error"].ToString()); string error = ""; if (!messages.TryGetValue(data["error"].ToString(), out error)) { error = DEFAULT_ERROR_MESSAGE; } sendToCallbackPersistent(error, ExtensionJSon.Empty(), state, id, conn.url, form_data, headers); return; } // Chama o callback com o resultado /*if (Scene.GetCurrent() != GameGUI.components.login_scene) */ data = data["result", (IJSonObject)null]; sendToCallbackPersistent(null, data, state, id, conn.url, form_data, headers); }