Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.

Commit

Permalink
fix dagame
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusan Malusev committed Mar 13, 2022
1 parent e243b0d commit 7a7f122
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions backend/pkg/http/handlers/ws/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"github.com/dustin/go-broadcast"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"github.com/rs/zerolog"
)

type LedgerRequest struct {
EventID uint64 `json:"event_id"`
}

func LedgerHandler(caster broadcast.Broadcaster) fiber.Handler {
func LedgerHandler(caster broadcast.Broadcaster, logger zerolog.Logger) fiber.Handler {
return websocket.New(func(c *websocket.Conn) {
eventIdStr := c.Params("eventId")

eventId, err := strconv.ParseUint(eventIdStr, 10, 64)

if err != nil {
Expand All @@ -26,6 +26,9 @@ func LedgerHandler(caster broadcast.Broadcaster) fiber.Handler {
return
}


logger.Debug().Uint64("event_id", eventId).Msg("Event Subscribed")

ch := make(chan interface{})

caster.Register(ch)
Expand All @@ -34,9 +37,19 @@ func LedgerHandler(caster broadcast.Broadcaster) fiber.Handler {

for item := range ch {
ledgerAndPoints := item.(ledger.Diff)
logger.Debug().
Uint64("ledger_id", ledgerAndPoints.Ledger.EventID).
Uint64("diff", ledgerAndPoints.Diff).
Msg("ledger sent")

if ledgerAndPoints.Ledger.EventID == eventId {
c.WriteJSON(ledgerAndPoints)
logger.Debug().
Uint64("ledger_id", ledgerAndPoints.Ledger.EventID).
Uint64("diff", ledgerAndPoints.Diff).
Msg("ledger sent to client")
if err := c.WriteJSON(ledgerAndPoints);err != nil {
logger.Error().Err(err).Msg("Error sending to client")
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func registerRoutes(c *container.Container, app *fiber.App) {
app.Get("/user-profile", authMiddleware, handlers.UserProfileMetrics(c.GetLedgerService()))
app.Post("/ledger", authMiddleware, handlers.InsertLedger(c.GetLedgerService(), c.GetBroadCaster()))
app.Post("/event/:eventId/participate", authMiddleware, events.InsertParticipent(c.GetParticipantService()))
app.Get("/ws/:eventId/collection", authMiddleware, middleware.WebSocket(), ws.LedgerHandler(c.GetBroadCaster()))
app.Get("/ws/:eventId/collection", authMiddleware, middleware.WebSocket(), ws.LedgerHandler(c.GetBroadCaster(), c.GetDefaultLogger()))

app.Get("/events", authMiddleware, events.GetEventHandler(c.GetEventService(), c.GetDefaultLogger()))
app.Get("/event/:eventId/participate",authMiddleware, events.ParticipantHandler())
Expand Down

0 comments on commit 7a7f122

Please sign in to comment.