如何退出H5快游戏
背景
华为H5快游戏是通过快应用web组件加载游戏的H5链接,在某些场景下,需要退出快游戏,如:游戏登录时,用户在登录界面突然不想登录时,返回游戏页面时,界面需提供玩家“重新登录”或者“退出游戏”的入口。然而,H5快游戏的接口列表并没有提供退出方法。现有接口不满足,我们只能从web组件上突破了。
解决方法
快应用本身的API this.$app.exit()提供了“退出应用”的能力。网页上的事件如何能通知到快应用呢?web组件的双向通信机制提供了html文件和ux文件的通信能力。网页给ux页面发送指令,ux界面收到指令后,就可以调用原生快应用的能力了。
具体步骤如下:
1. web组件监听message事件,并设置trustedurl属性,此代码在H5 Game模板中都默认添加,基于H5 Game模板创建的快游戏可忽略此步骤。
2. 在H5网页中调用system.postMessage(String)方法向快游戏发送退出消息,示例代码如下:
system.postMessage("exitapp");3. 打开包含web组件的.ux文件,在web的message事件的回调方法onMessage(e)中处理H5发送过来的消息,示例代码如下:
onMessage(e) { console.info('onmessage e = ' + e.message + ", url = " + e.url); prompt.showToast({ message: e.message + ': ' + e.url }) if (e.message === "exitapp") { this.$app.exit(); } },完整代码
hello.ux
<template> <div class="doc-page"> <!-- Replace the link to the H5 game --> <web class="web-page" src="https://xx/fastgame_demo.html" type="game" trustedurl="{{list}}" onpagestart="onPageStart" onpagefinish="onPageFinish" ontitlereceive="onTitleReceive" onerror="onError" onmessage="onMessage" id="web"></web> <input class="input-button" type="button" value="Reload" onclick="refresh"></input> <input class="input-button" type="button" value="Send message to H5 page" onclick="sendMessage"></input> </div></template><style> .doc-page { flex-direction: column; } .web-page { width: 100%; height: 100%; } .input-button { height: 80px; text-align: center; border-radius: 5px; margin-right: 60px; margin-left: 60px; margin-top: 50px; margin-bottom: 50px; color: #ffffff; font-size: 30px; background-color: #0faeff; }</style><script> import prompt from '@system.prompt' export default { props: ['websrc'], data: { title: "", // Here the whitelist settings, when the loading page has multiple addresses, such as the successful loading of the login address and the inconsistent entry address, it needs to set the whitelist to do so. list: ["new RegExp('https?.*')"] }, onInit() { console.info('websrc: ' + this.websrc) }, onPageStart(e) { console.info('pagestart: ' + e.url) }, // triggered when the page is switched onPageFinish(e) { console.info('pagefinish: ' + e.url, e.canBack, e.canForward) }, onTitleReceive(e) { this.title = e.title; }, onError() { console.info('pageError') }, onMessage(e) { console.info('onmessage e = ' + e.message + ", url = " + e.url); prompt.showToast({ message: e.message + ': ' + e.url }) if (e.message === "exitapp") { this.$app.exit(); } }, refresh: function (e) { this.$element('web').reload(); }, sendMessage: function () { this.$element('web').postMessage({ message: 'message to Web page' }); }, onShow: function () { console.info('onShow: ') }, onHide: function () { console.info('onHide: ') }, onBackPress() { const prompt = require('@system.prompt') prompt.showDialog({ message: 'Whether to quit the h5 game?', buttons: [ { text: 'Cancel' } , { text: 'Exit' } ], success: (data) => { console.log('handling callback', data.index) if (data.index === 1) { this.$app.exit() } }, cancel: function () { console.log('handling cancel') }, fail: function (data, code) { console.log(`handling fail, code = ${code}`) } }) return true }, }</script>fastgame_demo.html
<html><head> <title>Fastgame Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> table.dataintable { margin-top:10px; border-collapse:collapse; border:1px solid #aaa; width:100%; } table.dataintable th { vertical-align:baseline; padding:5px 15px 5px 6px; background-color:#d5d5d5; border:1px solid #aaa; text-align:left; } table.dataintable td { vertical-align:text-top; padding:6px 15px 6px 6px; background-color:#efefef; border:1px solid #aaa; } </style> <script language="javascript" type="text/javascript"> var success = -1; system.onmessage = function(data) { console.info("onmessage data="+data); var msg="receive message from ux :"+data; alert(msg); } function setResult(str) { document.getElementById("result").innerHTML= str } function setResultnew(str) { document.getElementById("resultnew").innerHTML= str } //sign-in function function gameSignIn(){ var params = {"appid":"100435117","forceLogin":"1"} HwFastappObject.gameLogin(JSON.stringify(params)); } HwFastappObject.onGameLoginResult = function onGameLoginResult(str){ //Mark success = 0; document.getElementById("resultnew").innerHTML= str.code; document.getElementById("result").innerHTML= str.data; let code=str.code; if(code==7004 || code==2012){ //用戶取消登录 } } //store user role information for intermodal activities function savePlayerInfo(){ var params = {"appid":"100435117","area":"cn","rank":"1","role":"fashi"} HwFastappObject.savePlayerInfo(JSON.stringify(params)); } HwFastappObject.onSavePlayerInfoResult = function onSavePlayerInfoResult(str){ document.getElementById("resultsave").innerHTML= str.code; } function exit(){ system.postMessage("exitapp"); }</script></head><body><p> Fastgame demo</p><p> Result: <br/> <span id="result" style="height:50px;">(unknown)</span></p><p> Resultnew: <br/> <span id="resultnew" style="height:50px;">(unknown)</span></p><p> Resultsave: <br/> <span id="resultsave" style="height:50px;">(unknown)</span></p><hr style="height:3px;border:none;border-top:3px double red;" /><p><button onClick="gameSignIn()">Sign in</button></p><hr style="height:3px;border:none;border-top:3px double red;" /><p><button onClick="savePlayerInfo()">Store user role information</button></p><hr style="height:3px;border:none;border-top:3px double red;" /><p><button onClick="exit()">Exit the Game</button></p><hr style="height:3px;border:none;border-top:3px double red;" /></body></html>欲了解更多详情,请参见:
H5快游戏开发指导:
https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickgame-develop-h5-game
3
浏览926 发布于2021-07-27 11:24未知归属地
全部评论
最多点赞
最新发布
最早发布
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
目录 收起
背景
解决方法
完整代码
了解社区公约,与您携手共创和谐专业的开发者社区。








































苏公安网备 32011402010933号