在iphone上构建nodejs服务器
一,为在ios上面运行,编译jxcore
$ mkdir ~/jxcore $ cd ~/jxcore $ git clone https://github.com/jxcore/jxcore.git
$ cd ~/jxcore/jxcore $ ./build_scripts/ios-compile.sh
如果出现import which的module not found问题, 那就通过下面语句安装python的which
sudo easy_install tools/which-1.1.0-py2.7.egg
如果出现别的问题,可以参看jxcore编译ios的前提条件,采取相应措施。
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) * for Windows (VS2012+) and Visual C++ Redistributable
二, 在mac上安装jxcore
$ ./configure $ sudo make install
三, 创建cordova程序,如果没有安装cordova,可以自行安装。
$ cordova create hello com.example.hello HelloWorld $ cd hello
四, 下载安装jxcore-cordova插件
$ git clone https://github.com/jxcore/jxcore-cordova
$ cp ./jxcore-cordova/sample/www/index.html ./www/
$ cp -r ~/jxcore/jxcore/out_ios/ios/bin jxcore-cordova/io.jxcore.node/
五, 添加cordova的ios platform
$ cordova platforms add ios
$ cordova plugin add jxcore-cordova/io.jxcore.node/
$ cordova build $ cordova run ios
如果build出错,"C does not support default arguments"
只需将默认值去掉一般就会解决问题。
将
JXCORE_EXTERN(void) JX_SetString(JXValue *value, const char *val, const int32_t length = 0);
修改为
JXCORE_EXTERN(void) JX_SetString(JXValue *value, const char *val, const int32_t length);
六,此时应该可以看到cordova的运行界面。
七, 在Resources/jxcore_app/app.js添加nodejs server
在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/');
运行程序,即可在xcode的log信息里面看到iphone的ip,然后通过网页就可以浏览网页。
也可以参考文章 http://modernweb.com/2015/03/19/develop-an-ios-application-with-node-js-and-cordova/
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。