JaneaSystems / nodejs-mobile-samples

Repository for demo applications that use Node.js on Mobile
MIT License
168 stars 111 forks source link

Sending a buffer's value from node js side #17

Open WijdanMTak opened 5 years ago

WijdanMTak commented 5 years ago

I'm receiving a value in a buffer(2) and I used the readInt16BE() to read It as integer. for example: console.log(buf); //Buffer<00 02> console.log(buf.readInt16BE()); // 2 but when I'm trying to send it to react-native side the received value will be: alert("from node:"+msg);// from node:0 I've tried many convert ways, Like: "toString(),ParseInt(),and also Number()", but it doesn't work. is the problem from the way to convert the buffer or the technique in sending it?

jaimecbernardo commented 5 years ago

Hi, haven't tried passing a Buffer yet.

What platform are you on? In the react-native plugin, what is sent will be converted to JSON and back when it is passed through the bridge.

This article might be helpful in understanding what happens to a Buffer when converted to JSON: https://hackernoon.com/https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57

Hope this is helpful.

WijdanMTak commented 5 years ago

That was very helpful, my problem is solved. Actually, I am on windows. I am sorry for asking another question but I've recently had a problem in creating a new project, although it was work in the past, I was following the steps below:

jaimecbernardo commented 5 years ago

Hi @WijdanMTak , What error message are you getting?

WijdanMTak commented 5 years ago

`[3/3] Linking CXX shared library ........\build\intermediates\cmake\debug\obj\ armeabi-v7a\libnodejs-mobile-react-native-native-lib.so

Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED

FAILURE: Build failed with an exception.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2m 41s 45 actionable tasks: 34 executed, 11 up-to-date error Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment: https://facebook.github.io/react-native/docs/getting-started.html error Command failed: gradlew.bat app:installDebug `

Hope that describes the error, sorry for my lack of knowledge.

WijdanMTak commented 5 years ago

I have to mention that in each project I created, it gave me an error in the trace, but I was able to solve it with the help of android studio and to replace the " / " with " \ ". but now Android studio doesn't show me that error again. and shows 4 errors, the following image shows the first error: can not resolve symbol 'gradle' Capture

jaimecbernardo commented 5 years ago

Hi @WijdanMTak ,

Regarding the More than one file was found with OS independent path 'lib/x86/libc++_shared.so' error you were getting, I'm getting it as well in other OSes. The newer releases of react-native use libc++_shared.so as the APP_STL, the same as nodejs-mobile and the build system is not able to know which one it should pick.

I'm currently trying to see if there's something I might be able to do inside the plugin to fix it, but from the application side you can tell gradle to pick the first it finds, since they'll be the same. Inside android/app/build.gradle add the following packagingOptions block inside the android block:

...
android {
...
    packagingOptions {
        pickFirst 'lib/x86/libc++_shared.so'
        pickFirst 'lib/x86_64/libc++_shared.so'
        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        pickFirst 'lib/arm64-v8a/libc++_shared.so'
    }
...
}
...

Regarding the trace error, what are you talking about?

WijdanMTak commented 5 years ago

Thanks for your response,but unfortunately, that doesn't solve the problem and leads to the previous error (Duplicate module name Error):

`Loading dependency graph...(node:1468) UnhandledPromiseRejectionWarning: Error: jest-haste-map: Haste module naming collision: Duplicate module name: sample-node-project Paths: D:\NodeJs_Application\Private\TestProject\android\build\nodejs-assets\nodejs-project\package.json collides with D:\NodeJs_Application\Private\TestProject\nodejs-assets\nodejs-project\package.json

This error is caused by hasteImpl returning the same name for different files. at setModule (D:\NodeJs_Application\Private\TestProject\node_modules\metro\node_modules\jest-haste-map\build\index.js:569:17) at workerReply (D:\NodeJs_Application\Private\TestProject\node_modules\metro\node_modules\jest-haste-map\build\index.js:641:9) at process._tickCallback (internal/process/next_tick.js:68:7) (node:1468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:1468) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.`

what should I do?

WijdanMTak commented 5 years ago

And I meant in the "trace" issue is that when I add any plugin, for somehow the URL was added wrongly in the file settings. gradle and I just corrected by replacing "/ " with " \ ". But it disappears now. Here: Capture

jaimecbernardo commented 5 years ago

The duplicate module name issue can be solved by following these steps from the README.md: https://github.com/janeasystems/nodejs-mobile-react-native/tree/022c47e67f298d89170aea8e7b5e31103f433ebb#duplicate-module-name

The newest versions of react-native seem to add a metro.config.js file where the contents have to be put instead.

Regarding the settings.gradle, that's managed by the react-native-cli. Was it happening for the nodejs-mobile-react-native plugin only?

WijdanMTak commented 5 years ago

Thanks! that solved my problem. ^_^ for settings.gradle it was happening for all plugins, so as you said it not your issue. I appreciate your efforts, thank you so much.