准备增加定时切断

pull/23/head
630892807@qq.com 3 years ago
parent 29aceb65a0
commit 177c5e8a23

@ -9,9 +9,10 @@ import (
) )
var ( var (
PageSize uint = 10 PageSize uint = 10
VisitorPageSize uint = 8 VisitorPageSize uint = 8
Version = "0.1.2" Version = "0.1.2"
VisitorExpire float64 = 600
GoflyConfig *Config GoflyConfig *Config
) )

@ -84,6 +84,10 @@ func SendMessageV2(c *gin.Context) {
}) })
} }
if cType == "visitor" { if cType == "visitor" {
guest, ok := ws.ClientList[vistorInfo.VisitorId]
if ok && guest != nil {
guest.UpdateTime = time.Now()
}
//kefuConns, ok := ws.KefuList[kefuInfo.Name] //kefuConns, ok := ws.KefuList[kefuInfo.Name]
//if kefuConns == nil || !ok { //if kefuConns == nil || !ok {
// c.JSON(200, gin.H{ // c.JSON(200, gin.H{

@ -10,6 +10,7 @@ var GOFLY_LANG={
"textarea":"请输入内容", "textarea":"请输入内容",
"closemes":"系统自动关闭连接!点击会重连", "closemes":"系统自动关闭连接!点击会重连",
"forceclosemes":"客服关闭连接!请重新打开页面", "forceclosemes":"客服关闭连接!请重新打开页面",
"autoclosemes":"长时间未回应关闭连接!请刷新页面",
}, },
"en":{ "en":{
"sent":"Send", "sent":"Send",
@ -22,5 +23,6 @@ var GOFLY_LANG={
"textarea":"Enter your message", "textarea":"Enter your message",
"closemes":"The system automatically closes the connection!", "closemes":"The system automatically closes the connection!",
"forceclosemes":"Admin closes the connection! please reload", "forceclosemes":"Admin closes the connection! please reload",
"autoclosemes":"session closed!please reload",
}, },
}; };

@ -100,6 +100,13 @@ new Vue({
this.socket.close(); this.socket.close();
this.socketClosed=true; this.socketClosed=true;
} }
if (redata.type == "auto_close") {
this.chatTitle=GOFLY_LANG[LANG]['autoclosemes'];
$(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
this.scrollBottom();
this.socket.close();
this.socketClosed=true;
}
window.parent.postMessage(redata,"*"); window.parent.postMessage(redata,"*");
}, },
//发送给客户 //发送给客户

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/models"
"log" "log"
"time" "time"
@ -26,11 +27,12 @@ func NewVisitorServer(c *gin.Context) {
return return
} }
user := &User{ user := &User{
Conn: conn, Conn: conn,
Name: vistorInfo.Name, Name: vistorInfo.Name,
Avator: vistorInfo.Avator, Avator: vistorInfo.Avator,
Id: vistorInfo.VisitorId, Id: vistorInfo.VisitorId,
To_id: vistorInfo.ToId, To_id: vistorInfo.ToId,
UpdateTime: time.Now(),
} }
go models.UpdateVisitorStatus(vistorInfo.VisitorId, 1) go models.UpdateVisitorStatus(vistorInfo.VisitorId, 1)
//go SendServerJiang(vistorInfo.Name, "来了", c.Request.Host) //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") 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
}
}()
}

@ -15,13 +15,14 @@ import (
) )
type User struct { type User struct {
Conn *websocket.Conn Conn *websocket.Conn
Name string Name string
Id string Id string
Avator string Avator string
To_id string To_id string
Role_id string Role_id string
Mux sync.Mutex Mux sync.Mutex
UpdateTime time.Time
} }
type Message struct { type Message struct {
conn *websocket.Conn conn *websocket.Conn
@ -65,6 +66,7 @@ func init() {
}, },
} }
go UpdateVisitorStatusCron() go UpdateVisitorStatusCron()
cleanVisitorExpire()
} }
func SendServerJiang(title string, content string, domain string) string { func SendServerJiang(title string, content string, domain string) string {
noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang")) noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang"))

Loading…
Cancel
Save