From 177c5e8a234ced5385bb81e5a4324981d262001c Mon Sep 17 00:00:00 2001 From: "630892807@qq.com" <630892807@qq.com> Date: Tue, 23 Mar 2021 23:37:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E5=A2=9E=E5=8A=A0=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=88=87=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 7 ++++--- controller/message.go | 4 ++++ static/js/chat-lang.js | 2 ++ static/js/chat-page.js | 7 +++++++ ws/visitor.go | 38 +++++++++++++++++++++++++++++++++----- ws/ws.go | 16 +++++++++------- 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index 8787697..edcf5ae 100644 --- a/config/config.go +++ b/config/config.go @@ -9,9 +9,10 @@ import ( ) var ( - PageSize uint = 10 - VisitorPageSize uint = 8 - Version = "0.1.2" + PageSize uint = 10 + VisitorPageSize uint = 8 + Version = "0.1.2" + VisitorExpire float64 = 600 GoflyConfig *Config ) diff --git a/controller/message.go b/controller/message.go index c35d590..6917456 100644 --- a/controller/message.go +++ b/controller/message.go @@ -84,6 +84,10 @@ func SendMessageV2(c *gin.Context) { }) } if cType == "visitor" { + guest, ok := ws.ClientList[vistorInfo.VisitorId] + if ok && guest != nil { + guest.UpdateTime = time.Now() + } //kefuConns, ok := ws.KefuList[kefuInfo.Name] //if kefuConns == nil || !ok { // c.JSON(200, gin.H{ diff --git a/static/js/chat-lang.js b/static/js/chat-lang.js index e247f9a..3a4d6b1 100644 --- a/static/js/chat-lang.js +++ b/static/js/chat-lang.js @@ -10,6 +10,7 @@ var GOFLY_LANG={ "textarea":"请输入内容", "closemes":"系统自动关闭连接!点击会重连", "forceclosemes":"客服关闭连接!请重新打开页面", + "autoclosemes":"长时间未回应关闭连接!请刷新页面", }, "en":{ "sent":"Send", @@ -22,5 +23,6 @@ var GOFLY_LANG={ "textarea":"Enter your message", "closemes":"The system automatically closes the connection!", "forceclosemes":"Admin closes the connection! please reload", + "autoclosemes":"session closed!please reload", }, }; \ No newline at end of file diff --git a/static/js/chat-page.js b/static/js/chat-page.js index ff4b3e2..7696876 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -100,6 +100,13 @@ new Vue({ this.socket.close(); this.socketClosed=true; } + if (redata.type == "auto_close") { + this.chatTitle=GOFLY_LANG[LANG]['autoclosemes']; + $(".chatBox").append("
"+this.chatTitle+"
"); + this.scrollBottom(); + this.socket.close(); + this.socketClosed=true; + } window.parent.postMessage(redata,"*"); }, //发送给客户 diff --git a/ws/visitor.go b/ws/visitor.go index 1ede96f..c04fc3e 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" "log" "time" @@ -26,11 +27,12 @@ func NewVisitorServer(c *gin.Context) { return } user := &User{ - Conn: conn, - Name: vistorInfo.Name, - Avator: vistorInfo.Avator, - Id: vistorInfo.VisitorId, - To_id: vistorInfo.ToId, + Conn: conn, + Name: vistorInfo.Name, + Avator: vistorInfo.Avator, + Id: vistorInfo.VisitorId, + To_id: vistorInfo.ToId, + UpdateTime: time.Now(), } go models.UpdateVisitorStatus(vistorInfo.VisitorId, 1) //go SendServerJiang(vistorInfo.Name, "来了", c.Request.Host) @@ -170,3 +172,29 @@ func VisitorAutoReply(vistorInfo models.Visitor, kefuInfo models.User, content s models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, welcome.Content, "kefu") } } +func cleanVisitorExpire() { + go func() { + return + log.Println("cleanVisitorExpire start...") + for { + for _, user := range ClientList { + diff := time.Now().Sub(user.UpdateTime).Seconds() + if diff >= config.VisitorExpire { + msg := TypeMessage{ + Type: "auto_close", + Data: user.Id, + } + str, _ := json.Marshal(msg) + if err := user.Conn.WriteMessage(websocket.TextMessage, str); err != nil { + user.Conn.Close() + delete(ClientList, user.Id) + } + log.Println(user.Name + ":cleanVisitorExpire finshed") + } + } + // 计算下一个零点 + t := time.NewTimer(time.Second * 10) + <-t.C + } + }() +} diff --git a/ws/ws.go b/ws/ws.go index 0fc1f47..da20799 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -15,13 +15,14 @@ import ( ) type User struct { - Conn *websocket.Conn - Name string - Id string - Avator string - To_id string - Role_id string - Mux sync.Mutex + Conn *websocket.Conn + Name string + Id string + Avator string + To_id string + Role_id string + Mux sync.Mutex + UpdateTime time.Time } type Message struct { conn *websocket.Conn @@ -65,6 +66,7 @@ func init() { }, } go UpdateVisitorStatusCron() + cleanVisitorExpire() } func SendServerJiang(title string, content string, domain string) string { noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang"))