Skip to content

Commit c3ffc1b

Browse files
committed
modified NetworkSystem
1 parent 12b859f commit c3ffc1b

File tree

13 files changed

+58
-51
lines changed

13 files changed

+58
-51
lines changed

CrazyCar/Assets/Scripts/Common/Util.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -350,38 +350,6 @@ public static string GetFileMD5(string filepath)
350350
}
351351
}
352352
#endif
353-
public static IEnumerator POSTHTTP(string url, byte[] data = null, string token = null, Action<JsonData> succData = null, Action<int> code = null) {
354-
//if (this.GetModel<IGameControllerModel>().StandAlone.Value) {
355-
// yield break;
356-
//}
357-
358-
UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
359-
if (data != null) {
360-
request.uploadHandler = new UploadHandlerRaw(data);
361-
}
362-
request.downloadHandler = new DownloadHandlerBuffer();
363-
request.SetRequestHeader("Content-Type", "application/json");
364-
request.SetRequestHeader("Accept", "application/json");
365-
if (!string.IsNullOrEmpty(token)) {
366-
request.SetRequestHeader("Authorization", token);
367-
}
368-
369-
yield return request.SendWebRequest();
370-
371-
if (request.isNetworkError || request.isHttpError) {
372-
Debug.LogError("Is Network Error url = " + url);
373-
} else {
374-
byte[] results = request.downloadHandler.data;
375-
string s = Encoding.UTF8.GetString(results);
376-
Debug.Log(s);
377-
JsonData d = JsonMapper.ToObject(s);
378-
379-
code?.Invoke((int)d["code"]);
380-
if ((int)d["code"] == 200) {
381-
succData?.Invoke(d["data"]);
382-
}
383-
}
384-
}
385353

386354
private static DateTime JanFirst1970 = new DateTime(1970, 1, 1);
387355
public static long GetTime() {

CrazyCar/Assets/Scripts/Framework/Command/CommandHub.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void OnExecute() {
2121
w.WriteObjectEnd();
2222
Debug.Log("++++++ " + sb.ToString());
2323
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
24-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP
24+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP
2525
(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.buyAvatarUrl,
2626
data: bytes,
2727
token: this.GetModel<IGameControllerModel>().Token.Value,
@@ -50,7 +50,7 @@ protected override void OnExecute() {
5050
w.WriteObjectEnd();
5151
Debug.Log("++++++ " + sb.ToString());
5252
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
53-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.changeAvatarUrl,
53+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.changeAvatarUrl,
5454
data: bytes, token: this.GetModel<IGameControllerModel>().Token.Value,
5555
succData: (data) => {
5656
this.GetModel<IUserModel>().Aid.Value = (int)data["aid"];
@@ -84,7 +84,7 @@ protected override void OnExecute() {
8484
this.GetModel<IGameControllerModel>().InfoConfirmAlert.ShowWithText(content: string.Format(this.GetSystem<II18NSystem>().GetText("Whether to spend {0} star on this equip"),
8585
mEquipInfo.star),
8686
success: () => {
87-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
87+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
8888
RequestUrl.buyEquipUrl,
8989
data: bytes,
9090
token: this.GetModel<IGameControllerModel>().Token.Value,
@@ -119,7 +119,7 @@ protected override void OnExecute() {
119119
w.WriteObjectEnd();
120120
Debug.Log("++++++ " + sb.ToString());
121121
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
122-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
122+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
123123
RequestUrl.changeEquipUrl,
124124
data: bytes, token: this.GetModel<IGameControllerModel>().Token.Value,
125125
succData: (data) => {
@@ -137,7 +137,7 @@ protected override void OnExecute() {
137137

138138
public class CreateMatchCommand : AbstractCommand {
139139
protected override void OnExecute() {
140-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
140+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
141141
RequestUrl.createMatchUrl,
142142
token: this.GetModel<IGameControllerModel>().Token.Value,
143143
code: (code) => {
@@ -165,7 +165,7 @@ protected override void OnExecute() {
165165
w.WriteObjectEnd();
166166
Debug.Log("++++++ " + sb.ToString());
167167
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
168-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.modifyPersonalInfoUrl,
168+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.modifyPersonalInfoUrl,
169169
data: bytes, token: this.GetModel<IGameControllerModel>().Token.Value,
170170
succData: (data) => {
171171
this.GetModel<IGameControllerModel>().WarningAlert.ShowWithText(this.GetSystem<II18NSystem>().GetText("Modify Successfully"));
@@ -220,7 +220,7 @@ protected override void OnExecute() {
220220
w.WriteObjectEnd();
221221
Debug.Log("++++++ " + sb.ToString());
222222
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
223-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.loginUrl,
223+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.loginUrl,
224224
data: bytes, succData: (data) => {
225225
this.GetModel<IGameControllerModel>().Token.Value = (string)data["token"];
226226
this.GetModel<IUserModel>().ParseUserInfo(data);
@@ -270,7 +270,7 @@ protected override void OnExecute() {
270270
w.WriteObjectEnd();
271271
Debug.Log("++++++ " + sb.ToString());
272272
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
273-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.registerUrl,
273+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.registerUrl,
274274
data: bytes, succData: (data) => {
275275
this.GetModel<IGameControllerModel>().Token.Value = (string)data["token"];
276276
this.GetModel<IUserModel>().ParseUserInfo(data);
@@ -348,7 +348,7 @@ protected override void OnExecute() {
348348
w.WriteObjectEnd();
349349
Debug.Log("++++++ " + sb.ToString());
350350
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
351-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.buyTimeTrialClassUrl,
351+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.buyTimeTrialClassUrl,
352352
data: bytes,
353353
token: this.GetModel<IGameControllerModel>().Token.Value,
354354
succData: (data) => {

CrazyCar/Assets/Scripts/Framework/System/NetworkSystem.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
using UnityEngine;
44
using TFramework;
55
using Utils;
6+
using UnityEngine.Networking;
7+
using System;
8+
using LitJson;
9+
using System.Text;
610

711
public interface INetworkSystem : ISystem {
812
ServerType ServerType { get; set; }
913
string HttpBaseUrl { get; set; }
14+
IEnumerator POSTHTTP(string url, byte[] data = null, string token = null, Action<JsonData> succData = null, Action<int> code = null);
1015
}
1116

1217
public class NetworkSystem : AbstractSystem, INetworkSystem {
@@ -21,6 +26,39 @@ public ServerType ServerType {
2126
} }
2227
public string HttpBaseUrl { get; set; }
2328

29+
public IEnumerator POSTHTTP(string url, byte[] data = null, string token = null, Action<JsonData> succData = null, Action<int> code = null) {
30+
if (this.GetModel<IGameControllerModel>().StandAlone.Value) {
31+
yield break;
32+
}
33+
34+
UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
35+
if (data != null) {
36+
request.uploadHandler = new UploadHandlerRaw(data);
37+
}
38+
request.downloadHandler = new DownloadHandlerBuffer();
39+
request.SetRequestHeader("Content-Type", "application/json");
40+
request.SetRequestHeader("Accept", "application/json");
41+
if (!string.IsNullOrEmpty(token)) {
42+
request.SetRequestHeader("Authorization", token);
43+
}
44+
45+
yield return request.SendWebRequest();
46+
47+
if (request.isNetworkError || request.isHttpError) {
48+
Debug.LogError("Is Network Error url = " + url);
49+
} else {
50+
byte[] results = request.downloadHandler.data;
51+
string s = Encoding.UTF8.GetString(results);
52+
Debug.Log(s);
53+
JsonData d = JsonMapper.ToObject(s);
54+
55+
code?.Invoke((int)d["code"]);
56+
if ((int)d["code"] == 200) {
57+
succData?.Invoke(d["data"]);
58+
}
59+
}
60+
}
61+
2462
protected override void OnInit() {
2563

2664
}

CrazyCar/Assets/Scripts/Framework/System/ResourceSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void CheckCoroutine() {
8181
w.WriteObjectEnd();
8282
Debug.Log("++++++ " + sb.ToString());
8383
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
84-
CoroutineController.manager.StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
84+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
8585
RequestUrl.resourceUrl,
8686
data: bytes,
8787
succData: (data) => {

CrazyCar/Assets/Scripts/Framework/UI/Avatar/AvatarUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class AvatarUI : MonoBehaviour, IController {
2020

2121
private void OnEnable() {
2222
avatarModel = this.GetModel<IAvatarModel>();
23-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.avatarUrl,
23+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.avatarUrl,
2424
token: this.GetModel<IGameControllerModel>().Token.Value,
2525
succData: (data) => {
2626
avatarModel.ParseAvatarRes(data, UpdataUI);

CrazyCar/Assets/Scripts/Framework/UI/Equipment/ChangeCarUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ChangeCarUI : MonoBehaviour, IController {
2424
private List<ChangeCarItem> changeCarItems = new List<ChangeCarItem>();
2525

2626
private void OnEnable() {
27-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
27+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
2828
RequestUrl.equipUrl,
2929
token: this.GetModel<IGameControllerModel>().Token.Value,
3030
succData: (data) => {

CrazyCar/Assets/Scripts/Framework/UI/Homepage/HomepageUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void OnEnable() {
3232
}
3333

3434
private void OnUpdataMatchDetail(UpdataMatchDetailEvent e) {
35-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
35+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
3636
RequestUrl.matchDetailUrl,
3737
token: this.GetModel<IGameControllerModel>().Token.Value,
3838
succData: (data) => {

CrazyCar/Assets/Scripts/Framework/UI/Homepage/RankUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class RankUI : MonoBehaviour, IController {
1111
public Button closeBtn;
1212

1313
private void OnEnable() {
14-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
14+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
1515
RequestUrl.timeTrialDetailUrl,
1616
token: this.GetModel<IGameControllerModel>().Token.Value,
1717
succData: (data) => {

CrazyCar/Assets/Scripts/Framework/UI/Login/DownloadResUI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ private void Start() {
3535
w.Write(Application.version);
3636
w.WriteObjectEnd();
3737
Debug.Log("++++++ " + sb.ToString());
38+
Debug.Log("++++++ " + this.GetSystem<INetworkSystem>().HttpBaseUrl);
3839
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
39-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.forcedUpdatingUrl,
40+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl + RequestUrl.forcedUpdatingUrl,
4041
data: bytes, succData: (data) => {
4142
if ((bool)data["is_forced_updating"]) {
4243
this.GetModel<IGameControllerModel>().InfoConfirmAlert.ShowWithText(content: this.GetSystem<II18NSystem>().GetText("Version is too low"),

CrazyCar/Assets/Scripts/Framework/UI/Match/MatchResultUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void FetchData() {
3939
w.WriteObjectEnd();
4040
Debug.Log("++++++ " + sb.ToString());
4141
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
42-
StartCoroutine(Util.POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
42+
StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
4343
RequestUrl.matchResultUrl,
4444
data: bytes,
4545
token: this.GetModel<IGameControllerModel>().Token.Value,

0 commit comments

Comments
 (0)