public IEnumerator SendAsync <TResponse>(object request, Func <TResponse, IEnumerator> callback = null) { return(SendMessage(request.GetType().Name, request, www => { var recv = HttpRecv <TResponse> .Create(www); if (recv == null) { Debug.LogError(string.Format("[HTTP RECV ERROR] {0}", www.text)); return; } var message = recv.data; IEnumerator coroutine; if (callback != null) { coroutine = callback(recv.data); } else if (Dispatcher.Dispatch(message, out coroutine) == false) { Debug.LogWarning(string.Format("未处理的消息: {0}\n{1}", message.GetType(), message.Dump())); return; } if (coroutine != null) { Singleton.Instance.StartCoroutine(coroutine); } })); }
/// <summary> /// 发送消息,可自动填充平台必要字段 /// </summary> /// <param name="do"></param> /// <param name="message"></param> /// <param name="callback"></param> /// <returns></returns> protected IEnumerator SendMessage(string @do, object message, Action <WWW> callback) { #region register-newaccount if (HasSession() == false) { Deserialize(); if (HasSession() == false) { var www = SendTo(LoginUrl, RegisterNewAccount.Name, new RegisterNewAccount() { mid = SystemInfo.deviceUniqueIdentifier }); yield return(www); if (OnResponse != null) { OnResponse(this, new HttpResponseEventArgs(www)); } var recv = HttpRecv <RegisterNewAccountOK> .Create(www); if (recv == null) { if (OnResponse == null) { Debug.LogError(string.Format("[WWW] ERROR {0} {1}\n{2}", LoginUrl, www.error, www.text)); } yield break; } this.GameID = recv.gameid; this.ZoneID = recv.zoneid; this.UID = recv.data.uid; this.SID = recv.data.sid; this.Serialize(); } } #endregion #region get-zone-gatewayurl if (string.IsNullOrEmpty(GatewayUrl)) { var www = SendTo(LoginUrl, GetZoneGatewayUrl.Name, new GetZoneGatewayUrl() { gameid = this.GameID, zoneid = this.ZoneID }); yield return(www); if (OnResponse != null) { OnResponse(this, new HttpResponseEventArgs(www)); } var recv = HttpRecv <GetZoneGatewayUrlOK> .Create(www); if (recv == null) { if (OnResponse == null) { Debug.LogError(string.Format("[WWW] ERROR {0} {1}\n{2}", LoginUrl, www.error, www.text)); } yield break; } this.GameID = recv.gameid; this.ZoneID = recv.zoneid; this.GatewayUrl = recv.data.gatewayurl; } #endregion { var www = SendTo(GatewayUrl, @do, message); yield return(www); if (OnResponse != null) { OnResponse(this, new HttpResponseEventArgs(www)); } if (!string.IsNullOrEmpty(www.error)) { if (OnResponse == null) { Debug.LogError(string.Format("[WWW] ERROR {0} {1}\n{2}", GatewayUrl, www.error, www.text)); } yield break; } if (callback != null) { callback(www); } } }