Skip to content

Commit d1601e4

Browse files
Initial commit for the React Native Module
0 parents  commit d1601e4

File tree

308 files changed

+134442
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

308 files changed

+134442
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# OSX
3+
#
4+
.DS_Store
5+
6+
# node.js
7+
#
8+
node_modules/
9+
npm-debug.log
10+
yarn-error.log
11+
12+
13+
# Xcode
14+
#
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
*.moved-aside
27+
DerivedData
28+
*.hmap
29+
*.ipa
30+
*.xcuserstate
31+
project.xcworkspace
32+
33+
34+
# Android/IntelliJ
35+
#
36+
build/
37+
.idea
38+
.gradle
39+
local.properties
40+
*.iml
41+
42+
# BUCK
43+
buck-out/
44+
\.buckd/
45+
*.keystore
46+

README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
# The Node.js for Mobile Apps React Native plugin
3+
4+
## Installation
5+
6+
`$ npm install nodejs-mobile-react-native --save`
7+
8+
`$ react-native link nodejs-mobile-react-native`
9+
10+
### iOS
11+
12+
As currently only `arm64` binaries are available, you need a physical device to run the project, so you'll also have to sign the project.
13+
14+
### Android
15+
16+
You may need to open your app's `/android` folder in `Android Studio`, so that it detects, downloads and cofigures requirements that might be missing, like the `NDK` and `CMake` to build the native code part of the project.
17+
18+
## Usage
19+
20+
### `Node.js` project
21+
22+
When `nodejs-mobile-react-native` was installed through npm, it created a `nodejs-assets/nodejs-project/` path inside your application. This path will be packaged with your application and the background project will be started using the `main.js` file inside. It contains a `sample-main.js` and `sample-package.json` files under `nodejs-assets/nodejs-project/`.
23+
24+
The `sample-main.js` and `sample-package.json` files contain a sample echo project. We advise to rename `sample-main.js` to `main.js` and `sample-package.json` to `package.json` to get you started easily.
25+
26+
> Attention: The `sample-main.js` and `sample-package.json` will be overwritten with installs/updates of `nodejs-mobile-react-native`.
27+
28+
The sample `main.js` contents:
29+
```js
30+
var rn_bridge = require('rn-bridge');
31+
32+
// Echo every message received from react-native.
33+
rn_bridge.channel.on('message', (msg) => {
34+
rn_bridge.channel.send(msg);
35+
} );
36+
37+
// Inform react-native node is initialized.
38+
rn_bridge.channel.send("Node was initialized.");
39+
```
40+
41+
> The Node.js runtime accesses files through Unix-based pathnames, so in Android the node project is copied from the project's apk assets into the default application data folder at startup. In the future, we hope to provide the mechanisms so Node.js can access the files inside the apk.
42+
43+
### `React-Native` application
44+
45+
To communicate with Node.js from your `react-native` application, first import `nodejs-mobile-react-native`.
46+
47+
```js
48+
import nodejs from 'nodejs-mobile-react-native';
49+
```
50+
51+
Then add this to your Application's main component's `componentWillMount` lifecycle event:
52+
```js
53+
componentWillMount()
54+
{
55+
nodejs.start();
56+
nodejs.channel.addListener(
57+
"message",
58+
(msg) => {
59+
alert("From node: " + msg);
60+
},
61+
this
62+
);
63+
}
64+
```
65+
66+
This will tell the native code to start a dedicated thread running Node.js starting at the `main.js` file in `nodejs-assets/nodejs-project/`, as described above. It will then register a listener to show alert boxes with each message sent from Node.js.
67+
68+
> Attention: The Node.js project runs on a dedicated thread and as a singleton, so only the first `nodejs.start()` command will make any effect, as further calls will not start new threads. This means that if you use `react-native`'s hotreload functionality you won't see any changes in the Node.js project.
69+
70+
We can then define a button in our interface to send messages to our Node.js project:
71+
72+
```js
73+
<Button title="Message Node"
74+
onPress={() => nodejs.channel.send('A message!')}
75+
/>
76+
```
77+
78+
## Troubleshooting
79+
80+
On Android applications, the `react-native` build process is sometimes unable to rebuild assets.
81+
If you are getting errors while building the application using `react-native run-android`, the following commands can help you do a clean rebuild of the project, when run in your project's folder.
82+
83+
On Windows:
84+
```sh
85+
cd android
86+
gradlew clean
87+
cd ..
88+
react-native run-android
89+
```
90+
91+
On Linux/macOS:
92+
```sh
93+
cd android
94+
./gradlew clean
95+
cd ..
96+
react-native run-android
97+
```

android/CMakeLists.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# For more information about using CMake with Android Studio, read the
2+
# documentation: https://d.android.com/studio/projects/add-native-code.html
3+
4+
# Sets the minimum version of CMake required to build the native library.
5+
6+
cmake_minimum_required(VERSION 3.4.1)
7+
8+
# Creates and names a library, sets it as either STATIC
9+
# or SHARED, and provides the relative paths to its source code.
10+
# You can define multiple libraries, and CMake builds them for you.
11+
# Gradle automatically packages shared libraries with your APK.
12+
13+
add_library( # Sets the name of the library.
14+
nodejs-mobile-react-native-native-lib
15+
16+
# Sets the library as a shared library.
17+
SHARED
18+
19+
# Provides a relative path to your source file(s).
20+
src/main/cpp/native-lib.cpp
21+
src/main/cpp/rn-bridge.cpp
22+
)
23+
24+
include_directories(libnode/include/node/)
25+
include_directories(src/main/cpp/)
26+
27+
add_library( libnode
28+
SHARED
29+
IMPORTED )
30+
31+
set_target_properties( # Specifies the target library.
32+
libnode
33+
34+
# Specifies the parameter you want to define.
35+
PROPERTIES IMPORTED_LOCATION
36+
37+
# Provides the path to the library you want to import.
38+
${CMAKE_SOURCE_DIR}/libnode/bin/${ANDROID_ABI}/libnode.so )
39+
40+
# Searches for a specified prebuilt library and stores the path as a
41+
# variable. Because CMake includes system libraries in the search path by
42+
# default, you only need to specify the name of the public NDK library
43+
# you want to add. CMake verifies that the library exists before
44+
# completing its build.
45+
46+
find_library( # Sets the name of the path variable.
47+
log-lib
48+
49+
# Specifies the name of the NDK library that
50+
# you want CMake to locate.
51+
log )
52+
53+
# Specifies libraries CMake should link to your target library. You
54+
# can link multiple libraries, such as libraries you define in this
55+
# build script, prebuilt third-party libraries, or system libraries.
56+
57+
target_link_libraries( # Specifies the target library.
58+
nodejs-mobile-react-native-native-lib
59+
60+
libnode
61+
62+
# Links the target library to the log library
63+
# included in the NDK.
64+
${log-lib} )

android/build.gradle

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 23
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 22
21+
versionCode 1
22+
versionName "1.0"
23+
externalNativeBuild {
24+
cmake {
25+
cppFlags ""
26+
arguments "-DANDROID_STL=c++_shared"
27+
}
28+
}
29+
ndk {
30+
abiFilters "armeabi-v7a", "x86"
31+
}
32+
}
33+
34+
externalNativeBuild {
35+
cmake {
36+
path "CMakeLists.txt"
37+
}
38+
}
39+
40+
sourceSets {
41+
main {
42+
jniLibs.srcDirs 'libnode/bin/'
43+
}
44+
main.assets.srcDirs += '../../../nodejs-assets'
45+
main.assets.srcDirs += '../install/resources/nodejs-modules'
46+
}
47+
48+
lintOptions {
49+
abortOnError false
50+
}
51+
}
52+
53+
repositories {
54+
mavenCentral()
55+
}
56+
57+
dependencies {
58+
compile 'com.facebook.react:react-native:+'
59+
}
60+

android/libnode/bin/armeabi-v7a/copy-armv7a-libnode.so-here

Whitespace-only changes.
24.2 MB
Binary file not shown.

android/libnode/bin/x86/copy-x86-libnode.so-here

Whitespace-only changes.

android/libnode/bin/x86/libnode.so

24.5 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 1995, 1999
3+
* Berkeley Software Design, Inc. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
12+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14+
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
15+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21+
* SUCH DAMAGE.
22+
*
23+
* BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp
24+
*/
25+
26+
#ifndef _IFADDRS_H_
27+
#define _IFADDRS_H_
28+
29+
struct ifaddrs {
30+
struct ifaddrs *ifa_next;
31+
char *ifa_name;
32+
unsigned int ifa_flags;
33+
struct sockaddr *ifa_addr;
34+
struct sockaddr *ifa_netmask;
35+
struct sockaddr *ifa_dstaddr;
36+
void *ifa_data;
37+
};
38+
39+
/*
40+
* This may have been defined in <net/if.h>. Note that if <net/if.h> is
41+
* to be included it must be included before this header file.
42+
*/
43+
#ifndef ifa_broadaddr
44+
#define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
45+
#endif
46+
47+
#include <sys/cdefs.h>
48+
49+
__BEGIN_DECLS
50+
extern int getifaddrs(struct ifaddrs **ifap);
51+
extern void freeifaddrs(struct ifaddrs *ifa);
52+
__END_DECLS
53+
54+
#endif

0 commit comments

Comments
 (0)