|
@@ -2,19 +2,38 @@ package sse
|
|
|
|
|
|
import (
|
|
|
"app.yhyue.com/moapp/jybase/common"
|
|
|
+ "app.yhyue.com/moapp/jybase/date"
|
|
|
"app.yhyue.com/moapp/jybase/go-logger/logger"
|
|
|
"app.yhyue.com/moapp/jybase/go-xweb/xweb"
|
|
|
- "app.yhyue.com/moapp/message/entity"
|
|
|
"app.yhyue.com/moapp/message/model"
|
|
|
- "encoding/json"
|
|
|
+ "app.yhyue.com/moapp/message/util"
|
|
|
"fmt"
|
|
|
"net/http"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type ServerSentRouter struct {
|
|
|
*xweb.Action
|
|
|
events xweb.Mapper `xweb:"/sse/events"`
|
|
|
+ send xweb.Mapper `xweb:"/sse/send"`
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ServerSentRouter) Send() {
|
|
|
+ msg := s.GetString("msg")
|
|
|
+ phone := s.GetString("phone")
|
|
|
+ if msg == "" {
|
|
|
+ msg = "Default notification at " + time.Now().Format(time.RFC3339)
|
|
|
+ } else {
|
|
|
+ util.SseBroadcast.SendToUsers(model.SseMessage{
|
|
|
+ Prize: msg,
|
|
|
+ User: phone,
|
|
|
+ State: model.AllTarget,
|
|
|
+ Time: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ Remark: fmt.Sprintf("测试信息:恭喜 %s 抽中 %s ", phone, msg),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ fmt.Fprintf(s.ResponseWriter, "Broadcasted: %s", msg)
|
|
|
}
|
|
|
|
|
|
func (s *ServerSentRouter) Events() {
|
|
@@ -25,45 +44,38 @@ func (s *ServerSentRouter) Events() {
|
|
|
s.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
sessVal := s.Session().GetMultiple()
|
|
|
- if userId := common.ObjToString(sessVal["userId"]); userId != "" {
|
|
|
- positionId := common.Int64All(sessVal["positionId"])
|
|
|
- clientChan := make(chan string, 1)
|
|
|
- entity.SseClients[positionId] = clientChan
|
|
|
+ userId := common.ObjToString(sessVal["userId"])
|
|
|
+ sessionId := s.Session().Id()
|
|
|
+ if sessionId != "" {
|
|
|
+ clientChan := make(chan string, 10)
|
|
|
+ util.SseBroadcast.Mu.Lock()
|
|
|
+ util.SseBroadcast.Clients[clientChan] = struct{}{}
|
|
|
+ util.SseBroadcast.Mu.Unlock()
|
|
|
defer func() {
|
|
|
- delete(entity.SseClients, positionId)
|
|
|
+ util.SseBroadcast.Mu.Lock()
|
|
|
+ delete(util.SseBroadcast.Clients, clientChan)
|
|
|
+ util.SseBroadcast.Mu.Unlock()
|
|
|
close(clientChan)
|
|
|
+ logger.Info("Client disconnected")
|
|
|
}()
|
|
|
- //go SendToClient(positionId)
|
|
|
+ flusher := s.ResponseWriter.(http.Flusher)
|
|
|
for {
|
|
|
select {
|
|
|
case msg := <-clientChan:
|
|
|
- logger.Info("msg:", msg, "---------------------------")
|
|
|
- fmt.Fprintf(s.ResponseWriter, "data: %s\n\n", msg)
|
|
|
- s.ResponseWriter.(http.Flusher).Flush()
|
|
|
+ logger.Info("--msg--:", msg, "--userId--:", userId)
|
|
|
+ var isSend bool
|
|
|
+ if strings.Contains(msg, model.AllTarget) {
|
|
|
+ isSend = true
|
|
|
+ } else if userId != "" {
|
|
|
+ isSend = true
|
|
|
+ }
|
|
|
+ if isSend {
|
|
|
+ fmt.Fprintf(s.ResponseWriter, "data: %s\n\n", msg)
|
|
|
+ flusher.Flush()
|
|
|
+ }
|
|
|
case <-s.Request.Context().Done():
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-func SendToClient(positionId int64) {
|
|
|
- clientChan, exists := entity.SseClients[positionId]
|
|
|
- if !exists {
|
|
|
- logger.Info("Client not found: %s", positionId)
|
|
|
- return
|
|
|
- }
|
|
|
- for i := 0; i < 10; i++ {
|
|
|
- message := fmt.Sprintf("这是发送内容 第 %d 条", i)
|
|
|
- msg := model.SseMessage{User: "剑鱼", Prize: message, Time: time.Now().Format("15:04:05")}
|
|
|
- msgData, _ := json.Marshal(msg)
|
|
|
- select {
|
|
|
- case clientChan <- string(msgData):
|
|
|
- logger.Info(fmt.Sprintf("Sent to client %s: %s", positionId, message))
|
|
|
- default:
|
|
|
- logger.Info(fmt.Sprintf("Channel full for client %s, message dropped: %s", positionId, message))
|
|
|
- return
|
|
|
- }
|
|
|
- time.Sleep(2 * time.Second)
|
|
|
- }
|
|
|
-}
|