mirror of https://gitee.com/godoos/godoos.git
11 changed files with 249 additions and 137 deletions
@ -0,0 +1,90 @@ |
|||||
|
package sys |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"net/http" |
||||
|
"sync" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
// 定义消息结构体
|
||||
|
type Message struct { |
||||
|
Type string `json:"type"` |
||||
|
Data interface{} `json:"data"` |
||||
|
} |
||||
|
|
||||
|
// 定义一个结构体来保存客户端连接
|
||||
|
type client struct { |
||||
|
conn http.ResponseWriter |
||||
|
} |
||||
|
|
||||
|
var clients = make(map[*client]bool) |
||||
|
var mutex sync.Mutex |
||||
|
|
||||
|
// 处理客户端连接
|
||||
|
func HandleSystemEvents(w http.ResponseWriter, r *http.Request) { |
||||
|
// 设置响应头
|
||||
|
w.Header().Set("Content-Type", "text/event-stream") |
||||
|
w.Header().Set("Cache-Control", "no-cache") |
||||
|
w.Header().Set("Connection", "keep-alive") |
||||
|
|
||||
|
// 创建客户端连接对象
|
||||
|
c := &client{conn: w} |
||||
|
|
||||
|
// 添加客户端到列表
|
||||
|
mutex.Lock() |
||||
|
clients[c] = true |
||||
|
mutex.Unlock() |
||||
|
// 发送初始消息
|
||||
|
updateInfo, err := GetUpdateInfo() |
||||
|
if err == nil { |
||||
|
SendToClient(c, Message{ |
||||
|
Type: "update", |
||||
|
Data: updateInfo, |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
// 监听客户端关闭
|
||||
|
defer func() { |
||||
|
mutex.Lock() |
||||
|
delete(clients, c) |
||||
|
mutex.Unlock() |
||||
|
}() |
||||
|
|
||||
|
// 读取客户端请求直到关闭
|
||||
|
for { |
||||
|
select { |
||||
|
case <-r.Context().Done(): |
||||
|
return |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 向客户端发送消息
|
||||
|
func SendToClient(c *client, msg Message) { |
||||
|
jsonMsg, err := json.Marshal(msg) |
||||
|
if err != nil { |
||||
|
fmt.Println("Error marshaling JSON:", err) |
||||
|
return |
||||
|
} |
||||
|
fmt.Fprintf(c.conn, "data: %s\n\n", jsonMsg) |
||||
|
c.conn.(http.Flusher).Flush() |
||||
|
} |
||||
|
|
||||
|
// 广播消息给所有客户端
|
||||
|
func Broadcast(msg Message) { |
||||
|
mutex.Lock() |
||||
|
defer mutex.Unlock() |
||||
|
for c := range clients { |
||||
|
SendToClient(c, msg) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 每隔一段时间广播一条消息
|
||||
|
func SendMessagePeriodically(msg Message) { |
||||
|
ticker := time.NewTicker(1 * time.Second) |
||||
|
for range ticker.C { |
||||
|
Broadcast(msg) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue