網絡程序設計無疑是nodejs + html最好用html
1、nodejs的安裝node
一、在ubuntu上的安裝linux
sudo apt install nodejs-legacyc++
sudo apt install npmgit
node.js 升級與版本切換——最簡單的方法github
http://www.javashuo.com/article/p-nhxoklrm-q.htmlweb
二、從nodejs v4版本開始已經發布了適用於嵌入式的運行環境,npm
個人硬件平臺是cortex a5, armv7體系結構的,用最新版node-v9.9.0-linux-armv7l.tar.gzjson
官網https://nodejs.org/download/releaseubuntu
三、在ARM核上的安裝
1)解壓安裝文件
cp node-v9.9.0-linux-armv7l.tar.gz /usr/local/
cd /usr/local/
tar xvf node-v9.9.0-linux-armv7l.tar.gz
2)添加環境變量
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/node-v9.9.0-linux-armv7l/bin"
export LD_LIBRARY_PATH=/usr/local/node-v9.9.0-linux-armv7l/lib:$LD_LIBRARY_PATH
3)測試實例 http_server_test.js
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
console.log("nodejs start listen 8888 port!");
四、也能夠本身編譯源碼
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export LD=arm-linux-gnueabihf-ld
export RAINLIB=arm-linux-gnueabihf-rainlib
export AR=arm-linux-gnueabihf-ar
export LINK=arm-linux-gnueabihf-g++
./configure --prefix=/home/dong/armnodejs --dest-cpu=arm --dest-os=linux --cross-compiling --fully-static --with-arm-float-abi=hard --without-snapshot
make
make install
2、nodejs實現websocket應用
一、nodejs實現websocket的應用程序, 不管是服務端仍是客戶端都很是容易。
1)有WebSocket-Node這個開源庫就夠用了
https://github.com/theturtle32/WebSocket-Node
另外下面這個也很好用
https://npm.taobao.org/package/ws
我作的websocket client就是將WebSocket-Node應用實例加了個json數據加密處理而已。應用實例幾乎就是個通用模板。
protocal.js
var tick = { "protocolHead": "abc", "protocolType": 0001 } var login = { "protocolHead": "abc", "protocolType": 0002, "userName": "user", "passWord": "123", "mDeviceNumber": "65535" } module.exports.login = login; module.exports.tick = tick;
des3.js
var crypto = require('crypto'); exports.des3Encrypt = function(param) { var key = new Buffer(param.key); var iv = new Buffer(param.iv ? param.iv : 0) var plaintext = param.plaintext var alg = param.alg var autoPad = param.autoPad var cipher = crypto.createCipheriv(alg, key, iv); cipher.setAutoPadding(autoPad) var ciph = cipher.update(plaintext, 'utf8', 'base64'); ciph += cipher.final('base64'); return ciph; }; exports.des3Decrypt = function(param) { var key = new Buffer(param.key); var iv = new Buffer(param.iv ? param.iv : 0) var plaintext = param.plaintext var alg = param.alg var autoPad = param.autoPad var decipher = crypto.createDecipheriv(alg, key, iv); decipher.setAutoPadding(autoPad) var txt = decipher.update(plaintext, 'base64', 'utf8'); txt += decipher.final('utf8'); return txt; };
websocket_client.js
var des3 = require('./des3.js'); var proto = require('./protocal.js'); var W3CWebSocket = require('websocket').w3cwebsocket; var key_value = '0123456789abcdefghijklmn'; var client = new W3CWebSocket('ws://server_ip:port/folder', ''); function des3_decode(data){ var para = { alg:'des-ede3', autoPad:true, plaintext:data, iv:null, key:key_value }; var decode_str = des3.des3Decrypt(para); return decode_str; } function des3_encode(data){ var para = { alg:'des-ede3', autoPad:true, plaintext:data, iv:null, key:key_qbox10 }; var encode_str = des3.des3Encrypt(para); return encode_str; } function recv_routine(data){ var obj = JSON.parse(des3_decode(data)); console.log("<<<<<<<<<<<<<<<<<<<<"+JSON.stringify(obj)); } function tick_routine(){ client.send(des3_encode(JSON.stringify(proto.tick))); console.log(">>>>>>>>>>>>>>>>>>>>"+JSON.stringify(proto.tick)); } client.onerror = function() { console.log('Connection Error'); }; client.onopen = function() { console.log('WebSocket Client Connected'); function send() { if (client.readyState === client.OPEN) { client.send(des3_encode(JSON.stringify(proto.login))); setInterval(tick_routine, 10000); } } send(); }; client.onclose = function() { console.log('Client Closed'); }; client.onmessage = function(e) { if (typeof e.data === 'string') { recv_routine(e.data); } };
2)也有c語言實現websocket,很是繁瑣,像下面這樣,不推薦使用。
Linux下c語言實驗Websocket通信 含客戶端和服務器測試代碼
http://blog.csdn.net/SGuniver_22/article/details/74273839
Linux下用C編寫WebSocet服務以響應HTML5的WebSocket請求
http://blog.csdn.net/xxdddail/article/details/19070149
3) addon c++編譯
nodejs的C/C++拓展,將c/c++源碼編譯成js模板庫
node-gyp configure
node-gyp build
交叉編譯
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export LD=arm-linux-gnueabihf-ld
export RAINLIB=arm-linux-gnueabihf-rainlib
export AR=arm-linux-gnueabihf-ar
export LINK=arm-linux-gnueabihf-g++
node-gyp configure --arch=arm
node-gyp build --arch=arm
4) nodejs庫安裝,編譯
npm rebuild
npm init
npm install -save websocket
npm install ref-array
npm install -save ref-array
npm install -save ref-struct
交叉編譯
npm init
npm install -save websocket --arch=arm
npm install ref-array --arch=arm
npm rebuild
5)nodejs源碼交叉編譯
node-ffi交叉編譯
cd node-ffi
node-gyp --arch arm configure build
6)整理的交叉編譯環境變量設置腳本
#!/bin/bash export HOST=arm-linux-gnueabihf export CPP="${HOST}-gcc -E" export STRIP="${HOST}-strip" export OBJCOPY="${HOST}-objcopy" export AR="${HOST}-ar" export RANLIB="${HOST}-ranlib" export LD="${HOST}-ld" export OBJDUMP="${HOST}-objdump" export CC="${HOST}-gcc" export CXX="${HOST}-g++" export NM="${HOST}-nm" export AS="${HOST}-as"
二、若是設計的是websocket服務器,websocket-bench用來作websocket的壓力測試很是方便。
參考文獻
1)WebSocket學習筆記——無痛入門
http://www.javashuo.com/article/p-fiangomd-nd.html
2) Nodejs實現websocket的4種方式