wangchuanjin преди 2 години
родител
ревизия
ceee5b49df
променени са 1 файла, в които са добавени 34 реда и са изтрити 25 реда
  1. 34 25
      src/jfw/front/websocket.go

+ 34 - 25
src/jfw/front/websocket.go

@@ -350,35 +350,44 @@ func GetBuoyMsg(conn *websocket.Conn) {
 		}
 	}()
 	//发送消息
+	counter := 0
+	isFirst := true
 	for {
-		if err := websocket.Message.Send(conn, "HeartBeat"); err != nil {
-			conn.Close()
-			return
-		}
-		timeA := time.Now().AddDate(0, 0, -qutil.IntAllDef(messageCenter["limitDay"], 7))
-		timeB, err := time.ParseInLocation(qutil.Date_Full_Layout, createtime, time.Local)
-		if err == nil && timeA.After(timeB) {
-			createtime = qutil.FormatDate(&timeA, qutil.Date_Full_Layout)
+		counter++
+		if counter%5 == 0 {
+			if err := websocket.Message.Send(conn, "HeartBeat"); err != nil {
+				conn.Close()
+				return
+			}
 		}
-		list := public.BaseMysql.SelectBySql(`select id,title,link,content,show_content,pc_tip,isdel,isRead from `+dbName+`.message where receive_userid=? and appid=? and createtime>? order by createtime desc limit ?`, userid, appid, createtime, limitCount)
-		if list != nil {
-			for _, v := range *list {
-				if qutil.IntAll(v["pc_tip"]) == 1 || qutil.IntAll(v["isdel"]) != 1 || qutil.IntAll(v["isRead"]) == 1 {
-					continue
-				}
-				id := qutil.Int64All(v["id"])
-				reply := fmt.Sprintf(`{"id":%d,"title":"%s","content":"%s","show_content":"%s","link":"%s"}`, id, qutil.ObjToString(v["title"]), qutil.ObjToString(v["content"]), qutil.ObjToString(v["show_content"]), strings.Split(qutil.ObjToString(v["link"]), ",")[0])
-				if err := websocket.Message.Send(conn, reply); err != nil {
-					//log.Println("websocket发送失败!", err)
-					conn.Close()
-					return
-				}
-				if id > 0 {
-					public.BaseMysql.UpdateOrDeleteBySql(`update `+dbName+`.message set pc_tip=1 where id=?`, id)
+		if isFirst || counter >= interval {
+			counter = 0
+			timeA := time.Now().AddDate(0, 0, -qutil.IntAllDef(messageCenter["limitDay"], 7))
+			timeB, err := time.ParseInLocation(qutil.Date_Full_Layout, createtime, time.Local)
+			if err == nil && timeA.After(timeB) {
+				createtime = qutil.FormatDate(&timeA, qutil.Date_Full_Layout)
+			}
+			list := public.BaseMysql.SelectBySql(`select id,title,link,content,show_content,pc_tip,isdel,isRead from `+dbName+`.message where receive_userid=? and appid=? and createtime>? order by createtime desc limit ?`, userid, appid, createtime, limitCount)
+			if list != nil {
+				for _, v := range *list {
+					if qutil.IntAll(v["pc_tip"]) == 1 || qutil.IntAll(v["isdel"]) != 1 || qutil.IntAll(v["isRead"]) == 1 {
+						continue
+					}
+					id := qutil.Int64All(v["id"])
+					reply := fmt.Sprintf(`{"id":%d,"title":"%s","content":"%s","show_content":"%s","link":"%s"}`, id, qutil.ObjToString(v["title"]), qutil.ObjToString(v["content"]), qutil.ObjToString(v["show_content"]), strings.Split(qutil.ObjToString(v["link"]), ",")[0])
+					if err := websocket.Message.Send(conn, reply); err != nil {
+						//log.Println("websocket发送失败!", err)
+						conn.Close()
+						return
+					}
+					if id > 0 {
+						public.BaseMysql.UpdateOrDeleteBySql(`update `+dbName+`.message set pc_tip=1 where id=?`, id)
+					}
+					time.Sleep(time.Second)
 				}
-				time.Sleep(time.Second)
 			}
 		}
-		time.Sleep(time.Duration(interval) * time.Second)
+		isFirst = false
+		time.Sleep(time.Second)
 	}
 }