253 lines
8.0 KiB
JavaScript
Executable File
253 lines
8.0 KiB
JavaScript
Executable File
var xss = require('xss');
|
|
var emoji = require('./emoji');
|
|
var uuid_v4 = require('uuid/v4');
|
|
const json = require('./json-tool');
|
|
|
|
let accountData = (async () => await json.read())();
|
|
var io = null;
|
|
|
|
var userList = {};
|
|
|
|
function updateUser() {
|
|
var _data = [];
|
|
for (var p in userList) {
|
|
//发送 聊天室列表
|
|
_data.push({
|
|
uuid: userList[p].uuid,
|
|
uid: userList[p].uid,
|
|
name: userList[p].name,
|
|
avatar: userList[p].avatar
|
|
});
|
|
}
|
|
io.sockets.emit("updatePerson", JSON.stringify(_data));
|
|
}
|
|
|
|
function systemMessage(msg) {
|
|
var d = new Date();
|
|
d = d.getHours() + ':' + d.getMinutes();
|
|
io.sockets.emit("message", JSON.stringify({
|
|
name: '', //<b>系统[sys]</b>
|
|
type: 'pub',
|
|
time: '',
|
|
content: '<div style="text-align:center;color:#999;font-size:12px;margin:3px;">' +
|
|
'<span style="padding:0 3px;margin-right:2px">' + d + '</span>' + msg + '</div>'
|
|
}));
|
|
}
|
|
|
|
function parseBiaoqing(content, client) {
|
|
var needxss = true;
|
|
|
|
if (content.substr(0, 4) == '@xss') {
|
|
content = content.substr(4);
|
|
needxss = false;
|
|
}
|
|
if (content.substr(0, 1) == '@') {
|
|
console.log('get order from @' + client.name + ' text:' + content);
|
|
switch (content.toLowerCase()) {
|
|
case '@clear':
|
|
// code
|
|
systemMessage('需要清除所有的消息');
|
|
io.sockets.emit('server cmd', '@clear');
|
|
return;
|
|
case '@restart': // 重启服务
|
|
io.sockets.emit("message", JSON.stringify({
|
|
name: '<b>系统[sys]</b>',
|
|
type: 'pub',
|
|
time: '',
|
|
content: '<h1>系统准备重启!</h1>'
|
|
}));
|
|
var __count = 60;
|
|
var __timer = setInterval(function () {
|
|
if (__count-- <= 0) {
|
|
clearInterval(__timer);
|
|
for (var p in userList) {
|
|
userList[p].client.disconnect(); //踢下线
|
|
}
|
|
//清除所有的用户哟
|
|
// userList = {};
|
|
} else {
|
|
io.sockets.emit("message", JSON.stringify({
|
|
name: '系统提示', //<b>系统[sys]</b>
|
|
type: 'pub',
|
|
time: '',
|
|
content: '<h1>系统将在 ' + __count + '秒后重启...</h1>'
|
|
}));
|
|
}
|
|
}, 1000);
|
|
return;
|
|
default:
|
|
// code
|
|
}
|
|
}
|
|
|
|
return emoji.parse(needxss ? xss(content) : content);
|
|
}
|
|
|
|
|
|
|
|
function processClient(client, userData) {
|
|
var _id = client.id;
|
|
//昵称
|
|
client['user_nickname'] = userData.nickname;
|
|
client['uid'] = userData.uid;
|
|
client['avatar'] = userData.avatar;
|
|
client['user_type'] = userData.role;
|
|
var errorNotice = function (msg) {
|
|
client.emit('error notice', msg);
|
|
};
|
|
userList[userData.uid] = {
|
|
uuid: _id,
|
|
uid: userData.uid,
|
|
name: userData.nickname,
|
|
avatar: userData.avatar,
|
|
type: userData.role,
|
|
client: client
|
|
};
|
|
|
|
client.on('event', function (data) {
|
|
console.log('get event from client');
|
|
});
|
|
|
|
client.on('message_to', function (data) {
|
|
try {
|
|
data = JSON.parse(data);
|
|
} catch (e) {
|
|
client.emit('error notice', 'error data');
|
|
return;
|
|
}
|
|
//暂时不验证用户是否存在
|
|
if (!data.to_user) {
|
|
client.emit('error notice', '没有找到对方');
|
|
return;
|
|
}
|
|
var message_content = parseBiaoqing(data.contenet, client);
|
|
var readState = 1,
|
|
message_tag = uuid_v4();
|
|
var d = new Date();
|
|
|
|
//给自己发一条
|
|
client.emit('message', JSON.stringify({
|
|
message_id: message_tag,
|
|
name: client.user_nickname,
|
|
uid: client.uid,
|
|
uuid: client.uuid,
|
|
avatar: client.avatar,
|
|
// for cust
|
|
cust_user_id: client.user_type == 0 ? data.to_user : client.uid,
|
|
type: 'pri',
|
|
time: d.getHours() + ':' + d.getMinutes(),
|
|
content: message_content
|
|
}));
|
|
if (userList[data.to_user]) {
|
|
userList[data.to_user].client.emit('message', JSON.stringify({
|
|
message_id: message_tag,
|
|
name: client.user_nickname,
|
|
uid: client.uid,
|
|
uuid: client.uuid,
|
|
avatar: client.avatar,
|
|
cust_user_id: client.user_type == 0 ? data.to_user : client.uid,
|
|
type: 'pri',
|
|
time: d.getHours() + ':' + d.getMinutes(),
|
|
content: message_content
|
|
}));
|
|
} else {
|
|
readState = 2;
|
|
systemMessage('对方暂时无法接收到该消息');
|
|
}
|
|
});
|
|
client.on('kill client', function (data) {
|
|
console.log('get kill message,data is ' + data);
|
|
var _client = userList[data].client;
|
|
if (!userList[data]) {
|
|
errorNotice('没有找到要kill的目标(not found uuid)');
|
|
return;
|
|
}
|
|
systemMessage(userList[data].name + '太调皮,需要下线休息一下');
|
|
|
|
_client.emit('error notice', '你将会在5s后被强制离线');
|
|
setTimeout(function () {
|
|
_client.disconnect();
|
|
}, 5000);
|
|
});
|
|
//
|
|
//接收到 客户端的消息
|
|
client.on('message', function (data) {
|
|
if (!data) {
|
|
client.emit('error notice', 'need message data');
|
|
return;
|
|
}
|
|
if (data.length > 7 && data.substr(0, 7) == 'sys msg') {
|
|
systemMessage(data.substr(7))
|
|
return;
|
|
}
|
|
var d = new Date(),
|
|
h = d.getHours(),
|
|
m = d.getMinutes(),
|
|
s = d.getSeconds();
|
|
|
|
io.sockets.emit("message", JSON.stringify({
|
|
name: userData.nickname,
|
|
type: 'pub',
|
|
time: (h < 10 ? '0' : h) + ':' + (h < 10 ? '0' : h) + ':' + (h < 10 ? '0' : h),
|
|
content: parseBiaoqing(data, client)
|
|
}));
|
|
});
|
|
|
|
client.on('disconnect', function () {
|
|
if (userList[_id]) {
|
|
// systemMessage(userData.nickname + ' 下线了');
|
|
delete userList[_id];
|
|
}
|
|
// 更新用户列表
|
|
updateUser();
|
|
});
|
|
}
|
|
|
|
//关闭连接
|
|
function closeClient(client, reason) {
|
|
client.emit('error notice', reason || '服务器即将断开连接(Server will disconnect)');
|
|
client.disconnect();
|
|
}
|
|
//处理用户登录
|
|
function processUserLogin(client) {
|
|
var queryData = client.handshake.query,
|
|
uid = queryData.uid,
|
|
pwd = queryData.pwd,
|
|
_id = client.id;
|
|
if (accountData[uid]) {
|
|
if (accountData[uid].pass == pwd) {
|
|
accountData[uid].uid = uuid_v4();
|
|
accountData[uid].avatar = 'https://default.wm-app.xyz/chat_server/chat/img/avatar.png';
|
|
console.log('user %s[type:%s](%s) connect success', accountData[uid]['nickname'], accountData[uid]['role'], _id);
|
|
processClient(client, accountData[uid])
|
|
} else {
|
|
closeClient(client, '登录密码不正确');
|
|
}
|
|
} else {
|
|
closeClient(client, '没有找到登录用户,请先注册!');
|
|
}
|
|
}
|
|
|
|
exports = module.exports = {
|
|
connect_key: 'test',
|
|
init: function (socket) {
|
|
io = socket;
|
|
},
|
|
processClient: processClient,
|
|
processUserLogin: processUserLogin,
|
|
systemMessage: systemMessage,
|
|
closeClient: closeClient,
|
|
getUserList: function () {
|
|
var _data = [];
|
|
for (var p in userList) {
|
|
//发送 聊天室列表
|
|
_data.push({
|
|
uuid: userList[p].uuid,
|
|
uid: userList[p].uid,
|
|
name: userList[p].name,
|
|
avatar: userList[p].avatar
|
|
});
|
|
}
|
|
return _data;
|
|
}
|
|
}; |