Enha: roomlistupdate only on change; heartbeat every 2s
This commit is contained in:
@ -63,8 +63,8 @@ func (broker *Broker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
broker.newClients <- messageChan
|
broker.newClients <- messageChan
|
||||||
defer func() { broker.closingClients <- messageChan }()
|
defer func() { broker.closingClients <- messageChan }()
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
// browser can close sse on its own
|
// browser can close sse on its own; ping every 2s to prevent
|
||||||
heartbeat := time.NewTicker(15 * time.Second)
|
heartbeat := time.NewTicker(2 * time.Second)
|
||||||
defer heartbeat.Stop()
|
defer heartbeat.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -57,6 +57,7 @@ func (cm *CronManager) CleanupRooms() {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
roomListChange := false
|
||||||
for _, room := range rooms {
|
for _, room := range rooms {
|
||||||
players, err := cm.repo.PlayerListByRoom(ctx, room.ID)
|
players, err := cm.repo.PlayerListByRoom(ctx, room.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,6 +72,7 @@ func (cm *CronManager) CleanupRooms() {
|
|||||||
if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil {
|
if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil {
|
||||||
cm.log.Error("failed to delete settings for empty room", "room_id", room.ID, "err", err)
|
cm.log.Error("failed to delete settings for empty room", "room_id", room.ID, "err", err)
|
||||||
}
|
}
|
||||||
|
roomListChange = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
creatorInRoom := false
|
creatorInRoom := false
|
||||||
@ -120,6 +122,7 @@ func (cm *CronManager) CleanupRooms() {
|
|||||||
if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil {
|
if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil {
|
||||||
cm.log.Error("failed to delete settings for room", "room_id", room.ID, "reason", reason, "err", err)
|
cm.log.Error("failed to delete settings for room", "room_id", room.ID, "reason", reason, "err", err)
|
||||||
}
|
}
|
||||||
|
roomListChange = true
|
||||||
// Move to the next room
|
// Move to the next room
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -127,11 +130,13 @@ func (cm *CronManager) CleanupRooms() {
|
|||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
cm.log.Error("failed to commit transaction", "err", err)
|
cm.log.Error("failed to commit transaction", "err", err)
|
||||||
}
|
}
|
||||||
|
if roomListChange {
|
||||||
broker.Notifier.Notifier <- broker.NotificationEvent{
|
broker.Notifier.Notifier <- broker.NotificationEvent{
|
||||||
EventName: models.NotifyRoomListUpdate,
|
EventName: models.NotifyRoomListUpdate,
|
||||||
Payload: "",
|
Payload: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (cm *CronManager) CleanupActions() {
|
func (cm *CronManager) CleanupActions() {
|
||||||
ctx, tx, err := cm.repo.InitTx(context.Background())
|
ctx, tx, err := cm.repo.InitTx(context.Background())
|
||||||
|
Reference in New Issue
Block a user