From 0716c2145b77d12957a7f013fbf7477b7856072a Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Mon, 29 Jun 2020 11:46:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E5=88=86=E5=9C=A8=E7=BA=BF=E5=92=8C?= =?UTF-8?q?=E7=A6=BB=E7=BA=BF=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 24 ++++++++++++++++++++---- models/visitors.go | 14 +++++++++++++- readme.md | 8 +++++--- static/html/chat_main.html | 5 +++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/controller/chat.go b/controller/chat.go index a5d3ab0..0ad8abd 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -21,7 +21,7 @@ type Message struct{ } var clientList = make(map[string]*vistor) var kefuList = make(map[string]*websocket.Conn) -var message = make(chan *Message, 10) +var message = make(chan *Message) type TypeMessage struct { Type interface{} `json:"type"` @@ -44,6 +44,7 @@ func init() { ReadBufferSize: 1024, WriteBufferSize: 1024, } + go sendPingUpdateStatus() go singleBroadcaster() sendPingToClient() } @@ -103,6 +104,7 @@ func sendPingToClient() { err := user.conn.WriteMessage(websocket.TextMessage,str) if err != nil { delete(clientList, uid) + models.UpdateVisitorStatus(uid,0) SendNoticeToAllKefu() } } @@ -111,6 +113,19 @@ func sendPingToClient() { }() } +//定时给更新数据库状态 +func sendPingUpdateStatus() { + for { + visitors:=models.FindVisitorsOnline() + for _,visitor :=range visitors{ + _,ok:=clientList[visitor.VisitorId] + if !ok{ + models.UpdateVisitorStatus(visitor.VisitorId,0) + } + } + time.Sleep(10 * time.Second) + } +} func SendNoticeToAllKefu() { if len(kefuList) != 0 { //发送给客服通知 @@ -127,7 +142,6 @@ func SendNoticeToAllKefu() { //获取当前的在线用户 func getOnlineUser(w *websocket.Conn) { result := make([]map[string]string, 0) - log.Println(clientList) for _, user := range clientList { userInfo := make(map[string]string) userInfo["uid"] = user.id @@ -146,12 +160,14 @@ func getOnlineUser(w *websocket.Conn) { func singleBroadcaster(){ for { message:=<-message + log.Println("debug:",message) + var typeMsg TypeMessage var clientMsg ClientMessage json.Unmarshal(message.content, &typeMsg) conn:=message.conn if typeMsg.Type == nil || typeMsg.Data == nil { - break + continue } msgType := typeMsg.Type.(string) msgData, _ := json.Marshal(typeMsg.Data) @@ -180,7 +196,7 @@ func singleBroadcaster(){ kefuList[clientMsg.Id] = conn //发送给客户 if len(clientList) == 0 { - break + continue } //for _, conn := range clientList { // SendKefuOnline(kfMsg, conn) diff --git a/models/visitors.go b/models/visitors.go index 3d93325..3945533 100644 --- a/models/visitors.go +++ b/models/visitors.go @@ -17,6 +17,8 @@ type Visitor struct { func CreateVisitor(name string,avator string,sourceIp string,toId string,visitorId string,refer string,city string,clientIp string){ old:=FindVisitorByVistorId(visitorId) if old.Name!=""{ + //更新状态上线 + UpdateVisitorStatus(visitorId,1) return } v:=&Visitor{ @@ -39,6 +41,16 @@ func FindVisitorByVistorId(visitorId string)Visitor{ } func FindVisitors()[]Visitor{ var visitors []Visitor - DB.Find(&visitors) + DB.Order("status desc, updated_at desc").Find(&visitors) return visitors +} +func FindVisitorsOnline()[]Visitor{ + var visitors []Visitor + DB.Where("status = ?",1).Find(&visitors) + return visitors +} +func UpdateVisitorStatus(visitorId string,status uint){ + visitor:=Visitor{ + } + DB.Model(&visitor).Where("visitor_id = ?",visitorId).Update("status", status) } \ No newline at end of file diff --git a/readme.md b/readme.md index 5a97e52..8153a35 100644 --- a/readme.md +++ b/readme.md @@ -33,11 +33,13 @@ ### 安装使用 -1. 先安装和运行mysql , 创建go-fly数据库,并导入*.sql创建表结构与数据,在config +1. 先安装和运行mysql , 创建go-fly数据库,并导入*.sql创建表结构与数据. -2. 在go get github.com/taoshihan1991/go-fly ,进入$GOPATH/src/github.com/taoshihan1991/go-fly 目录 +2. 在go get github.com/taoshihan1991/go-fly + + 进入$GOPATH/src/github.com/taoshihan1991/go-fly 目录 - 目录mysql.json中配置数据库 + 在config目录mysql.json中配置数据库 ```php { "Server":"127.0.0.1", diff --git a/static/html/chat_main.html b/static/html/chat_main.html index ae7d6fe..e9cb6ea 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -57,6 +57,7 @@ .chatBox{max-height: 350px;overflow-y: auto;overflow-x: hidden;} .onlineUsers{padding: 5px;height: 40px;line-height: 40px;font-size: 14px;border-bottom: solid 1px #e6e6e6;} .onlineUsers:hover,.onlineUsers.cur{background-color: #f0f9eb;color: #67C23A;} + .imgGray {-webkit-filter: grayscale(100%);-ms-filter: grayscale(100%);filter: grayscale(100%);filter: gray;color:#888;} @@ -82,9 +83,9 @@
- + - + <{item.name}>