加入10s心跳

This commit is contained in:
srcrs 2023-08-02 01:46:57 +08:00
parent c369404e28
commit b20e67b951
2 changed files with 15 additions and 0 deletions

View File

@ -113,6 +113,10 @@
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
const socket = new WebSocket(protocol + window.location.host + "/ws"); const socket = new WebSocket(protocol + window.location.host + "/ws");
socket.onmessage = event => { socket.onmessage = event => {
if (event.data === 'heartbeat') {
// 心跳消息,忽略处理
return;
}
const feed = JSON.parse(event.data); const feed = JSON.parse(event.data);
const existingFeed = this.feeds.find(f => f.link === feed.link); const existingFeed = this.feeds.find(f => f.link === feed.link);
if (existingFeed) { if (existingFeed) {

11
main.go
View File

@ -72,6 +72,17 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
defer conn.Close() defer conn.Close()
if rssUrls.AutoUpdatePush > 0 {
go func() {
for {
if err := conn.WriteMessage(websocket.TextMessage, []byte("heartbeat")); err != nil {
log.Printf("heartbeat Write error: %v", err)
return
}
time.Sleep(time.Duration(10) * time.Second)
}
}()
}
for { for {
for _, url := range rssUrls.Values { for _, url := range rssUrls.Values {
feedJSON, err := get(url) feedJSON, err := get(url)