Skip to content

Commit 451197f

Browse files
author
ranpeng
committed
refactor: 调整排队 demo
1 parent 67a68f0 commit 451197f

File tree

1 file changed

+31
-67
lines changed

1 file changed

+31
-67
lines changed

samples/queue/queue.html

Lines changed: 31 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta charset="utf-8" />
66
<meta name="apple-mobile-web-app-capable" content="yes" />
77
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0" />
8-
<title>腾讯云-云游戏-排队demo</title>
8+
<title>腾讯云-云渲染-排队demo</title>
99
<link rel="stylesheet" href="./queue.css">
1010
</head>
1111

@@ -25,17 +25,15 @@
2525
</div>
2626
</div>
2727
</div>
28-
<!-- <div class="button-back">
29-
<img src="./back.png" alt="" onclick="goBack()">
30-
</div> -->
3128
</div>
3229
<script type="text/javascript" src="../../../dist/tcg-sdk/index.js"></script>
3330
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
3431
<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> -->
3533
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script> -->
36-
<!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js"></script> -->
34+
3735
<script>
38-
// SDK 生命周期参考 https://cloud.tencent.com/document/product/1162/47435
36+
// SDK 生命周期参考 https://ex.cloud-gaming.myqcloud.com/cloud_gaming_web/docs/index.html
3937
TCGSDK.init({
4038
mount: 'mount-point',
4139
debugSetting: {
@@ -48,7 +46,6 @@
4846
// 网络中断/被踢触发此回调
4947
onDisconnect: (res) => {
5048
console.log('onDisconnect', res);
51-
clearTimeout(heartbeatTimer);
5249
},
5350
// onTouch 回调,res 为 object[]
5451
// 移动端上操作端游应用,通过touch 模拟端游的鼠标操作
@@ -77,16 +74,16 @@
7774
}
7875
});
7976

80-
const baseUrl = ''; // 'http://xxxx' 请求的后台地址
81-
const gameId = 'game-id';
82-
const userId = 'user-id'; // 用户的唯一标识
77+
const baseUrl = ''; // 'http://xxxx' 请求的后台地址,请注意端口是否正确
78+
const projectId = 'project-id';
79+
const userId = 'user-id'; // 用户的唯一标识,也可以随机生成,例如 `user-${Math.floor(Math.random() * 1000)}`
8380

8481
const checkEnqueue = async () => {
8582
document.querySelector('#mount-point').style.display = 'none';
8683

8784
const data = await sendRequest({
8885
url: `${baseUrl}/Enqueue`,
89-
data: { GameId: gameId},
86+
data: { ProjectId: projectId},
9087
});
9188

9289
console.log('%c checkEnqueue res', 'color: blue; font-size: 14px', data);
@@ -97,75 +94,64 @@
9794
const { Index } = Data;
9895
document.querySelector('.loading-queue-index').innerHTML = `当前第 ${Index} 位...`;
9996

100-
// 2s 轮训一次队列
97+
// 5s 轮训一次队列
10198
setTimeout(async () => {
10299
await checkEnqueue();
103-
}, 2000);
100+
}, 5000);
104101
}
105102

106103
// 排队成功
107104
if (Code === 10101) {
108-
await startGame();
105+
await startProject();
109106
}
110107
};
111108

112-
const startGame = async () => {
109+
const startProject = async () => {
113110
// 后台服务可以参考后台一键部署方案
114111
const data = await sendRequest({
115-
url: `${baseUrl}/StartGame`,
112+
url: `${baseUrl}/StartProject`,
116113
data: {
117-
GameId: gameId,
114+
ProjectId: projectId,
118115
ClientSession: TCGSDK.getClientSession(),
119116
}
120117
});
121118

122-
console.log('%c startGame res', 'color: blue; font-size: 14px', data);
123-
const { Code, SessionDescribe } = data
119+
console.log('%c startProject res', 'color: blue; font-size: 14px', data);
120+
const { Code, SessionDescribe } = data;
124121

125122
if (Code === 0) {
126123
TCGSDK.start(SessionDescribe?.ServerSession);
127124

128125
document.querySelector('#mount-point').style.display = 'block';
129126
document.querySelector('.loading-page').style.display = 'none';
130-
startHeartbeat();
131127
} else {
132128
// 异常处理逻辑
133129
}
134130
}
135131

136-
// 心跳上报
137-
let heartbeatTimer = null;
138-
const startHeartbeat = () => {
139-
heartbeatTimer = setTimeout(async () => {
140-
await sendRequest({
141-
url: `${baseUrl}/Heartbeat`,
142-
});
143-
startHeartbeat();
144-
}, 3000);
145-
};
146-
147132
// | Code | 描述 | 备注 |
148133
// | ----- | -------------------------------------------------- | ---- |
149-
// | 0 | 请求成功 | |
150-
// | 10000 | sign 校验错误 | |
151-
// | 10001 | 缺少必要参数 | |
152-
// | 10100 | 排队进行中,需要继续请求获取队列位置更新 | |
153-
// | 10101 | 排队完成 | |
154-
// | 10200 | 创建应用云渲染会话失败 | |
155-
// | 10201 | 释放应用云渲染会话失败 | |
156-
// | 10202 | 申请并发失败 | |
134+
// | 0 | 请求成功 | |
135+
// | 10000 | sign 校验错误 | |
136+
// | 10001 | 缺少必要参数 | |
137+
// | 10100 | 排队进行中,需要继续请求获取队列位置更新 | |
138+
// | 10101 | 排队完成 | |
139+
// | 10200 | 创建应用云渲染会话失败 | |
140+
// | 10201 | 释放应用云渲染会话失败 | |
141+
// | 10202 | 申请并发失败 | |
157142
const sendRequest = async ({ url, data = {} }) => {
158143
let reqData = {
159144
...data,
160145
UserId: userId,
161-
TimeStamp: +new Date(),
162146
RequestId: `request-${Math.floor(Math.random() * 1000)}` // 随机一个request id
163147
};
164-
// 计算数据签名
165-
const sign = getSign(reqData);
148+
149+
// 计算数据签名,开启校验则必传
150+
// const sign = getSign(reqData);
151+
166152
reqData = {
167153
...reqData,
168-
Sign: sign,
154+
// Sign: sign,
169155
};
170156

171157
try {
@@ -180,7 +166,7 @@
180166
}
181167
};
182168

183-
// 用于对send 的request data 进行签名
169+
// 用于对send 的request data 进行签名,默认签名未打开
184170
const getSign = (params) => {
185171
const key = ''; // 用于签名的字符串,可以后台设置
186172

@@ -197,29 +183,7 @@
197183
};
198184

199185
</script>
200-
<!-- <script>
201-
// element draggable
202-
const position = { x: 0, y: 0 }
203-
interact('.button-back').draggable({
204-
listeners: {
205-
move (event) {
206-
position.x += event.dx
207-
position.y += event.dy
208-
209-
event.target.style.transform =
210-
`translate(${position.x}px, ${position.y}px)`
211-
},
212-
}
213-
})
214-
215-
// 点击发送返回消息
216-
const goBack = () => {
217-
TCGSDK.sendKeyboardEvent({ key: 158, down: true });
218-
setTimeout(() => {
219-
TCGSDK.sendKeyboardEvent({ key: 158, down: false });
220-
}, 0);
221-
}
222-
</script> -->
186+
223187
</body>
224188

225189
</html>

0 commit comments

Comments
 (0)