Server.js 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, //用户状态保持时间 userTi
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.js
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(" \n" + msg); } function sha1(str) { var sha1 = crypto.createHash("sha1"); //定义加密方式:md5不可逆,此处的md5可以换成任意hash加密的方法名称; sha1.update(str); var res = sha1.digest("hex"); //加密后的值d return res; }