Browse Source

wip:日志

wangshan 2 months ago
parent
commit
23e685b26d
2 changed files with 6 additions and 3 deletions
  1. 4 3
      services/sse/sse.go
  2. 2 0
      util/sse.go

+ 4 - 3
services/sse/sse.go

@@ -51,20 +51,21 @@ func (s *ServerSentRouter) Notify() {
 		}()
 		// 监听消息通道并发送给客户端
 		flusher := s.ResponseWriter.(http.Flusher)
-		ticker := time.NewTicker(15 * time.Second)
+		ticker := time.NewTicker(60 * time.Second)
 		defer ticker.Stop()
 		for {
 			select {
 			case msg := <-messageChan:
 				logger.Info("--msg--:", msg, "--userId--:", userId)
 				fmt.Fprintf(s.ResponseWriter, "data: %s\n\n", msg)
+				flusher.Flush()
 			case <-ticker.C:
 				logger.Info("--heartbeat--:", userId)
 				fmt.Fprintf(s.ResponseWriter, ": heartbeat\n\n")
+				flusher.Flush()
 			case <-s.Request.Context().Done():
 				return // 客户端断开连接
 			}
-			flusher.Flush()
 		}
 	} else {
 		s.ServeJson("not login")
@@ -81,7 +82,7 @@ func (s *ServerSentRouter) Send() {
 		util.SendNotification(userId, model.SseMessage{
 			Name:   msg,
 			User:   phone,
-			State:  model.WinningTarget,
+			State:  model.TaskTarget,
 			Time:   time.Now().Format(date.Date_Full_Layout),
 			Remark: fmt.Sprintf("测试信息:恭喜 %s 抽中 %s ", phone, msg),
 		})

+ 2 - 0
util/sse.go

@@ -42,6 +42,7 @@ var (
 
 // 向指定用户发送通知
 func SendNotification(userId string, msg model.SseMessage) {
+	logger.Info("SendNotification-----SseClients:", SseClients)
 	SseClientsMu.Lock()
 	defer SseClientsMu.Unlock()
 	msgData, _ := json.Marshal(msg)
@@ -52,6 +53,7 @@ func SendNotification(userId string, msg model.SseMessage) {
 
 // 向所有用户发送通知
 func SendNotificationToAll(msg model.SseMessage) {
+	logger.Info("SendNotificationToAll ----- SseClients:", SseClients)
 	SseClientsMu.Lock()
 	defer SseClientsMu.Unlock()
 	msgData, _ := json.Marshal(msg)