86 lines
2.4 KiB
Go
Executable File
86 lines
2.4 KiB
Go
Executable File
package websocket
|
|
|
|
import (
|
|
"fmt"
|
|
participanthandler "legatus-rme/backend/participantHandler"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
func (s *Server) participantHandler(w http.ResponseWriter, r *http.Request) {
|
|
c, err := s.Upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Println("Websocket Server - Error upgrading connection:", err)
|
|
return
|
|
}
|
|
defer c.Close()
|
|
|
|
for {
|
|
var req ParticipantHandlerWebsocketRequestWithoutData
|
|
if err := c.ReadJSON(&req); err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if req.Type != "getAllParticipants" {
|
|
req2 := ParticipantHandlerWebsocketRequest(req)
|
|
err = validator.New().Struct(req2)
|
|
if err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
} else {
|
|
err = validator.New().Struct(req)
|
|
if err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
var (
|
|
participantHandlerByExcersiseId participanthandler.ParticipantHandler
|
|
ok bool
|
|
)
|
|
if participantHandlerByExcersiseId, ok = s.ParticipantHandlers[req.ExcerciseId]; !ok {
|
|
participantHandlerByExcersiseId = OpenWebsocket("participantHandler", []int{s.ExerciseServicePortMap[fmt.Sprintf("%d-ParticipantManagement1", req.ExcerciseId)], req.ExcerciseId * 102}).(participanthandler.ParticipantHandler)
|
|
}
|
|
|
|
switch req.Type {
|
|
case "checkIfParticipantIsValid":
|
|
participantData := participantHandlerByExcersiseId.CheckIfParticipantIsValid(req.ParticipantData.ParticipantId)
|
|
c.WriteJSON(&participantData)
|
|
continue
|
|
case "getAllParticipants":
|
|
participantData := participantHandlerByExcersiseId.GetAllParticipants()
|
|
c.WriteJSON(&participantData)
|
|
continue
|
|
case "changeAbcProtection":
|
|
if err := participantHandlerByExcersiseId.ChangeAbcProtection(req.ParticipantData.ParticipantId, req.ParticipantData.AbcProtection); err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
c.WriteJSON(req.ParticipantData)
|
|
continue
|
|
case "changePosition":
|
|
if err := participantHandlerByExcersiseId.ChangePosition(req.ParticipantData.ParticipantId, &participanthandler.GeoPosition{
|
|
Lat: req.ParticipantData.Positions[0].Lat,
|
|
Lng: req.ParticipantData.Positions[0].Lng,
|
|
}); err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
}
|