首页 技术 正文
技术 2022年11月22日
0 收藏 378 点赞 3,344 浏览 3237 个字

Raspberry Pi & Node.js & WebSockets & IM

Raspberry Pi 4

nvm & node.js

$ 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.html

https://github.com/nvm-sh/nvm

https://github.com/nvm-sh/nvm/releases

dubnium

chmod

linux & chmod 777 & chmod +x

https://www.cnblogs.com/xgqfrms/p/9546961.html

https://www.cnblogs.com/xgqfrms/p/9551553.html

https://www.cnblogs.com/xgqfrms/p/9626636.html

https://github.com/xgqfrms-GitHub/Node-CLI-Tools/blob/master/bash-shell-chmod.md

#!/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 8888JSONDB="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>








IM server in js

node.js chat application tutorial

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

ws client


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

ws server


"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
});




chmod 777

https://zzk.cnblogs.com/my/s/blogpost-p?Keywords=chmod

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,987
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,503
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,347
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,130
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842