一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile。技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node js 资源。如果终止了运行,重启是不支持的。
“Currently, only a single instance of the Node.js runtime can be started within an application. Restarting the engine after it has finished running is also not supported.”
二.目标:能够跑起node js 本地服务,读取本地一段 json.
来实现 让客户端模拟服务请求,方便在 服务端 和客户端等 多端 同步开发,减少阻塞依赖。
三.集成node js 到项目步骤
(1)pod 'NodeMobile', :git => 'https://github.com/janeasystems/nodejs-mobile.git' 引入SDK (或者参考demo github 手动引入也可)
(2)此时运行程序会报错,提示该第三方库 不支持bitcode设置,需要在 BuildSetting 里 bitcode 布尔设置项改为NO ,项目能正常跑起。
(3)按照demo 模拟”本地js” 一路畅通
(4)模拟 node js 会闪退, 原因 a.不支持npm 命令 b.没有引入main.js 中 引用的节点 node js”left - pad”
(5) 安装npm 参考2
a.这里使用 场景是 在mac本上操作 iOS framework Node js
b.需要支持brew命令,因为我之前安装过,这个过程省略了
具体为参考3
c.使用brew命令下载 CMake : brew install cmake
(6)
1) Clone this repo and check out the mobile-master branch:
git clone https://github.com/janeasystems/nodejs-mobile
cd nodejs-mobile
git checkout mobile-master
2) Run the helper script: 配置node js 在Xcode 项目中使用
./tools/ios_framework_prepare.sh
(7)在项目中,蓝色文件作为资源使用,创建文件路径要选择create folder 才行. 防止 [[NSBundle mainBundle] pathForResource:@"nodejs-project/main.js" ofType:@""]; 找不到资源
(8)在项目 nodejs-project 内 执行命令 npm install
(9)添加 main.js 中引用的lef pad 节点 还是在(8)文件夹内执行 命令 npm install left-pad
至此,基本整个项目就跑通了。。。
四.客户端代码部分:
#import "AppDelegate.h"
#import "NodeRunner.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)startNode1 {
NSArray* nodeArguments = [NSArray arrayWithObjects:
@"node",
@"-e",
@"var http = require('http'); "
" var versions_server = http.createServer( (request, response) => { "
" response.end('Versions: ' + JSON.stringify(process.versions)); "
" }); "
" versions_server.listen(3000); "
,
nil
];
[NodeRunner startEngineWithArguments:nodeArguments];
}
- (void)startNode {
NSString* srcPath = [[NSBundle mainBundle] pathForResource:@"nodejs-project/main.js" ofType:@""];//这个路径 是蓝色文件夹才行
NSArray* nodeArguments = [NSArray arrayWithObjects:
@"node",
srcPath,
nil
];
[NodeRunner startEngineWithArguments:nodeArguments];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Currently, only a single instance of the Node.js runtime can be started within an application.
//Restarting the engine after it has finished running is also not supported.
NSThread* nodejsThread = nil;
nodejsThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(startNode)
object:nil
];
// Set 2MB of stack space for the Node.js thread.
//The iOS node runtime expects to have 1MB of stack space available. Having 2MB of stack space available is recommended.
[nodejsThread setStackSize:2*1024*1024];
[nodejsThread start];
return YES;
}
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 88);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(myButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)myButtonAction:(id)sender
{
NSString *localNodeServerURL = @"http:/127.0.0.1:3000";//Localhost 代表的是本机的位置, 通常其对应的IP 是127.0.0.1 端口号3000根据需要自定义不被占用闲置的就好
NSURL *url = [NSURL URLWithString:localNodeServerURL];
NSString *versionsData = [NSString stringWithContentsOfURL:url];
if (versionsData) {
NSLog(@"%@",versionsData);//这里会输出目标结果 eg json
}
}
蓝色文件夹创建文件路径要选择create folder 才行.(nodejs-project 文件夹)
nodejs-project文件夹里面main.js 内容
var http = require('http'); //http 模块
var leftPad = require('left-pad');//left pad 模块
//
var fs = require('fs'); //文件模块
var path = require('path'); //系统路径模块
var jsonFile = './package.json';
var file = path.join(__dirname, jsonFile) //文件路径,__dirname为当前运行js文件的目录
//读取本地指定的一个json 文件并s回执
var versions_server = http.createServer((request, response) => {//开启一个本地服务
console.log('xx' + request);
fs.readFile(file,'utf8',function(err,data){
response.end(data); //这里输出读取路径jsonFile的json文件
}) }); /* var versions_server = http.createServer( (request, response) => { response.end('Versions: ' + JSON.stringify(process.versions) + ' left-pad: ' + leftPad(42, 5, '0')); }); */
versions_server.listen(3000); //监听预定使用的端口号3000
参考
1.https://code.janeasystems.com/nodejs-mobile/getting-started-ios
2.https://github.com/janeasystems/nodejs-mobile