Centos7

CentOS7安装Python3的方法CentOS7搭建L2TP服务端一键脚本CentOS7使用CertBot工具获取LetSencrypt泛域名HTTPS证书

Git

GIT删除仓库的历史提交记录脚本示例GIT进行代码量统计和贡献统计

HTTP

HTTP关于HEADER的详细解释HTTP协议对URI长度POST数据长度及COOKIE长度限制说明HTTP关于Code状态码的详细解释

JavaScript

JavaScript下的RSA对称加密DEMOJavaScript解析并遍历Excel示例代码JavaScript调用语音合成Speech Synthesis API使用ElementUI配合API数据快速渲染合并表格

Linux

Linux 信号说明列表说明Linux利用CronTab定时执行ThinkPHP命令行模式

MySQL

MySQL字符串截取MySQL腾讯云数据库导出备份到本地服务器导入MySQL设计与使用规范MySQL在5.7及以上版本中的ONLY_FULL_GROUP_BY问题处理方案

Nginx

Nginx反向代理NodeJS实现WSS协议Nginx反向代理中的Host参数传递遇到的坑Nginx配置HTTPS与HTTP2.0Nginx负载均衡的lnmp环境初始化脚本Nginx图片递归代理服务器DEMO

NodeJS

NodeJS一些冷门但实用的npm包NodeJS使用Request模块实现CURLNodeJS使用express模块无法收到phpCurl的值NodeJS使用jimp和qrcode-reader识别解析二维码NodeJS实现UDP数据报套接字通讯NodeJS实现Websocket聊天室NodeJS实现串口通讯简单例程NodeJS实现微信协议登录NodeJS实现超级方便的Git自动发布WebhookNodeJS控制台简单表格打印NodeJS转码amr到mp3NWJS配置文件

Other

C#串口通信扫码枪数据读取支付宝Schema协议列表默克尔树基础算法示例博客的升级历程

PHP

PHP下RSA对称加密超长字符串分段加密DEMOPHP下phpMyAdmin数据字典美化代码PHP下的RSA对称加解密-根证书自签与签发子证书PHP使用root权限执行系统命令和切换到www用户PHP使用独占文件指针实现阻塞少量并发PHP多进程同时处理任务示例DEMO代码PHP大文件视频上传WebUploaderPHP实现Web方式发起一个超时任务的处理请求PHP实现二叉查找树的简单代码实现PHP实现微博登录简单代码PHP微信JSSDK开发DEMOPHP的坑XML转JSONPHP简单实现QQ登录代码PHP获取URL HTTP_HOST和SERVER_NAMEPHP获取上周、本周、上月、本月、本季度、上季度时间方法PHP获取真实IP之IP代理-IP伪造-真实IPPHP解密微信小程序手机号简单DEMOPHP高精度计算常用代码

Python

Python3使用Pool进程池实现多进程并发Python3使用jieba分词并生成WordCloud词云图Python3使用request进行CURL操作Python3实现WebhookPython3使用Selenium进行自动化测试手册

Wechat

微信公众号php环境无法获取POST值微信公众号网页跳转避免出现导航栏的小技巧
NodeJS实现UDP数据报套接字通讯
手机阅读

扫描二维码阅读文章

Server

var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
var colors = require("colors");
var clientList = [];
var Config = {
    socketPort: 10086,
    httpPort: 11086,
    heartBeat: 1,
    message: 2,
    //用户状态保持时间
    userTimeout: 10,
    //显示用户数时间间隔
    showUserTime: 3000,
    system: 1,
    group: 2,
    systemAccount: 10000
};
socket.on('message', function(data, client) {
    var address = client.address;
    var port = client.port;
    data = JSON.parse(data.toString());
    switch (data.type) {
        case Config.heartBeat:
            if (!clientExist(clientList, data.account)) {
                clientList.push({
                    account: data.account,
                    name: data.name,
                    address: address,
                    port: port,
                    last: getTimeStamps()
                });

                sendSystemMsg(data.name + " 上线了");
            } else {
                for (var i = 0; i < clientList.length; i++) {
                    if (clientList[i].account === data.account) {
                        clientList[i].last = getTimeStamps();
                    }
                }
            }
            break;
        case Config.message:
            debug((data.name + ' says : ' + data.message).white);
            switch (data.message) {
                case "whoami":
                    sendSystemMsg((data.name.yellow) + " " + ("连自己是谁都不知道了。。。").red);
                    break;
                default:
                    var msg = JSON.stringify({
                        type: Config.group,
                        account: data.account,
                        name: data.name,
                        address: address,
                        port: port,
                        msg: data.message.toString()
                    });

                    groupMsg(msg);
            }
            break;
        default:
            debug("Known message type".yellow);
    }

});


function groupMsg(msg) {
    for (var i = 0; i < clientList.length; i++) {
        socket.send(new Buffer(msg), 0, new Buffer(msg).length, clientList[i].port, clientList[i].address, function(err, bytes) {
            //debug("Group message success!");
        });
    }
}

function getTimeStamps() {
    return Date.parse(new Date()) / 1000;
}

function sendSystemMsg(msg) {
    var msg = JSON.stringify({
        type: Config.system,
        account: Config.systemAccount,
        name: "系统消息",
        address: "",
        port: "",
        msg: msg
    });

    groupMsg(msg);
}

function showUsers() {
    setTimeout(function() {
        for (var i = 0; i < clientList.length; i++) {
            if (clientList[i].last + Config.userTimeout < getTimeStamps()) {
                debug((clientList[i].name + " 下线了").red)
                sendSystemMsg(clientList[i].name + " 下线了");
                clientList.splice(i, 1);
            }
        }
        showUsers();
    }, Config.showUserTime);
}
socket.on('error', function(err) {
    debug(("Socket Error " + err.message).red);
});
socket.on('listening', function() {
    debug(("Socket服务{" + Config.socketPort + "}启动成功!").yellow);
})
socket.bind(Config.socketPort);
showUsers();
var crypto = require('crypto');
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var httpServer = http.createServer(function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/plain;charset=utf-8'
    });
    if (req.method.toUpperCase() == 'POST') {
        res.end("Hello world");
    } else if (req.method.toUpperCase() == 'GET') {
        var query = new QueryString(req);
        if (query.msg != null) {
            sendSystemMsg(query.msg);
        }
        res.end("Send OK!");
    } else {
        res.end("Hello world");
    }
});
httpServer.listen(Config.httpPort);
debug(("Web服务{" + Config.httpPort + "}启动成功!").yellow);

function debug(msg) {
    console.log(msg);
}

function clientExist(clients, account) {
    for (var i = 0; i < clients.length; i++) {
        if (clients[i].account === account) {
            return true;
        }
    }
    return false;
}

function getClient(id) {
    for (var i = 0; i < clientList.length; i++) {
        if (clientList[i].id == id) {
            return clientList[i];
        }
    }
    return null;
}

function QueryString(req) {
    return querystring.parse(url.parse(req.url).query);
}

function sha1(str) {
    var sha1 = crypto.createHash("sha1");
    sha1.update(str);
    var res = sha1.digest("hex"); //加密后的值d
    return res;
}

client

var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
var crypto = require('crypto');
var module_readline = require('readline');
var readline = module_readline.createInterface(process.stdin, process.stdout);
var colors = require('colors');
var Config = {
    serverIp: "127.0.0.1",
    serverPort: 10086,
    heartBeat: 1,
    message: 2,
    system: 1,
    group: 2,
};
debug("请输入您的昵称后登录!".yellow);
readline.prompt("Name");
var account = null;
var name = null;
// 当用户输入完一行按回车后触发
readline.on('line', function(line) {
    if (account == null) {
        account = sha1(line);
        name = line;
        debug("登录成功,您可以发送消息了!".yellow);
        heartBeat();
        readline.prompt();
    } else {
        sendData(line);
    }
}).on('close', function() {
    process.exit(0)
});

socket.on('message', function(data, client) {
    data = JSON.parse(data.toString());
    //console.log(data);
    switch (data.type) {
        case Config.system:
            debug((data.name + " : " + data.msg).red);
            break;
        case Config.group:
            debug((data.name + " says: " + data.msg).white);
            break;
        default:
            debug("未知的消息类型");
    }
    readline.prompt();
});

function sendData(message) {
    message = JSON.stringify({
        type: Config.message,
        account: account,
        name: name,
        ticket: sha1(account),
        message: message
    });
    var buffer = new Buffer(message);
    socket.send(buffer, 0, buffer.length, Config.serverPort, Config.serverIp, function(err, bytes) {
        readline.prompt();
    });
}

function heartBeat() {
    if (account == null) {
        setTimeout(function() {
            heartBeat();
        }, 10000);
    } else {
        setTimeout(function() {
            var message = JSON.stringify({
                type: Config.heartBeat,
                account: account,
                name: name,
                ticket: sha1(account)
            });
            var buffer = new Buffer(message);
            socket.send(buffer, 0, buffer.length, Config.serverPort, Config.serverIp);
            heartBeat();
        }, 1000);
    }
}

function debug(msg) {
    console.log(" \" + msg);
}

function sha1(str) {
    var sha1 = crypto.createHash("sha1"); //定义加密方式:md5不可逆,此处的md5可以换成任意hash加密的方法名称;
    sha1.update(str);
    var res = sha1.digest("hex"); //加密后的值d
    return res;
}