fix 01
This commit is contained in:
parent
20eed6c531
commit
5fe8e5de76
191
io/io.js
191
io/io.js
@ -33,30 +33,29 @@ function ioServer(io) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
io.on('connection', function (socket) {
|
io.on('connection',async function (socket) {
|
||||||
console.log('SocketIO有新的连接!');
|
console.log('有新的连接进来了:', socket.id);
|
||||||
|
|
||||||
_self.updateOnlieCount(true);
|
|
||||||
|
|
||||||
//用户与Socket进行绑定
|
//用户与Socket进行绑定
|
||||||
socket.on('login', async function (msg) {
|
socket.on('login', async function (msg) {
|
||||||
var uid = msg.uid;
|
var uid = msg.uid;
|
||||||
|
socket._user = msg;
|
||||||
let type = msg.type; // 获取用户类型
|
let type = msg.type; // 获取用户类型
|
||||||
console.log(uid + '登录成功');
|
console.log(type, uid, '登录成功');
|
||||||
|
|
||||||
//如果不是客服登录
|
//如果不是客服登录
|
||||||
if (type != 'kefu') {
|
if (type != 'kefu') {
|
||||||
let gongHao = msg.gongHao;
|
let gongHao = msg.gongHao;
|
||||||
console.log('new customer login process gongHao=>', gongHao)
|
console.log('new customer login process gongHao=>', gongHao)
|
||||||
// 获取管理员的socket
|
// 获取管理员的socket
|
||||||
try{
|
try {
|
||||||
//获取管理员 socket
|
//获取管理员 socket
|
||||||
let kefuData =await sessoionModel.find(gongHao);
|
let kefuData = await sessoionModel.find(gongHao);
|
||||||
if(kefuData){ // 找到客服数据
|
if (kefuData) { // 找到客服数据
|
||||||
let location = Common.getIpLocation(msg.ip);
|
let location = Common.getIpLocation(msg.ip);
|
||||||
let socket = socket.id;
|
// let socket = socket.id;
|
||||||
let type = 'customer';
|
// let type = 'customer';
|
||||||
let kefu_id = gongHao;
|
// let kefu_id = gongHao;
|
||||||
|
|
||||||
let clientInfo = {
|
let clientInfo = {
|
||||||
"uid": uid,
|
"uid": uid,
|
||||||
@ -64,105 +63,63 @@ function ioServer(io) {
|
|||||||
"type": 'online'
|
"type": 'online'
|
||||||
};
|
};
|
||||||
// 添加客户到 对应的客服
|
// 添加客户到 对应的客服
|
||||||
sessoionModel.create({
|
await sessoionModel.createOrUpdate({uid}, {
|
||||||
uid,
|
uid,
|
||||||
socket:socket.id,
|
socket: socket.id,
|
||||||
type:'customer',
|
type: 'customer',
|
||||||
kefu_id:gongHao,
|
kefu_id: gongHao,
|
||||||
nickname:clientInfo.name,
|
nickname: clientInfo.name,
|
||||||
})
|
})
|
||||||
// 给管理员发送通知
|
// 给管理员发送通知
|
||||||
io.to(kefuData.socket).emit('update-users', info);
|
io.to(kefuData.socket).emit('update-users', clientInfo);
|
||||||
}
|
}
|
||||||
}catch (e) {
|
} catch (e) {
|
||||||
//TODO 失败重发机制
|
//TODO 失败重发机制
|
||||||
console.log('给管理员发送通知失败');
|
console.log('给管理员发送通知失败',e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
//将用户id和socket进行保定
|
try {
|
||||||
// redis.set(uid, socket.id, 3600 * 3, function (err, ret) {
|
// 保存客服socket
|
||||||
// if (err) {
|
sessoionModel.createOrUpdate({uid}, {
|
||||||
// console.error(err);
|
uid,
|
||||||
// }
|
socket: socket.id,
|
||||||
// });
|
type: 'kefu',
|
||||||
|
nickname: '客服 ' + uid,
|
||||||
// redis.set(socket.id,uid,null,function (err,ret) {
|
status: 1
|
||||||
// if(err){
|
})
|
||||||
// console.error(err);
|
} catch (e) {
|
||||||
// }
|
console.log('客服' + uid + '登录失败了', e);
|
||||||
// });
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//断开事件
|
//断开事件
|
||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', async function () {
|
||||||
console.log("与服务其断开");
|
console.log("与服务其断开");
|
||||||
|
|
||||||
_self.updateOnlineCount(false);
|
// _self.updateOnlineCount(false);
|
||||||
|
let user = socket['_user'];
|
||||||
|
if (user) {
|
||||||
|
// 更新用户状态
|
||||||
|
await sessoionModel.update(user.uid, {status: 0});
|
||||||
|
if (user.type == 'customer') {
|
||||||
|
// 查找对应kf通知下线操作
|
||||||
|
let kf_data = await sessoionModel.find(user.kefu_id);
|
||||||
|
if (kf_data && kf_data.status == 1) { // 客服在线才通知哟
|
||||||
|
|
||||||
redis.get(socket.id, function (err, val) {
|
var info = {
|
||||||
if (err) {
|
"uid": user.uid,
|
||||||
console.error(err);
|
"name": '客户下线',
|
||||||
}
|
"type": 'offline'
|
||||||
redis.del(socket.id, function (err, ret) {
|
};
|
||||||
if (err) {
|
io.to(kf_data.socket).emit('update-users', info);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//重连事件
|
//重连事件
|
||||||
@ -171,38 +128,36 @@ function ioServer(io) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//监听客户端发送的信息,实现消息转发到各个其他客户端
|
//监听客户端发送的信息,实现消息转发到各个其他客户端
|
||||||
socket.on('message', function (msg) {
|
socket.on('message', async function (msg) {
|
||||||
//保存到数据库
|
let userData = socket['_user'];
|
||||||
|
if (!userData) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//保存消息到数据库
|
||||||
msgModel.add(msg.from_uid, msg.uid, msg.content, msg.chat_type, msg.image, function (err) {
|
msgModel.add(msg.from_uid, msg.uid, msg.content, msg.chat_type, msg.image, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (msg.type == msgType.messageType.public) {
|
const sendMsg = {
|
||||||
var mg = {
|
"uid": msg.from_uid,
|
||||||
"uid": msg.from_uid,
|
"content": msg.content,
|
||||||
"content": msg.content,
|
"chat_type": msg.chat_type ? msg.chat_type : 'text',
|
||||||
"chat_type": msg.chat_type ? msg.chat_type : 'text',
|
"image": msg.image
|
||||||
"image": msg.image
|
};
|
||||||
};
|
if (msg.type == msgType.messageType.public) { // 广播
|
||||||
socket.broadcast.emit("message", mg);
|
socket.broadcast.emit("message", sendMsg);
|
||||||
} else if (msg.type == msgType.messageType.private) {
|
} else if (msg.type == msgType.messageType.private) {
|
||||||
var uid = msg.uid;
|
let uid = msg.uid; // 对方uid
|
||||||
redis.get(uid, function (err, sid) {
|
// 暂时不 await
|
||||||
if (err) {
|
sessoionModel.find(uid).then(toUser => {
|
||||||
console.error(err);
|
if (toUser && toUser.status == 1) { // 对方在线才发送消息哟
|
||||||
}
|
io.to(toUser.socket()).emit('message', sendMsg);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
}).catch(e => {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -6,11 +6,11 @@ var Schema = mongoose.Schema;
|
|||||||
// session模型
|
// session模型
|
||||||
var SessionSchema = new Schema({
|
var SessionSchema = new Schema({
|
||||||
uid: {type: String, index: true},
|
uid: {type: String, index: true},
|
||||||
socket: {type: String},
|
socket: {type: String,index:true},
|
||||||
// 类型
|
// 类型
|
||||||
type: {type: String, default: "customer"},
|
type: {type: String, default: "customer"},
|
||||||
// 客服编号
|
// 客服编号
|
||||||
kefu_id: {type: String},
|
kefu_id: {type: String,default:""},
|
||||||
nickname: {type: String},
|
nickname: {type: String},
|
||||||
status: {type: Number,default:1},
|
status: {type: Number,default:1},
|
||||||
create_at: {type: Date, default: Date.now}
|
create_at: {type: Date, default: Date.now}
|
||||||
@ -29,9 +29,34 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
createOrUpdate(condition,data){
|
||||||
|
return new Promise((success, fail) => {
|
||||||
|
sessionModel.findOne(condition, (err, session) => {
|
||||||
|
if(err){
|
||||||
|
fail(err);return;
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
if(session){
|
||||||
|
sessionModel.update(condition,data, (err, doc) => {
|
||||||
|
if (err) fail(err);
|
||||||
|
else success(doc);
|
||||||
|
})
|
||||||
|
}else{//新增
|
||||||
|
let model = new sessionModel(data);
|
||||||
|
model.save(data, (err, doc) => {
|
||||||
|
if (err) fail(err);
|
||||||
|
else success(doc);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}catch (e) {
|
||||||
|
fail(e)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
create(data) {
|
create(data) {
|
||||||
return new Promise((success, fail) => {
|
return new Promise((success, fail) => {
|
||||||
sessionModel.insert(data, (err, doc) => {
|
sessionModel.save(data, (err, doc) => {
|
||||||
if (err) fail(err);
|
if (err) fail(err);
|
||||||
else success(doc);
|
else success(doc);
|
||||||
})
|
})
|
||||||
|
@ -93,7 +93,7 @@ $(function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const gongHao = 10002;
|
const gongHao = 10001;
|
||||||
$("#btnSend").click(function(){
|
$("#btnSend").click(function(){
|
||||||
var msg = $("#textarea").val();
|
var msg = $("#textarea").val();
|
||||||
if(msg){
|
if(msg){
|
||||||
@ -192,8 +192,12 @@ $(function(){
|
|||||||
//连接服务器
|
//连接服务器
|
||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
//uuid = 'chat'+ guid();
|
//uuid = 'chat'+ guid();
|
||||||
var fp1 = new Fingerprint();
|
let uuid_store_key = 'kf_c_uuid';
|
||||||
uuid = fp1.get();
|
uuid = localStorage.getItem(uuid_store_key);
|
||||||
|
if(!uuid){
|
||||||
|
uuid = (new Fingerprint()).get();
|
||||||
|
localStorage.setItem(uuid_store_key,uuid);//save uuid to ls
|
||||||
|
}
|
||||||
console.log('连接成功...'+uuid);
|
console.log('连接成功...'+uuid);
|
||||||
|
|
||||||
var ip = $("#keleyivisitorip").text();
|
var ip = $("#keleyivisitorip").text();
|
||||||
@ -216,6 +220,9 @@ $(function(){
|
|||||||
insert_agent_html(msg);
|
insert_agent_html(msg);
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
});
|
});
|
||||||
|
socket.on('log',function (msg) {
|
||||||
|
console.log(msg);
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
@ -242,10 +242,10 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
|||||||
//连接服务器
|
//连接服务器
|
||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
console.log('连接成功...');
|
console.log('连接成功...');
|
||||||
uuid = 'chat-admin-' + data.username;
|
uuid = data.username;
|
||||||
var ip = $("#keleyivisitorip").html();
|
var ip = $("#keleyivisitorip").text().trim();
|
||||||
var msg = {
|
var msg = {
|
||||||
"uid": uuid,
|
"uid": data.username,
|
||||||
"ip": ip,
|
"ip": ip,
|
||||||
type:'kefu'
|
type:'kefu'
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user