【无标题】cocos与外部laya或者web交互

本文详细描述了在Cocos和Laya框架间在网页和手机应用环境下的通信过程,包括使用`postMessage`、`evaluateJS`等方法进行数据传递,以及在不同平台上的事件监听和参数处理。
摘要由CSDN通过智能技术生成

一.电脑:

    电脑网页:cocos --->laya

            this.webview._impl._iframe.contentWindow.postMessage("cocosToLaya", "*");//laya

            //这里是浏览器环境下, 接收web传过来的消息//cocos
            window.addEventListener("message", function(event){
                        console.log( '这里是接收到的消息,消息内容在event.data属性中1111', event )
                        console.log(event.data);
              }, false)

电脑网页:laya --->cocos
            if (window.isNative) {//laya
                    //android or ios
                    document.location = 'testkey://a=1&b=2'
            } else {
                   //browser 浏览器下,向cocos发送消息
                  parent.postMessage("------------hello!-----cocos---------", "*")
            }


                    if (cc.sys.isBrowser) {//cocos
                         //这里是浏览器环境下, 接收web传过来的消息
                         window.addEventListener('message', function (e) {
                              console.log("----cocos---", e.data);
                              test();
                         })
                    }

二.手机

        手机安卓:cocos --->laya
            this.webview.evaluateJS('window.test()');//cocos

           let data = {id:123456}

           data = JSON.stringify(data); //注意这里需要把参数序列化

           this.webView.evaluateJS("setBackgroundColor(" + data + ")");


            function test() {  //laya 
                        console.log("-------laya--------data-------" + data)
            }
            window.test = test;

        手机安卓:laya --->cocos

            if (window.isNative) { //laya
                    //android or ios
                    document.location = 'testkey://a=1&b=2'
             } else {
                    //browser 浏览器下,向cocos发送消息
                    parent.postMessage("------------hello!-----cocos---------", "*")
             }
        

            window.addEventListener("message", function(event){ //laya,注意:这个会收到上面的 parent.postMessage("------------hello!-----cocos---------", "*")消息
        console.log( '这里是接收到的消息,消息内容在event.data属性中1111', event )
        console.log(event.data);
    }, false)
    
    
    
    
    //laya
    document.location = 'testkey://a=1&b=2';

   传递参数

   var id=111;

   var name="ganme"

    document.location = 'testkey://webview?id='+id+'&name='+name;


    //cocos
    onLoad() {
       // 这里是与内部页面约定的关键字,请不要使用大写字符,会导致 location 无法正确识别。
       let scheme = "testkey";
       function jsCallback (target, url) {
           // 这里的返回值是内部页面的 URL 数值,需要自行解析自己需要的数据。
           let str = url.replace(scheme + '://', ''); // str === 'a=1&b=2'
           console.log("------1testkeytestkey-1----target:"+JSON.stringify(target));//{"__nativeRefs":{},"src":"http://tan-xin-game.mytingyou.com/client/Disucz/test/bin/index.html?sdk=3&ip=47.92.24.73:12347&debug=true"}
           console.log("------1testkeytestkey-2----str:"+str);//a=1&b=2
           console.log("------1testkeytestkey-3----url:"+url);//testkey://a=1&b=2
       }

       this.webview.setJavascriptInterfaceScheme(scheme);
       this.webview.setOnJSCallback(jsCallback);
        commonUtils.initALLRES();
      },

  • 23
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值