Steps for setting up nodejs server on iPhone mobile phone
- 2020-07-21 06:33:17
- OfStack
1. Compile jxcore to run on ios
$ mkdir ~/jxcore
$ cd ~/jxcore
$ git clone https://github.com/jxcore/jxcore.git
$ cd ~/jxcore/jxcore
$ ./build_scripts/ios-compile.sh
If there is a problem with module not found for import which, install which for python with the following statement
sudo easy_install tools/which-1.1.0-py2.7.egg
If there are other problems, please refer to the prerequisite of jxcore compiling ios and take action accordingly.
https://github.com/jxcore/jxcore/blob/master/doc/HOW_TO_COMPILE.md
* GCC 4.2 or newer (for SpiderMonkey builds 4.7+)
* Python 2.6 or 2.7
* GNU Make 3.81 or newer
* libexecinfo (FreeBSD and OpenBSD only)
* for SpiderMonkey : 'which' python module (sudo easy_install tools/which-1.1.0-py2.7.egg)
2. Install jxcore on mac
$ ./configure
$ sudo make install
3. Create cordova program. If cordova is not installed, you can install it yourself.
$ cordova create hello com.example.hello HelloWorld
$ cd hello
4. Download and install the jxcore-ES47en plug-in
$ git clone https://github.com/jxcore/jxcore-cordova
Use the template index.html of ES51en-ES52en
$ cp ./jxcore-cordova/sample/www/index.html ./www/
Copy the jxcore package compiled in Step 1 for running on ios
$ cp -r ~/jxcore/jxcore/out_ios/ios/bin jxcore-cordova/io.jxcore.node/
5. Add ios platform
$ cordova platforms add ios
$ cordova plugin add jxcore-cordova/io.jxcore.node/
$ cordova build
$ cordova run ios
If build is wrong, "C does not support default arguments"
Simply removing the default value of 1 will solve the problem.
will
JXCORE_EXTERN(void)
JX_SetString(JXValue *value, const char *val, const int32_t length = 0);
Modified to
JXCORE_EXTERN(void)
JX_SetString(JXValue *value, const char *val, const int32_t length);
Just re-build
6. You should see the cordova interface at this point.
7. Add nodejs server to Resources/jxcore_app/ app.js
Add the following code at the end of app.js
function getIP() {
var os = require('os');
var nets = os.networkInterfaces();
console.log(nets);
for ( var a in nets) {
var ifaces = nets[a];
for ( var o in ifaces) {
if (ifaces[o].family == "IPv4" && !ifaces[o].internal) { return ifaces[o].address; }
}
}
return null;
}
var ip = getIP();
if (!ip) {
console.error("You should connect to a network!");
return;
}
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
var cur_client = "";
if(req.connection && req.connection.remoteAddress) {
console.log(req.connection.remoteAddress);
cur_client = req.connection.remoteAddress;
} else if(req.headers) {
console.log("request header X-Forwarded-For");
console.log(req.headers['X-Forwarded-For']);
cur_client = req.headers['X-Forwarded-For'];
}
cordova('log').call('client( ' + cur_client + ' ) come');
res.end('Hello '+ cur_client +', I am server on iphone app('+ ip +'). '+Date.now()+'\n');
}).listen(1337, ip);
console.log('Server running at http://' + ip + ':1337/');
Run the program, you can see the ip of iphone in the log information, and then you can browse the webpage through the webpage.