定时清理内存

pull/30/head
taoshihan1991 5 years ago
parent 060fc93ced
commit 1625c12cf4

@ -62,9 +62,15 @@ type UserMessage struct {
Time string `json:"time"` Time string `json:"time"`
Content string `json:"content"` Content string `json:"content"`
} }
//定时检测客户端是否在线
func init() {
sendPingToClient()
}
//兼容之前的聊天服务 //兼容之前的聊天服务
func ChatServer(w *websocket.Conn) { func ChatServer(w *websocket.Conn) {
var error error var error error
for { for {
//接受消息 //接受消息
var receive string var receive string
@ -94,7 +100,7 @@ func ChatServer(w *websocket.Conn) {
//用户id对应的连接 //用户id对应的连接
clientList[userMsg.From_id] = w clientList[userMsg.From_id] = w
clientNameList[userMsg.From_id]=userMsg.From_name clientNameList[userMsg.From_id]=userMsg.From_name
SendUserAllNotice(userMsg.From_id) SendUserAllNotice()
//客服上线 //客服上线
case "kfOnline": case "kfOnline":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData,&kfMsg)
@ -117,6 +123,9 @@ func ChatServer(w *websocket.Conn) {
case "kfChatMessage": case "kfChatMessage":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData,&kfMsg)
conn:=clientList[kfMsg.Guest_id] conn:=clientList[kfMsg.Guest_id]
if kfMsg.Guest_id=="" ||conn==nil{
return
}
msg:=NoticeMessage{ msg:=NoticeMessage{
Type: "kfChatMessage", Type: "kfChatMessage",
Data:KfMessage{ Data:KfMessage{
@ -151,16 +160,12 @@ func ChatServer(w *websocket.Conn) {
} }
} }
//发送给所有客服客户上线 //发送给所有客服客户上线
func SendUserAllNotice(uid string){ func SendUserAllNotice(){
if len(kefuList)!=0{ if len(kefuList)!=0{
//发送给客服通知 //发送给客服通知
for _, conn := range kefuList { for _, conn := range kefuList {
userInfo := make(map[string]string)
userInfo["uid"] = uid
userInfo["username"]=clientNameList[uid]
msg:=NoticeMessage{ msg:=NoticeMessage{
Type: "notice", Type: "notice",
Data:userInfo,
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str,_:=json.Marshal(msg);sendStr:=string(str)
websocket.Message.Send(conn,sendStr) websocket.Message.Send(conn,sendStr)
@ -215,8 +220,27 @@ func getOnlineUser(w *websocket.Conn){
str,_:=json.Marshal(msg);sendStr:=string(str) str,_:=json.Marshal(msg);sendStr:=string(str)
websocket.Message.Send(w,sendStr) websocket.Message.Send(w,sendStr)
} }
func SendUserChat(){} //定时给客户端发送消息判断客户端是否在线
func SendKefuChat(){} func sendPingToClient(){
msg:=NoticeMessage{
Type: "ping",
}
go func() {
for{
log.Println(2)
str,_:=json.Marshal(msg);sendStr:=string(str)
for uid, conn := range clientList {
err:=websocket.Message.Send(conn,sendStr)
if err!=nil{
delete(clientList,uid)
SendUserAllNotice()
}
}
time.Sleep(10*time.Second)
}
}()
}
var clientList = make(map[string]*websocket.Conn) var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string) var clientNameList = make(map[string]string)
var kefuList = make(map[string]*websocket.Conn) var kefuList = make(map[string]*websocket.Conn)

@ -158,16 +158,17 @@
switch (redata.type){ switch (redata.type){
case "getOnlineUsers": case "getOnlineUsers":
this.handleOnlineUsers(redata.data); this.handleOnlineUsers(redata.data);
this.sendKefuOnline();
break; break;
case "notice": case "notice":
if(!this.usersMap[redata.data.uid]){ // if(!this.usersMap[redata.data.uid]){
this.$notify({ // this.$notify({
title: "通知", // title: "通知",
message: "新客户访问", // message: "新客户访问",
type: 'success', // type: 'success',
duration: 0, // duration: 0,
}); // });
} // }
this.getOnlineUsers(); this.getOnlineUsers();
this.sendKefuOnline(); this.sendKefuOnline();
break; break;
@ -228,6 +229,9 @@
}, },
//发送给客户 //发送给客户
chatToUser() { chatToUser() {
if(this.messageContent==""||kfConfig.guest_id==""){
return;
}
let mes = {}; let mes = {};
mes.type = "kfChatMessage"; mes.type = "kfChatMessage";
kfConfig.content = this.messageContent; kfConfig.content = this.messageContent;

@ -218,7 +218,7 @@
shadeClose: true, shadeClose: true,
shade: false, shade: false,
maxmin: true, //开启最大化最小化按钮 maxmin: true, //开启最大化最小化按钮
area: ['860px', '600px'], area: ['660px', '600px'],
content: ['/chat_page','no'], content: ['/chat_page','no'],
end: function(){ end: function(){
$(".chatBtn").show(); $(".chatBtn").show();

Loading…
Cancel
Save