diff --git a/io/io.js b/io/io.js
index 7a6e62b..a0c3efb 100644
--- a/io/io.js
+++ b/io/io.js
@@ -7,6 +7,7 @@ var redis = require('../utils/redis');
var msgType = require('./messageTpye');
var ioSvc = require('./ioHelper').ioSvc;
var AppConfig = require('../config');
+var Common = require('../utils/common');
//服务端连接
function ioServer(io) {
@@ -14,6 +15,8 @@ function ioServer(io) {
var _self = this;
ioSvc.setInstance(io);
+ var __uuids = [];
+
//初始化连接人数
redis.set('online_count',0,null,function (err,ret) {
if(err){
@@ -34,7 +37,8 @@ function ioServer(io) {
_self.updateOnlieCount(true);
//用户与Socket进行绑定
- socket.on('login', function (uid) {
+ socket.on('login', function (msg) {
+ var uid = msg.uid;
console.log(uid+'登录成功');
//通知用户上线
@@ -54,36 +58,45 @@ function ioServer(io) {
if(typeof val == 'string'){
val = parseInt(val);
}
- var info = {
- "uid":uid,
- "name":'客户'+val,
- "type":'online'
- };
- io.to(sid).emit('update-users',info);
+
+ //var ip = socket.request.connection.remoteAddress;
+ //此处获取IP可能会有延迟,建议改成自己的IP库
+ Common.getIpLocation(msg.ip,function (err,location) {
+ var info = {
+ "uid":uid,
+ "name":location + ' 客户',
+ "type":'online'
+ };
+
+ redis.get('user-uuids',function (err,uuids) {
+ if(err){
+ console.error(err);
+ }
+ if(uuids){
+ uuids =JSON.parse(uuids);
+ }else{
+ uuids = [];
+ }
+
+ if(__uuids.indexOf(uid) == -1){
+ __uuids.push(uid);
+ var d_user = {"uid":uid,"name":location + ' 客户'};
+ uuids.push(d_user);
+ uuids = JSON.stringify(uuids);
+ redis.set('user-uuids',uuids,null,function (err,ret) {
+ if(err){
+ console.error(err);
+ }
+ });
+ }
+ });
+
+ 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 = [];
- }
- if(uuids.indexOf(uid) == -1){
- uuids.push(uid);
- uuids = JSON.stringify(uuids);
- redis.set('user-uuids',uuids,null,function (err,ret) {
- if(err){
- console.error(err);
- }
- });
- }
- });
-
}
redis.set(uid,socket.id,null,function (err,ret) {
@@ -146,9 +159,10 @@ function ioServer(io) {
}else{
uuids = [];
}
-
- if(uuids.indexOf(val) != -1){
- uuids.remove(val);
+ var idx = __uuids.indexOf(val);
+ if( idx != -1){
+ __uuids.remove(val);
+ uuids.splice(idx,1);
uuids = JSON.stringify(uuids);
redis.set('user-uuids',uuids,null,function (err,ret) {
if(err){
diff --git a/public/js/client/client.js b/public/js/client/client.js
index 05ae6ef..dafb2c1 100644
--- a/public/js/client/client.js
+++ b/public/js/client/client.js
@@ -64,7 +64,14 @@ $(function(){
var fp1 = new Fingerprint();
uuid = fp1.get();
console.log('连接成功...'+uuid);
- socket.emit('login', uuid);
+
+ var ip = $("#keleyivisitorip").html();
+ var msg = {
+ "uid" : uuid,
+ "ip" : ip
+ };
+ socket.emit('login', msg);
+
});
// /* 后端推送来消息时
diff --git a/public/js/common.js b/public/js/common.js
index 6280dd8..efb75ce 100644
--- a/public/js/common.js
+++ b/public/js/common.js
@@ -1,30 +1,37 @@
- //用于生成uuid
- function S4() {
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
- }
+//用于生成uuid
+function S4() {
+ return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+}
- function guid() {
- return (S4()+S4()+"-"+S4()+S4()+S4());
- }
+function guid() {
+ return (S4()+S4()+"-"+S4()+S4()+S4());
+}
- function dateFormat(fmt,date) {
- if(!fmt){
- fmt = "yyyy-MM-dd hh:mm:ss";
- }
- if(!date){
- date = new Date();
- }
- var o = {
- "M+": date.getMonth() + 1, //月份
- "d+": date.getDate(), //日
- "h+": date.getHours(), //小时
- "m+": date.getMinutes(), //分
- "s+": date.getSeconds(), //秒
- "q+": Math.floor((date.getMonth() + 3) / 3), //季度
- "S": date.getMilliseconds() //毫秒
- };
- if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o)
- if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- return fmt;
- }
\ No newline at end of file
+function dateFormat(fmt,date) {
+ if(!fmt){
+ fmt = "yyyy-MM-dd hh:mm:ss";
+ }
+ if(!date){
+ date = new Date();
+ }
+ var o = {
+ "M+": date.getMonth() + 1, //月份
+ "d+": date.getDate(), //日
+ "h+": date.getHours(), //小时
+ "m+": date.getMinutes(), //分
+ "s+": date.getSeconds(), //秒
+ "q+": Math.floor((date.getMonth() + 3) / 3), //季度
+ "S": date.getMilliseconds() //毫秒
+ };
+ if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
+ for (var k in o)
+ if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
+ return fmt;
+}
+
+function arrayRemove(array,val) {
+ var index = array.indexOf(val);
+ if (index > -1) {
+ array.splice(index, 1);
+ }
+};
\ No newline at end of file
diff --git a/public/js/server/index.js b/public/js/server/index.js
index b130287..54198a4 100644
--- a/public/js/server/index.js
+++ b/public/js/server/index.js
@@ -10,14 +10,6 @@ layui.use(['layer', 'form', 'jquery'], function () {
var uuids = [];
- Array.prototype.remove = function(val) {
- var index = this.indexOf(val);
- if (index > -1) {
- this.splice(index, 1);
- }
- };
-
-
//页面初始化函数
function init() {
$(".admin-index").addClass("layui-this");
@@ -87,14 +79,14 @@ layui.use(['layer', 'form', 'jquery'], function () {
$('.chat-user').html('');
var data = data.data;
- var count = 1;
- data.forEach(function (uid) {
- insert_user_html(uid,'客户' + count++);
+
+ data.forEach(function (user) {
+ insert_user_html(user.uid,user.name);
//创建聊天section
- insert_section(uid);
+ insert_section(user.uid);
});
if(data.length > 0 && !currentUUID){
- currentUUID = data[0];
+ currentUUID = data[0].uid;
}
$(".user-info").css("background","#ffffff");
@@ -125,7 +117,12 @@ layui.use(['layer', 'form', 'jquery'], function () {
socket.on('connect', function () {
console.log('连接成功...');
uuid = 'chat-kefu-admin';
- socket.emit('login', uuid);
+ var ip = $("#keleyivisitorip").html();
+ var msg = {
+ "uid" : uuid,
+ "ip" : ip
+ };
+ socket.emit('login', msg);
});
//后端推送来消息时
@@ -138,23 +135,33 @@ layui.use(['layer', 'form', 'jquery'], function () {
socket.on('update-users', function(msg){
if(msg.type == 'offline'){
$("#"+msg.uid).remove();
- uuids.remove(msg.uid);
+ arrayRemove(uuids,msg.uid);
+ $("#section-" + msg.uid).remove();
+ $(".chat-user").find("#"+msg.uid).remove();
}else if(msg.type == 'online'){
if(!currentUUID){
currentUUID = msg.uid;
}
- if(uuids.indexOf(msg.uid) == -1){
+ var index = uuids.indexOf(msg.uid);
+ if( index == -1){
uuids.push(msg.uid);
- insert_user_html(msg.uid,msg.name);
+ insert_user_html(msg.uid,msg.name + '#'+ (uuids.length + 1));
//创建聊天section
insert_section(msg.uid);
+ }else{
+ if($(".chat-user").find("#2316602733").length == 0){
+ insert_user_html(msg.uid,msg.name + '#'+ (uuids.length + 1));
+ //创建聊天section
+ insert_section(msg.uid);
+ }
}
}
});
//更新用户在线数
socket.on('update_online_count', function(msg){
- $(".friend-head-right").html( (msg.online_count - 1) + '人' );
+ var count = (msg.online_count - 1) >= 0 ? (msg.online_count - 1) : 0;
+ $(".friend-head-right").html( count + '人' );
});
//切换用户
diff --git a/utils/common.js b/utils/common.js
new file mode 100644
index 0000000..8795917
--- /dev/null
+++ b/utils/common.js
@@ -0,0 +1,28 @@
+var http=require('http');
+
+function getClientIp(req) {
+ return req.headers['x-forwarded-for'] ||
+ req.connection.remoteAddress ||
+ req.socket.remoteAddress ||
+ req.connection.socket.remoteAddress;
+};
+
+function getIpLocation(ip,callback) {
+ http.get('http://ip.taobao.com/service/getIpInfo.php?ip='+ip,function(req,res){
+ var html='';
+ req.on('data',function(data){
+ html+=data;
+ });
+ req.on('end',function(){
+ console.info(html);
+ var json = JSON.parse(html);
+ if(json.code == 0){
+ return callback(null,json.data.region + json.data.city);
+ }
+
+ });
+ });
+}
+
+exports.getClientIp = getClientIp;
+exports.getIpLocation = getIpLocation;
\ No newline at end of file
diff --git a/views/client/index.ejs b/views/client/index.ejs
index 588f41d..d856d1b 100644
--- a/views/client/index.ejs
+++ b/views/client/index.ejs
@@ -21,7 +21,7 @@
客服系统
+ 客服系统