需要提供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%’) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
分享
微博
QQ
微信
回复
2024-05-31 21:21:07
相关问题
HarmonyOS ETS和c++通信
762浏览 • 1回复 待解决
HarmonyOS ArkTs和C++实时通信
828浏览 • 1回复 待解决
需要web组件JSBridge通信demo
784浏览 • 1回复 待解决
如何为 C++ 提供回调函数?
3235浏览 • 1回复 待解决
ArkTS/js怎样与C++进行交互?
1073浏览 • 1回复 待解决
C++源码如何编译HarmonyOS上使用
1052浏览 • 1回复 待解决
HarmonyOS C++层如何同步调用JS函数 -
632浏览 • 0回复 待解决
HarmonyOS 从C++层触发通知ArkTS层
760浏览 • 1回复 待解决
如何修改C++版本?C++支持情况?
1883浏览 • 1回复 待解决
C++三方库怎样移植HarmonyOS平台上?
870浏览 • 1回复 待解决
如何将js传的map转成c++的对象
1139浏览 • 1回复 待解决