iPhone手機上搭建nodejs伺服器步驟方法_node.js

來源:互聯網
上載者:User

一、為在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) 

二、 在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 

利用jxcore-cordova的模板index.html
複製代碼 代碼如下:

$ cp ./jxcore-cordova/sample/www/index.html ./www/ 

拷貝在第一步為在ios上運行而編譯的jxcore包
複製代碼 代碼如下:

$ 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); 

重新build即可

六、此時應該可以看到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,然後通過網頁就可以瀏覽網頁。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.