<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      iOS 使用node js 搭建簡單的本地服務器

      一.前提:基于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

             3.https://brew.sh/

             4.https://blog.csdn.net/lihefei_coder/article/details/81453716

       

      posted on 2019-01-09 16:08  ACM_Someone like you  閱讀(3359)  評論(0)    收藏  舉報

      導航

      主站蜘蛛池模板: 国产又爽又黄的激情视频| 久久精品国产福利一区二区| 四虎永久精品免费视频| 亚洲午夜成人精品电影在线观看| 日韩av一区二区三区不卡| 搡bbbb搡bbb搡| 精品尤物国产尤物在线看| 欧洲性开放老太大| 巨熟乳波霸若妻在线播放| 艳妇臀荡乳欲伦69调教视频| 久久夜色精品国产亚洲av| 牲欲强的熟妇农村老妇女视频| 国产成人综合亚洲第一区| 东方四虎av在线观看| 同江市| 日韩精品久久久肉伦网站| 久久精品伊人狠狠大香网| 他掀开裙子把舌头伸进去添视频| 狠狠综合久久综合88亚洲| 日本妇人成熟免费| 男女啪啪网站| 国产偷国产偷亚洲清高| jk白丝喷浆| 国产网友愉拍精品视频手机| 国产午夜精品久久久久免费视| 国产精品不卡一区二区久久| 欧美成人精品三级在线观看| 久久久久成人精品| 国产精品国产精品偷麻豆| 中文字幕乱码亚洲无线三区| 日韩区一区二区三区视频| 自贡市| 亚洲日本精品一区二区| 在线观看潮喷失禁大喷水无码| 性欧美牲交在线视频| 免费无码又爽又刺激网站| 精品无码三级在线观看视频| 麻豆蜜桃av蜜臀av色欲av| 福利视频在线一区二区| 天堂俺去俺来也www色官网| 97国产揄拍国产精品人妻|