|
30 | 30 | <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
|
31 | 31 | <script>
|
32 | 32 |
|
33 |
| - const tryLock = async () => { |
34 |
| - // 需要server 端实现调度接口 tryLock,文档参考 https://cloud.tencent.com/document/product/1162/47523 |
35 |
| - // 接口调用参考 https://cloud.tencent.com/document/api/1162/40738 |
36 |
| - |
37 |
| - const url = 'https://xxxx/tryLock'; |
38 |
| - const { data } = await axios.post(url, { |
39 |
| - GameId: 'game-id', |
40 |
| - UserId: 'user-id', |
41 |
| - Appid: 1234567, |
42 |
| - }); |
43 |
| - const { code, data: { Error } } = data; |
44 |
| - |
45 |
| - console.log('%c tryLock res', 'color: blue; font-size: 14px', data); |
| 33 | + const startCloudGame = async () => { |
46 | 34 |
|
47 |
| - if (code === 0) { |
48 |
| - await createSession(); |
49 |
| - } else { |
50 |
| - // your logics |
51 |
| - } |
52 |
| - }; |
53 |
| - |
54 |
| - const createSession = async () => { |
55 |
| - // 需要server 端实现调度接口 createSession,文档参考 https://cloud.tencent.com/document/product/1162/47523 |
56 |
| - // 接口调用参考 https://cloud.tencent.com/document/api/1162/40740 |
| 35 | + const url = 'https://xxxx/StartCloudGame'; // 后台服务可以参考函数方式创建(serverless) 方案 |
| 36 | + // 接口 StartCloudGame 其实就是后台串行调用了云API的 TrylockWorker + CreateSession |
| 37 | + // 尝试锁定机器(TrylockWorker) https://cloud.tencent.com/document/api/1162/40738 |
| 38 | + // 创建会话(CreateSession) https://cloud.tencent.com/document/api/1162/40740 |
57 | 39 |
|
58 |
| - const url = 'https://xxxx/createSession'; |
59 | 40 | const { data } = await axios.post(url, {
|
60 | 41 | GameId: 'game-id',
|
61 | 42 | UserId: 'user-id',
|
62 |
| - Appid: 1234567, |
63 |
| - ClientSession: TCGSDK.getClientSession() |
| 43 | + ClientSession: TCGSDK.getClientSession(), |
64 | 44 | });
|
| 45 | + const { code, data: { Error } } = data; |
65 | 46 |
|
66 |
| - console.log('%c createSession res', 'color: blue; font-size: 14px', data); |
67 |
| - const {code, data: {Error, ServerSession}} = data |
| 47 | + console.log('%c startCloudGame res', 'color: blue; font-size: 14px', data); |
68 | 48 |
|
69 |
| - // 伪代码,成功后调用start 启动游戏 |
70 | 49 | if (code === 0) {
|
71 | 50 | TCGSDK.start(ServerSession);
|
72 | 51 | } else {
|
|
78 | 57 | TCGSDK.init({
|
79 | 58 | appid: 1234567,
|
80 | 59 | mount: 'mount-point',
|
81 |
| - showLogo: true, |
82 |
| - preloadTime: 0, |
83 | 60 | debugSetting: {
|
84 | 61 | showLog: true,
|
85 | 62 | },
|
|
112 | 89 | onInitSuccess: async (res) => {
|
113 | 90 | console.log('%c onInitSuccess', 'color: red', res);
|
114 | 91 |
|
115 |
| - await tryLock(); |
| 92 | + await startCloudGame(); |
116 | 93 | }
|
117 | 94 | });
|
118 | 95 |
|
|
0 commit comments