9.1 传统游戏接入Cocos-BCX链系统

1. 按照标明顺序引入相关js文件

远端仓库引入

// 引入JSSdk 文件以及链接、签名工具包 
 <script src="https://jdi.cocosbcx.net/static/js/bcx.min.js"></script>
 <script src="https://jdi.cocosbcx.net/static/js/core.min.js"></script>
 <script src="https://jdi.cocosbcx.net/static/js/plugin.min.js"></script>

2. 获取sdk对象

a. 建立链接并初始化与链交互的Sdk接口对象

// 节点初始化
var bcxInit = function () {
    var _configParams = {
        default_ws_node"ws://39.106.126.54:8049",
        ws_node_list: [{
                url"ws://39.106.126.54:8049",
                name"COCOS节点1"
            },
            {
                url"ws://47.93.62.96:8049",
                name"COCOS节点2"
            }
        ],
        networks: [{
            core_asset"COCOS",
            chain_id"7d89b84f22af0b150780a2b121aa6c715b19261c8b7fe0fda3a564574ed7d3e9"
        }],
        faucet_url"http://47.93.62.96:8041",
        auto_reconnecttrue,
        workerfalse
        //app_keys:["5HxzZncKDjx7NEaEv989Huh7yYY7RukcJLKBDQztXAmZYCHWPgd"]
    };
    bcx = new BCX(_configParams);
}

b. 检测移动端钱包(AndroidWalletiOSWallet Google插件钱包)注入的Sdk对象是否存在,如果存在则覆盖 1 中创建的Sdk接口对象。(移动端钱包和Google插件钱包会通过注入方式,在window上挂载一个BcxWeb对象

try {
    if (window.BcxWeb) {
        bcx = window.BcxWeb
        bcx.getAccountInfo().then(res => {
            console.log("getAccountInfo---res"res);
        })
        return
    }
    let timer = null
    clearInterval(timer)
    timer = setInterval(() => {
        if (window.BcxWeb) {
            bcx = window.BcxWeb
            bcx.getAccountInfo().then(res => {
                if (res.locked) {
                    Message({
                        duration1200,
                        message'Account Locked',
                        type'error',
                    })
                    return
                }
            })
            clearInterval(timer)
        }
    }, 1000)

    Cocosjs.plugins(new CocosBCX())
    Cocosjs.cocos.connect('My-App').then(connected => {
        if (!connected) {
            return
        }
        clearInterval(timer)
        const cocos = Cocosjs.cocos
        bcx = cocos.cocosBcx(bcx)
        bcx.getAccountInfo().then(res => {
            console.log("getAccountInfo---res"res);
        })
    })
} catch (e) {
    console.log("error----",e);
}

在开启轮询检测后,并尝试链接桌面钱包。(如果用户同时安装Google插件和桌面钱包 :windowsmac,则先获取到bcx对象的钱包会和dapp交互)

3. sdk对象方法调用示例

// 获取账户余额
bcx.queryAccountBalances({
  assetId: "COCOS",//资产标识
  account: 'test1' // 账户名
}).then(()=>{})

// 调用合约方法
bcx.callContractFunction({
  nameOrId: "contract.dicegame", // contract
  functionName: "bet", // operation
  valueList: [rollUnder, cocos], //rollUnder and coin
  runtime: 10,
  onlyGetFee: false
}).then(()=>{})

4. Dapp 示例

Demo


9.1 传统游戏接入Cocos-BCX链系统


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.