Rate limit websocket connection

This commit is contained in:
Marco 2024-05-12 15:42:40 +02:00
parent 4207a0ed72
commit 97ad45e505
2 changed files with 8 additions and 4 deletions

View File

@ -1,4 +1,4 @@
package websocket
package handler
import (
"context"
@ -21,6 +21,8 @@ var upgrader = gorillaws.Upgrader{
}
func RegisterWebSocketConnection(c *gin.Context) {
limiter.Take()
log.Println(c.Request)
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
@ -31,6 +33,8 @@ func RegisterWebSocketConnection(c *gin.Context) {
}
func waitForAndHandlePlayerID(ctx context.Context, conn *gorillaws.Conn) {
limiter.Take()
msgType, msg, err := conn.ReadMessage()
if err != nil {
errorMessage := fmt.Sprintf("Reading from websocket connection did not work: %s", err)
@ -72,5 +76,6 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *gorillaws.Conn) {
}
func ConnectWsForGame(c *gin.Context) {
limiter.Take()
}

View File

@ -4,7 +4,6 @@ import (
"flag"
"log"
"mchess_server/api/handler"
"mchess_server/api/websocket"
"github.com/gin-gonic/gin"
)
@ -26,10 +25,10 @@ func main() {
router := gin.Default()
router.GET("/api/hostPrivate", handler.HostGameHandler)
router.POST("/api/joinPrivate", handler.JoinPrivateGame)
router.GET("/api/ws", websocket.RegisterWebSocketConnection)
router.GET("/api/ws", handler.RegisterWebSocketConnection)
router.GET("/api/getLobbyForPassphrase/:phrase", handler.GetLobbyForPassphraseHandler)
router.GET("/api/registerWsForGame/:id", websocket.ConnectWsForGame)
router.GET("/api/registerWsForGame/:id", handler.ConnectWsForGame)
router.POST("/api/joinGame/:id", handler.JoinGameHandler)
if debugMode {