kefu/io/io.js
2019-09-30 10:31:42 +08:00

247 lines
8.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
*介绍socket.io 功能封装
*作者TaiGuangYin
*时间2017-09-09
* */
var redis = require('../utils/redis');
var msgType = require('./messageTpye');
var ioSvc = require('./ioHelper').ioSvc;
var AppConfig = require('../config');
var Common = require('../utils/common');
var msgModel = require('../model/message');
const sessoionModel = require('../model/session');
//服务端连接
function ioServer(io) {
var _self = this;
ioSvc.setInstance(io);
var __uuids = [];
//初始化连接人数
redis.set('online_count', 0, null, function (err, ret) {
if (err) {
console.error(err);
}
});
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
io.on('connection', function (socket) {
console.log('SocketIO有新的连接!');
_self.updateOnlieCount(true);
//用户与Socket进行绑定
socket.on('login', async function (msg) {
var uid = msg.uid;
let type = msg.type; // 获取用户类型
console.log(uid + '登录成功');
//如果不是客服登录
if (type != 'kefu') {
let gongHao = msg.gongHao;
console.log('new customer login process gongHao=>', gongHao)
// 获取管理员的socket
try{
//获取管理员 socket
let kefuData =await sessoionModel.find(gongHao);
if(kefuData){ // 找到客服数据
let location = Common.getIpLocation(msg.ip);
let socket = socket.id;
let type = 'customer';
let kefu_id = gongHao;
let clientInfo = {
"uid": uid,
"name": location + ' 客户',
"type": 'online'
};
// 添加客户到 对应的客服
sessoionModel.create({
uid,
socket:socket.id,
type:'customer',
kefu_id:gongHao,
nickname:clientInfo.name,
})
// 给管理员发送通知
io.to(kefuData.socket).emit('update-users', info);
}
}catch (e) {
//TODO 失败重发机制
console.log('给管理员发送通知失败');
}
}
//将用户id和socket进行保定
// redis.set(uid, socket.id, 3600 * 3, function (err, ret) {
// if (err) {
// console.error(err);
// }
// });
// redis.set(socket.id,uid,null,function (err,ret) {
// if(err){
// console.error(err);
// }
// });
});
//断开事件
socket.on('disconnect', function () {
console.log("与服务其断开");
_self.updateOnlineCount(false);
redis.get(socket.id, function (err, val) {
if (err) {
console.error(err);
}
redis.del(socket.id, function (err, ret) {
if (err) {
console.error(err);
}
});
redis.del(val, function (err, ret) {
if (err) {
console.error(err);
}
});
//通知用户下线
if (val != AppConfig.KEFUUUID) {
redis.get(AppConfig.KEFUUUID, function (err, sid) {
if (err) {
console.error(err);
}
if (sid) {
var info = {
"uid": val,
"name": '客户下线',
"type": 'offline'
};
io.to(sid).emit('update-users', info);
}
});
redis.get('user-uuids', function (err, uuids) {
if (err) {
console.error(err);
}
if (uuids) {
uuids = JSON.parse(uuids);
} else {
uuids = [];
}
val = parseInt(val);
var idx = __uuids.indexOf(val);
if (idx != -1) {
__uuids.remove(val);
//uuids.splice(idx,1);
var tmp = [];
uuids.forEach(function (user) {
if (user.uid != val) {
tmp.push(user);
}
});
uuids = JSON.stringify(tmp);
redis.set('user-uuids', uuids, null, function (err, ret) {
if (err) {
console.error(err);
}
});
}
});
}
});
});
//重连事件
socket.on('reconnect', function () {
console.log("重新连接到服务器");
});
//监听客户端发送的信息,实现消息转发到各个其他客户端
socket.on('message', function (msg) {
//保存到数据库
msgModel.add(msg.from_uid, msg.uid, msg.content, msg.chat_type, msg.image, function (err) {
if (err) {
console.error(err);
}
});
if (msg.type == msgType.messageType.public) {
var mg = {
"uid": msg.from_uid,
"content": msg.content,
"chat_type": msg.chat_type ? msg.chat_type : 'text',
"image": msg.image
};
socket.broadcast.emit("message", mg);
} else if (msg.type == msgType.messageType.private) {
var uid = msg.uid;
redis.get(uid, function (err, sid) {
if (err) {
console.error(err);
}
if (sid) {
//给指定的客户端发送消息
var mg = {
"uid": msg.from_uid,
"content": msg.content,
"chat_type": msg.chat_type ? msg.chat_type : 'text',
"image": msg.image
};
io.to(sid).emit('message', mg);
}
});
}
});
});
this.updateOnlineCount = function (isConnect) {
//记录在线客户连接数
redis.get('online_count', function (err, val) {
if (err) {
console.error(err);
}
if (!val) {
val = 0;
}
if (typeof val == 'string') {
val = parseInt(val);
}
if (isConnect) {
val += 1;
} else {
val -= 1;
if (val <= 0) {
val = 0;
}
}
console.log('当前在线人数:' + val);
io.sockets.emit('update_online_count', {online_count: val});
redis.set('online_count', val, null, function (err, ret) {
if (err) {
console.error(err);
}
});
});
};
}
//模块导出
exports.ioServer = ioServer;