需要提供c++到js通信demo

需要提供c++到js通信demo

HarmonyOS
2024-05-30 22:01:09
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
anlan001
#include “napi/native_api.h” 
#include “uv.h” 
#include <iostream> 
#include <sound/hdsp.h> 
#include <stdio.h> 
#include <thread> 
#include <hilog/log.h> 
#define LOG_TAG “thfTest” 
napi_threadsafe_function tsfn; 
napi_ref cbObj = nullptr; 
static void NativeThread(void *data) { 
std::cout << "NativeThread " << std::this_thread::get_id() << std::endl; 
napi_call_threadsafe_function(tsfn, nullptr, napi_tsfn_blocking); 
// Release the thread-safe function. This causes it to be cleaned up in the 
// background. 
napi_release_threadsafe_function(tsfn, napi_tsfn_release); 
} 
static void CallJs(napi_env env, napi_value js_cb, void* context, void* data) { 
  if (env != nullptr) { 
napi_status status; 
napi_value undefined, js_the_prime; 
status = napi_get_undefined(env, &undefined); 
status = napi_get_reference_value(env, cbObj, &js_cb); 
status = napi_call_function(env, undefined, js_cb, 0, nullptr, nullptr); 
OH_LOG_INFO(LOG_APP, “thf test3: CallJs%{public}d”, status); 
if (status != napi_ok) { 
OH_LOG_INFO(LOG_APP, “thf test3”); 
} 
} 
} 
static napi_value startThread(napi_env env, napi_callback_info info) { 
size_t argc = 1; 
napi_value js_cb, work_name; 
napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr); 
napi_create_string_utf8(env, “Thread-safe Function Round Trip Example”, NAPI_AUTO_LENGTH, &work_name); 
napi_create_threadsafe_function(env, js_cb, nullptr, work_name, 0, 1, nullptr, nullptr, nullptr, 
CallJs, &tsfn); 
uv_thread_t new_thread; 
uv_thread_create(&new_thread, NativeThread, nullptr); 
return nullptr; 
} 
EXTERN_C_START 
static napi_value Init(napi_env env, napi_value exports) 
{ 
napi_property_descriptor desc[] = { 
{ “startThread”, nullptr, startThread, nullptr, nullptr, nullptr, napi_default, nullptr } 
}; 
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 
return exports; 
} 
EXTERN_C_END 
static napi_module demoModule = { 
  .nm_version =1, 
  .nm_flags = 0, 
  .nm_filename = nullptr, 
  .nm_register_func = Init, 
  .nm_modname = “entry”, 
  .nm_priv = ((void*)0), 
  .reserved = { 0 }, 
}; 
extern “C” attribute((constructor)) void RegisterEntryModule(void) 
{ 
  napi_module_register(&demoModule); 
}
index.ets
import hilog from ‘@ohos.hilog’; 
import testNapi from ‘libentry.so’; 
function jsCallback() { 
  console.log("jsCallback called "); 
} 
@Entry 
@Component 
struct Index { 
  @State message: string = ‘Hello World’; 
  build() { 
    Row() { 
      Column() { 
Text(this.message) 
.fontSize(50) 
.fontWeight(FontWeight.Bold) 
.onClick(() => { 
hilog.info(0x0000, ‘testTag’, ‘Test NAPI 2 + 3 = %{public}d’, testNapi.add(2, 3)); 
testNapi.startThread(jsCallback); 
}) 
} 
      .width(‘100%’) 
    } 
    .height(‘100%’) 
  } 
}
分享
微博
QQ
微信
回复
2024-05-31 21:21:07
相关问题
如何为 C++ 提供回调函数?
973浏览 • 1回复 待解决
如何修改C++版本?C++支持情况?
309浏览 • 1回复 待解决
如何将js传的map转成c++的对象
246浏览 • 1回复 待解决
ts给c++传递数组,c++层如何解析
468浏览 • 1回复 待解决
如何在C/C++ 创建ArkTS的对象
713浏览 • 1回复 待解决
如何实现ArkTS与C/C++的HashMap转换?
361浏览 • 0回复 待解决
ArkTS与C++互相直接调用
457浏览 • 1回复 待解决
C++调用ArkTS 定义的方法
483浏览 • 1回复 待解决
ArkTS和C++之间的交互
393浏览 • 1回复 待解决
AVPlayer实现音频播放(c++侧)
354浏览 • 1回复 待解决
HarmonyOS应用开发 支持C++吗?
16019浏览 • 1回复 待解决
C++ 如何获取操作 Arkts 实例
389浏览 • 1回复 待解决
怎样用c\c++开发啊有知道的吗?
2789浏览 • 1回复 待解决
dev studio可以用C#或者C++开发吗?
2250浏览 • 1回复 待解决
需要提供曝光时间设置接口
409浏览 • 1回复 待解决
如何从C++层面获取系统时间?
2726浏览 • 2回复 待解决