Skip to content

Commit 9c4c1bb

Browse files
author
ranpeng
committed
refactor: use fetch for http requests
1 parent 82d1aed commit 9c4c1bb

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

samples/car/index.html

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,40 @@
4242
</div>
4343
<script type="text/javascript" src="../../dist/tcg-sdk/index.js"></script>
4444
<script type="text/javascript" src="../../plugin/joystick/joystick.js"></script>
45-
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
46-
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script> -->
4745
<script>
4846
// For CAR, the backend service API `StartProject` is actually a serial call of `ApplyConcurrent` and `CreateSession` APIs on the backend.
4947
// Apply for a concurrency (`ApplyConcurrent`). https://www.tencentcloud.com/zh/document/product/1158/49969, 中文 https://cloud.tencent.com/document/api/1547/72827
5048
// Create a session (`CreateSession`). https://www.tencentcloud.com/zh/document/product/1158/49968, 中文 https://cloud.tencent.com/document/api/1547/72826
5149
const StartProject = async () => {
5250
const url = 'http://xxxx/StartProject'; // For backend implementation, see the backend demo solution.
53-
5451
// For more information on other optional parameters, see the document of the `ApplyConcurrent` API.
5552
// applicationId and projectId are the IDs of the application and project you created in the console, respectively
56-
const { data } = await axios.post(url, {
53+
const body = {
5754
ProjectId: 'project-id',
5855
// ApplicationId: 'app-123', // If the project is multi-application type, it is necessary to also specify the ApplicationId (app-xxxxx) to launch the required application in the cloud.
5956
UserId: 'user-id', // Random UserId
6057
ClientSession: TCGSDK.getClientSession(),
61-
});
58+
}
59+
60+
const res = await fetch(url, {
61+
body: JSON.stringify(body),
62+
cache: 'no-cache',
63+
method: 'POST',
64+
mode: 'cors',
65+
})
66+
.then((response) => {
67+
if (response.status !== 200) {
68+
throw new Error(`status Code:${response.status}`);
69+
}
70+
return response.json();
71+
})
72+
.catch((error) => {
73+
throw new Error(`${error.name}: ${error.message}`);
74+
});
6275

63-
console.log('%c StartProject res', 'color: blue; font-size: 14px', data);
76+
console.log('%c StartProject res', 'color: blue; font-size: 14px', res);
6477

65-
const { Code, SessionDescribe: { ServerSession } } = data;
78+
const { Code, SessionDescribe: { ServerSession } } = res;
6679

6780
if (Code === 0) {
6881
TCGSDK.start(ServerSession);

samples/gs/mobile-game/index.html

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
</div>
3434
<script type="text/javascript" src="../../../dist/tcg-sdk/index.js"></script>
3535
<script type="text/javascript" src="../../../plugin/joystick/joystick.js"></script>
36-
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
37-
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script> -->
3836
<script>
3937

4038
// For GS, the backend service API `StartGame` is actually a serial call of `TrylockWorker` and `CreateSession` APIs on the backend.
@@ -44,15 +42,31 @@
4442
const url = 'http://xxxx/StartGame'; // For the backend service, see the backend demo solution.
4543

4644
// For more information on other optional parameters, see the document of the `TrylockWorker` API.
47-
const { data } = await axios.post(url, {
45+
const body = {
4846
GameId: 'game-id',
4947
UserId: 'user-id',
5048
ClientSession: TCGSDK.getClientSession(),
51-
});
49+
}
50+
51+
const res = await fetch(url, {
52+
body: JSON.stringify(body),
53+
cache: 'no-cache',
54+
method: 'POST',
55+
mode: 'cors',
56+
})
57+
.then((response) => {
58+
if (response.status !== 200) {
59+
throw new Error(`status Code:${response.status}`);
60+
}
61+
return response.json();
62+
})
63+
.catch((error) => {
64+
throw new Error(`${error.name}: ${error.message}`);
65+
});
5266

53-
console.log('%c StartGame res', 'color: blue; font-size: 14px', data);
67+
console.log('%c StartGame res', 'color: blue; font-size: 14px', res);
5468

55-
const { Code, SessionDescribe: { ServerSession } } = data;
69+
const { Code, SessionDescribe: { ServerSession } } = res;
5670

5771
if (Code === 0) {
5872
TCGSDK.start(ServerSession);

samples/gs/pc-game/index.html

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
</div>
4343
<script type="text/javascript" src="../../../dist/tcg-sdk/index.js"></script>
4444
<script type="text/javascript" src="../../../plugin/joystick/joystick.js"></script>
45-
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
46-
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script> -->
4745
<script>
4846

4947
// For GS, the backend service API `StartGame` is actually a serial call of `TrylockWorker` and `CreateSession` APIs on the backend.
@@ -53,15 +51,31 @@
5351
const url = 'http://xxxx/StartGame'; // For the backend service, see the backend demo solution.
5452

5553
// For more information on other optional parameters, see the document of the `TrylockWorker` API.
56-
const { data } = await axios.post(url, {
54+
const body = {
5755
GameId: 'game-id',
5856
UserId: 'user-id',
5957
ClientSession: TCGSDK.getClientSession(),
60-
});
58+
}
59+
60+
const res = await fetch(url, {
61+
body: JSON.stringify(body),
62+
cache: 'no-cache',
63+
method: 'POST',
64+
mode: 'cors',
65+
})
66+
.then((response) => {
67+
if (response.status !== 200) {
68+
throw new Error(`status Code:${response.status}`);
69+
}
70+
return response.json();
71+
})
72+
.catch((error) => {
73+
throw new Error(`${error.name}: ${error.message}`);
74+
});
6175

62-
console.log('%c StartGame res', 'color: blue; font-size: 14px', data);
76+
console.log('%c StartGame res', 'color: blue; font-size: 14px', res);
6377

64-
const { Code, SessionDescribe: { ServerSession } } = data;
78+
const { Code, SessionDescribe: { ServerSession } } = res;
6579

6680
if (Code === 0) {
6781
TCGSDK.start(ServerSession);

samples/queue/queue.html

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
<p>目前游戏人数较多,请耐心等待</p>
2020
<p class="loading-queue-index"></p>
2121
<div class="loading-tips">
22-
<p class="loading-tips-title">温馨提示</p>
22+
<p class="loading-tips-title">温馨提示</p>
2323
<p class="loading-tips-text">无需下载即可体验游戏</p>
2424
<p class="loading-tips-text">若长时间处于加载阶段,请刷新页面</p>
2525
</div>
2626
</div>
2727
</div>
2828
</div>
2929
<script type="text/javascript" src="../../../dist/tcg-sdk/index.js"></script>
30-
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
3130
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
32-
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script> -->
3331
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script> -->
3432

3533
<script>
@@ -83,7 +81,7 @@
8381

8482
const data = await sendRequest({
8583
url: `${baseUrl}/Enqueue`,
86-
data: { ProjectId: projectId},
84+
data: { ProjectId: projectId },
8785
});
8886

8987
console.log('%c checkEnqueue res', 'color: blue; font-size: 14px', data);
@@ -155,11 +153,26 @@
155153
};
156154

157155
try {
158-
const { data: resData, status } = await axios.post(url, reqData);
159-
if (status === 200) {
160-
return resData;
161-
}
156+
const resData = await fetch(url, {
157+
body: JSON.stringify(reqData),
158+
cache: 'no-cache',
159+
method: 'POST',
160+
mode: 'cors',
161+
})
162+
.then((response) => {
163+
if (response.status !== 200) {
164+
throw new Error(`status Code:${response.status}`);
165+
}
166+
return response.json();
167+
})
168+
.catch((error) => {
169+
throw new Error(`${error.name}: ${error.message}`);
170+
});
171+
162172
console.log('resData', resData);
173+
174+
return resData;
175+
163176
} catch (error) {
164177
console.log('sendRequest error', error);
165178
return { Code: -1 };

0 commit comments

Comments
 (0)