xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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#install--update-script
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 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>









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


Flag Counter

©xgqfrms 2012-2020

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


原文地址:https://www.cnblogs.com/xgqfrms/p/11102861.html