fixed broadcast bug

This commit is contained in:
2023-02-25 01:10:59 +00:00
parent 4011f3e83f
commit d42cde1b6f
3 changed files with 41 additions and 2 deletions

View File

@@ -81,6 +81,15 @@ func RemoveTableGameSession(db *gorm.DB) (*gorm.DB, error) {
return db, nil
}
func RemoveTableGameSessionID(db *gorm.DB, id string) (*gorm.DB, error) {
var gameSession GameSession
// Creates table of type GameSessions
db.Where("game_session_id = ?", id).Delete(&gameSession)
// returns variable DB of type *gorm.DB and error
// which is nil at the current moment
return db, nil
}
// DisplayGameSessions Returns all the rows of all the game session information
// to display
func DisplayGameSessions(db *gorm.DB) ([]*GameSession, error) {

View File

@@ -3,8 +3,10 @@ package server
import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
"strconv"
"time"
)
// Server Starts the server for the client side
@@ -91,7 +93,7 @@ func Server(port string) error {
// TODO redirect to homepage
// Set client Cookie as Session ID
// To be changed when we use HTTPS
c.SetCookie("SessionID", session.SessionKey, 3600, "/", "localhost", false, true)
c.SetCookie("SessionID", session.SessionKey, 3600, "/", "*", false, true)
// redirects to the home page
//c.Redirect(http.StatusFound, "/")
@@ -178,6 +180,8 @@ func Server(port string) error {
}
})
go CheckIfGameSessionIsActiveOrRemove(connect)
// Run gin server on the specified port
err = r.Run(":" + port)
if err != nil {
@@ -186,3 +190,29 @@ func Server(port string) error {
return nil
}
func CheckIfGameSessionIsActiveOrRemove(gorm *gorm.DB) {
fmt.Println("here")
for {
time.Sleep(2 * time.Second)
Gamesessions, err := DisplayGameSessions(gorm)
if err != nil {
return
}
for i, _ := range Gamesessions {
req, err := http.NewRequest("GET", Gamesessions[i].Link, nil)
// Sending request to the backend server
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
// remove from database game session
_, err = RemoveTableGameSessionID(gorm, Gamesessions[i].GameSessionID)
if err != nil {
fmt.Println(err)
return
}
}
}
}
}