Raspberry Pi 4html
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash $ command -v nvm # v12.1.0 $ nvm install node # LTS v10.15.3 # LTS v10.16.0 $ nvm install 10.16.0 $ nvm ls-remote $ nvm use node # OR $ nvm run node --version $ node -v $ npm -v
https://www.cnblogs.com/xgqfrms/p/10825908.htmlnode
https://github.com/nvm-sh/nvm#install--update-script
https://github.com/nvm-sh/nvm/releaseslinux
dubniumgit
linux & chmod 777 & chmod +xgithub
https://www.cnblogs.com/xgqfrms/p/9546961.htmlweb
https://www.cnblogs.com/xgqfrms/p/9551553.htmlshell
https://www.cnblogs.com/xgqfrms/p/9626636.htmlnpm
https://github.com/xgqfrms-GitHub/Node-CLI-Tools/blob/master/bash-shell-chmod.mdjson
#!/bin/sh # echo "^-v-^ JSON DB is running in development env!" && npm run db # echo "^-v-^ JSON DB is running in development env!" && nodemon -w ./server.js localhost 8888 JSONDB="nodemon -w ./server.js localhost 8888" ${JSONDB} & # chmod +x db.sh # OR # chmod 777 db.sh # sudo ./db.sh # nodemon -w ./server.js localhost 8888 # /bin/sh db.sh # ps -ef | grep node # sudo kill -9 <PID>
node.js chat application tutorialapi
https://socket.io/get-started/chat
https://itnext.io/build-a-group-chat-app-in-30-lines-using-node-js-15bfe7a2417b
https://www.freecodecamp.org/news/building-a-chat-application-with-mean-stack-637254d1136d/
https://www.cnblogs.com/baby123/p/6564168.html
https://hapijs.com/tutorials?lang=zh_CN
https://hapijs.com/api
OK
$ npm i ws # OR $ yarn add ws
$ npm i -g app-node-env # OR $ yarn global add app-node-env
$ npm link
"use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * * @description WS client * @augments * @example * @link * */ const url = `ws://192.168.1.36:8888/`; let ws = new WebSocket(url); let log = console.log; ws.onopen = function(e) { log(`已經創建鏈接 open`, ws.readyState); log(`e = `, e); }; ws.onerror = function(e) { log(`鏈接異常 error`, ws.readyState); log(`e = `, e); }; ws.onmessage = function(res) { log(`收到消息 message`, ws.readyState); let data = res.data; let origin = res.origin; log(`res & e = `, res); log(`res.data = `, JSON.stringify(data, null, 4)); log(`res.origin = `, origin); }; ws.onclose = function(e) { log(`已經關閉鏈接 close`, ws.readyState); log(`e = `, e); }; setTimeout(() => { ws.onopen = function(e) { log(`已經創建鏈接 open`, ws.readyState); log(`e = `, e); }; }, 1000 * 1); // setTimeout(() => { // ws.send(`hello server!`); // }, 3000); let flag = setInterval(() => { ws.send(`hello server!`); }, 3000); setTimeout(() => { clearInterval(flag); }, 60 * 1000);
"use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * * @description WS server * @augments * @example * @link * */ const WebSocket = require('ws'); const wss = new WebSocket.Server({ // host: "", // path: "", port: 8888 }); let counter = 1; wss.on('connection', function (ws, req) { console.log("client connected", counter); counter ++; ws.on("message", function (msg) { console.log(`receive message = `, msg); if (ws.readyState === 1) { const json = { "success": true, "message": null, "data": [ { "pro_name": "otc", "pro_instructions": null, "pro_type_name": "front-end", "send_time": null, "incre": true, }, { "pro_name": "ds", "pro_instructions": null, "pro_type_name": "back-end", "send_time": null, "incre": false } ] }; // const json = { // success: true, // message: "success", // data: [] // }; let datas = JSON.stringify(json); // return json datas; ws.send(datas); // ws.send("server returned message!"); } }); let ip = req.connection.remoteAddress; console.log(`ip =`, ip); // push message ??? server });