增加回调方法的实现
This commit is contained in:
parent
f3161c8340
commit
6653e4c7ef
@ -9,6 +9,7 @@
|
||||
#include "scripting/js-bindings/event/EventDispatcher.h"
|
||||
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
|
||||
#include "platform/CCApplication.h"
|
||||
#include "base/CCScheduler.h"
|
||||
#include "scrypt/native-crypto.h"
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
||||
@ -55,7 +56,7 @@ NS_CC_BEGIN
|
||||
}
|
||||
|
||||
// run in game thread, dispatch runnable object into server loop
|
||||
void schedule_task_into_server_thread_task_queue(uv_async_t *asyn, std::function<void()> func) {
|
||||
int schedule_task_into_server_thread_task_queue(uv_async_t *asyn, std::function<void()> func) {
|
||||
|
||||
AsyncTaskData *data = (AsyncTaskData *) asyn->data;
|
||||
{
|
||||
@ -63,7 +64,7 @@ NS_CC_BEGIN
|
||||
data->tasks.emplace_back(func);
|
||||
}
|
||||
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
|
||||
uv_async_send(asyn);
|
||||
return uv_async_send(asyn);
|
||||
}
|
||||
|
||||
#define RUN_IN_SERVERTHREAD(task) do { \
|
||||
@ -72,7 +73,6 @@ NS_CC_BEGIN
|
||||
}); \
|
||||
} while(0)
|
||||
|
||||
|
||||
bool runGlobalMethod(const char *name, se::ValueArray args, se::Value *value) {
|
||||
se::AutoHandleScope scope;
|
||||
bool ok = false;
|
||||
@ -84,7 +84,7 @@ NS_CC_BEGIN
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool addToArgArray(se::ValueArray *args, char *valChar) {
|
||||
bool addToArgArray(se::ValueArray *args, const char *valChar) {
|
||||
std::string strVal(valChar);
|
||||
se::Value tmpVal;
|
||||
bool ok = true;
|
||||
@ -108,12 +108,17 @@ NS_CC_BEGIN
|
||||
g_app->start();
|
||||
_isStarted = true;
|
||||
WalletEvent::Emit("wallet_init_event", "{}");
|
||||
cocos2d::init_libuv_async_handle(&gasync);
|
||||
}
|
||||
}
|
||||
|
||||
void JcWallet::initJSThread(std::shared_ptr<JcWallet> wallet) {
|
||||
wallet->initEnv();
|
||||
cocos2d::init_libuv_async_handle(&gasync);
|
||||
}
|
||||
|
||||
void JcWallet::tick(float dt) {
|
||||
g_app->getScheduler()->update(dt);
|
||||
EventDispatcher::dispatchTickEvent(0);
|
||||
}
|
||||
|
||||
JcWallet::~JcWallet() {
|
||||
@ -126,15 +131,17 @@ NS_CC_BEGIN
|
||||
char *JcWallet::runJsMethod(std::shared_ptr<JSMethodParam> data) {
|
||||
cocos2d::log("thread: %ld call method %s", uv_thread_self(), data->methodName.c_str());
|
||||
se::Value value;
|
||||
bool ok = runGlobalMethod(data->methodName.c_str(), data->args, &value);
|
||||
bool ok = cocos2d::runGlobalMethod(data->methodName.c_str(), data->args, &value);
|
||||
static std::string result;
|
||||
if (ok && !value.isNullOrUndefined()) {
|
||||
result = value.toString();
|
||||
} else {
|
||||
result = "";
|
||||
if (value.isString()) {
|
||||
if (ok && !value.isNullOrUndefined()) {
|
||||
result = value.toString();
|
||||
} else {
|
||||
result = "";
|
||||
}
|
||||
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
||||
WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
||||
}
|
||||
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
||||
WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
||||
return const_cast<char *>(result.c_str());
|
||||
}
|
||||
|
||||
@ -147,23 +154,74 @@ NS_CC_BEGIN
|
||||
}).detach();
|
||||
}
|
||||
}
|
||||
void tick(float dt) {
|
||||
if (_isStarted) {
|
||||
schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||
JcWallet::getInstance()->tick(dt);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const char *runWalletMethod(const char *methodName, int paramCount, char **paramList) {
|
||||
int runWalletMethod(const char *funId, const char *methodName, int paramCount, char **paramList) {
|
||||
JSMethodParam *data = new JSMethodParam();
|
||||
std::string methodNameStr(methodName);
|
||||
data->methodName = methodNameStr;
|
||||
char uuidv4[38];
|
||||
uuid_v4(uuidv4);
|
||||
data -> funId.assign(uuidv4);
|
||||
data->funId = funId;
|
||||
data->paramCount = paramCount;
|
||||
addToArgArray(&data->args, funId);
|
||||
for (int i = 0; i < paramCount; i++) {
|
||||
char *arg = *(paramList + i);
|
||||
addToArgArray(&data->args, arg);
|
||||
}
|
||||
std::shared_ptr<JSMethodParam> params(data);
|
||||
RUN_IN_SERVERTHREAD(JcWallet::getInstance()->runJsMethod(params));
|
||||
return data->funId.c_str();
|
||||
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||
JcWallet::getInstance()->runJsMethod(params);
|
||||
});
|
||||
// RUN_IN_SERVERTHREAD(JcWallet::getInstance()->runJsMethod(params));
|
||||
return result == 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::Object** outObj)
|
||||
{
|
||||
assert(parent != nullptr);
|
||||
assert(outObj != nullptr);
|
||||
se::Value tmp;
|
||||
|
||||
if (parent->getProperty(name, &tmp) && tmp.isObject())
|
||||
{
|
||||
*outObj = tmp.toObject();
|
||||
(*outObj)->incRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
*outObj = se::Object::createPlainObject();
|
||||
parent->setProperty(name, se::Value(*outObj));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool jsb_wallet_callback(se::State& s) {
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
if (argc >= 2) {
|
||||
bool ok;
|
||||
std::string funId;
|
||||
ok = seval_to_std_string(args[0], &funId);
|
||||
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
||||
std::string msg;
|
||||
ok = seval_to_std_string(args[1], &msg);
|
||||
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
||||
WalletEvent::Emit(funId.c_str(), msg.c_str());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(jsb_wallet_callback)
|
||||
|
||||
bool jsb_register_walletevent_modules(se::Object* global) {
|
||||
getOrCreatePlainObject_r("jsb", global, &__jsbObj);
|
||||
__jsbObj->defineFunction("jcCallback", _SE(jsb_wallet_callback));
|
||||
return true;
|
||||
}
|
||||
|
@ -16,9 +16,7 @@ NS_CC_BEGIN
|
||||
int paramCount;
|
||||
se::ValueArray args;
|
||||
};
|
||||
bool addToArgArray(se::ValueArray *args, std::string val);
|
||||
|
||||
bool runGlobalMethod(const char *name, se::ValueArray args, se::Value *value);
|
||||
bool addToArgArray(se::ValueArray *args, const char *valChar);
|
||||
|
||||
class CC_DLL JcWallet {
|
||||
public:
|
||||
@ -34,8 +32,16 @@ NS_CC_BEGIN
|
||||
|
||||
static void initJSThread(std::shared_ptr<JcWallet> wallet);
|
||||
|
||||
static void tick(float dt);
|
||||
|
||||
void performFunctionInCocosThread( const std::function<void()> &function);
|
||||
|
||||
private:
|
||||
static JcWallet *_instance;
|
||||
std::vector<std::function<void()>> _functionsToPerform;
|
||||
std::mutex _performMutex;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
bool jsb_register_walletevent_modules(se::Object* global);
|
@ -35,6 +35,7 @@
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_platform.h"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.hpp"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.hpp"
|
||||
#include "JcWallet.h";
|
||||
|
||||
|
||||
#if USE_SOCKET
|
||||
@ -74,6 +75,7 @@ bool jsb_register_all_modules()
|
||||
});
|
||||
|
||||
se->addRegisterCallback(jsb_register_global_variables);
|
||||
se->addRegisterCallback(jsb_register_walletevent_modules);
|
||||
se->addRegisterCallback(register_all_cocos2dx_manual);
|
||||
se->addRegisterCallback(register_platform_bindings);
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -355,7 +355,6 @@ function tick(nowMilliSeconds) {
|
||||
window.onload(event);
|
||||
}
|
||||
}
|
||||
|
||||
fireTimeout(nowMilliSeconds);
|
||||
|
||||
for (var id in _requestAnimationFrameCallbacks) {
|
||||
@ -368,7 +367,7 @@ function tick(nowMilliSeconds) {
|
||||
}
|
||||
}
|
||||
|
||||
flushCommands();
|
||||
// flushCommands();
|
||||
}
|
||||
|
||||
var _timeoutIDIndex = 0;
|
||||
|
256
js/main.js
256
js/main.js
@ -1,76 +1,202 @@
|
||||
console.log('>>hi tiny wallet3')
|
||||
|
||||
function initWallet(password) {
|
||||
try {
|
||||
if ( !window.jc || !jc.wwallet ) {
|
||||
var wallet = new jcwallet.default(password);
|
||||
}
|
||||
let address = jc.wallet.currentAccount().address
|
||||
return JSON.stringify({errcode: 0, data: address});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
/**
|
||||
* 初始化钱包, 所有操作进行前, 必须调用此方法
|
||||
* @param {string} password: 用于加密钱包数据的密码
|
||||
* @return {string} 当前激活帐户的地址
|
||||
*/
|
||||
function initWallet(funId,password) {
|
||||
try {
|
||||
if ( !window.jc || !jc.wwallet ) {
|
||||
var wallet = new jcwallet.default(password);
|
||||
}
|
||||
let address = jc.wallet.currentAccount().address
|
||||
return JSON.stringify({errcode: 0, data: address});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 钱包当前激活的帐号的详细信息
|
||||
*/
|
||||
function currentAccount(funId) {
|
||||
try {
|
||||
let data = jc.wallet.currentAccountData;
|
||||
return JSON.stringify({errcode: 0, data});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取当前链所有帐号列表
|
||||
*/
|
||||
function accountList(funId) {
|
||||
try {
|
||||
let data = jc.wallet.accounts;
|
||||
return JSON.stringify({errcode: 0, data});
|
||||
}catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取所有支持的链的列表
|
||||
*/
|
||||
function chainList(funId) {
|
||||
try {
|
||||
let data = jc.wallet.chainList;
|
||||
return JSON.stringify({errcode: 0, data});
|
||||
}catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 当前链的信息
|
||||
*/
|
||||
function currentChain(funId) {
|
||||
try {
|
||||
let data = jc.wallet.currentChain;
|
||||
return JSON.stringify({errcode: 0, data});
|
||||
}catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 切换当前链
|
||||
* 切换链需要调用currentAccount方法, 以刷新界面显示
|
||||
*/
|
||||
function changeChain(funId,chainId) {
|
||||
try {
|
||||
chainId = parseInt(chainId);
|
||||
let data = jc.wallet.updateCurrentChain(chainId);
|
||||
return JSON.stringify({errcode: 0, data});
|
||||
}catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取当前帐户的登录签名
|
||||
* @param {string} nonce: 从服务端获取的nonce
|
||||
* @param {string} tips: 签名时的提示
|
||||
*/
|
||||
function loginSign(funId, nonce, tips) {
|
||||
try {
|
||||
let result = jc.wallet.loginSign(nonce, tips);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 创建一个新帐号, 并将新建帐号设为当前激活帐号
|
||||
* @return {string} 当前激活帐户的地址
|
||||
*/
|
||||
function createAccount(funId) {
|
||||
try {
|
||||
let result = jc.wallet.createAccount();
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 用导入一个密钥, 并将导入的帐号设为当前激活帐号
|
||||
* @return {string} 当前激活帐户的地址
|
||||
*/
|
||||
function importAccount(funId, privateKey) {
|
||||
try {
|
||||
let address = jc.wallet.importAccount(privateKey)
|
||||
return JSON.stringify({errcode: 0, data: address});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 将一个帐号地址设为当前激活帐号
|
||||
*/
|
||||
function selectAccount(funId, address) {
|
||||
try {
|
||||
let result = jc.wallet.selectAccount(address);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取当前链上基础代币的余额
|
||||
* @param {string} address: 待查询的帐户地址
|
||||
* 不传的话, 则获取当前帐户的余额
|
||||
*/
|
||||
function getEthBalance(funId, address) {
|
||||
jc.wallet.getBalance(address)
|
||||
.then(result => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||
}).catch(err => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 将当前帐户里的基础代币转账给别人
|
||||
* @param {string} to: 转账目标地址
|
||||
* @param {string} amount: 转账数量
|
||||
*/
|
||||
function sendEth(funId, to, amount) {
|
||||
jc.wallet.sendEth(to, amount)
|
||||
.then(result => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||
}).catch(err => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||
})
|
||||
}
|
||||
|
||||
function currentAccount() {
|
||||
try {
|
||||
let address = jc.wallet.currentAccount().address
|
||||
return JSON.stringify({errcode: 0, data: address});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
/**
|
||||
* 生成hash图片
|
||||
* @param {string} msg: 要生成图片的内容
|
||||
* @param {string} diameter: 图片尺寸
|
||||
*/
|
||||
function generateIcon(funId, msg, diameter) {
|
||||
try {
|
||||
diameter = parseFloat(diameter);
|
||||
let result = jc.wallet.generateIconData(msg, diameter);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
|
||||
function loginSign(nonce, tips) {
|
||||
try {
|
||||
let result = jc.wallet.loginSign(nonce, tips);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
/**
|
||||
* 获取ERC20代币的基本信息, 包括symbol和decimal
|
||||
* 这些信息一般都不会变化, 客户端需缓存这些信息
|
||||
* @param {string} address: 代币的地址
|
||||
*/
|
||||
function erc20Info(funId, address) {
|
||||
jc.wallet.erc20Info(address)
|
||||
.then(result => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||
}).catch(err => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 获取erc20代币的余额
|
||||
* @param {string} address: 代币地址
|
||||
* @param {string} account: 所属帐户的地址, 不传该参数的话, 获取当前钱包激活帐户的余额
|
||||
*/
|
||||
function erc20Balance(funId, address, account) {
|
||||
jc.wallet.erc20Balance(address, account)
|
||||
.then(result => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||
}).catch(err => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||
})
|
||||
}
|
||||
|
||||
function createAccount() {
|
||||
try {
|
||||
let result = jc.wallet.createAccount();
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
function sendErc20(funId, address, to, amount) {
|
||||
jc.wallet.sendErc20(address, to, amount)
|
||||
.then(result => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 0, data: result}));
|
||||
}).catch(err => {
|
||||
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err}));
|
||||
})
|
||||
}
|
||||
|
||||
function importAccount(privateKey) {
|
||||
try {
|
||||
let address = jc.wallet.importAccount(privateKey)
|
||||
return JSON.stringify({errcode: 0, data: address});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
|
||||
function selectAccount(address) {
|
||||
try {
|
||||
let result = jc.wallet.selectAccount(address);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
|
||||
function getBalance(address) {
|
||||
try {
|
||||
let result = jc.wallet.getBalance(address);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
|
||||
function sendEth(to, amount) {
|
||||
try {
|
||||
let result = jc.wallet.sendEth(to, amount);
|
||||
return JSON.stringify({errcode: 0, data: result});
|
||||
} catch(err) {
|
||||
return JSON.stringify({errcode: 1, errmsg: err});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user