From 4b6f49846c8733e1b4e7a73979268afe6e1d1a2a Mon Sep 17 00:00:00 2001 From: Eccentric Date: Sat, 20 Jan 2024 23:08:29 +0000 Subject: [PATCH] Implement map for socket type --- fortnite/image.go | 5 ----- handlers/socket.go | 40 +++++++++++++++++++++++++--------------- handlers/xmpp.go | 20 ++++++++++++++++++++ main.go | 4 ++-- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 handlers/xmpp.go diff --git a/fortnite/image.go b/fortnite/image.go index cc2f4c9..5b57cf0 100644 --- a/fortnite/image.go +++ b/fortnite/image.go @@ -103,11 +103,6 @@ func getRandomCharacterImage() image.Image { continue } - // newDisplayAsset := "DAv2_" + strings.ReplaceAll(character.ID, "Athena_Commando_", "") - // if !KnownDisplayAssets[newDisplayAsset] { - // continue - // } - found = true } diff --git a/handlers/socket.go b/handlers/socket.go index 7e86540..8cfd9a9 100644 --- a/handlers/socket.go +++ b/handlers/socket.go @@ -8,13 +8,24 @@ import ( "github.com/google/uuid" ) +type SocketType string +var ( + SocketTypeXmpp SocketType = "xmpp" + SocketTypeUnknown SocketType = "unknown" +) + type Socket struct { ID string + Type SocketType Connection *websocket.Conn Person *person.Person } var ( + handles = map[SocketType]func(string) { + SocketTypeXmpp: handlePresenceSocket, + } + sockets = aid.GenericSyncMap[Socket]{} ) @@ -23,34 +34,33 @@ func MiddlewareWebsocket(c *fiber.Ctx) error { return fiber.ErrUpgradeRequired } - c.Locals("protocol", c.Get("Sec-WebSocket-Protocol")) + var protocol SocketType + switch c.Get("Sec-WebSocket-Protocol") { + case "xmpp": + protocol = SocketTypeXmpp + default: + protocol = SocketTypeUnknown + } + c.Locals("uuid", uuid.New().String()) + c.Locals("protocol", protocol) return c.Next() } func WebsocketConnection(c *websocket.Conn) { - protocol := c.Locals("protocol").(string) + protocol := c.Locals("protocol").(SocketType) uuid := c.Locals("uuid").(string) sockets.Add(uuid, &Socket{ ID: uuid, + Type: protocol, Connection: c, }) defer close(uuid) - switch protocol { - case "xmpp": - aid.Print("(xmpp) new connection: ", uuid) - default: - aid.Print("(unknown) new connection: ", uuid) - } - - for { - _, _, err := c.ReadMessage() - if err != nil { - break - } - } + if handle, ok := handles[protocol]; ok { + handle(uuid) + } } func close(id string) { diff --git a/handlers/xmpp.go b/handlers/xmpp.go new file mode 100644 index 0000000..9946f5f --- /dev/null +++ b/handlers/xmpp.go @@ -0,0 +1,20 @@ +package handlers + +import "github.com/ectrc/snow/aid" + +func handlePresenceSocket(id string) { + aid.Print("(xmpp) connection opened", id) + socket, ok := sockets.Get(id) + if !ok { + return + } + + for { + _, msg, err := socket.Connection.ReadMessage() + if err != nil { + aid.Print("(xmpp) error reading message", err) + break + } + aid.Print("(xmpp) message received", string(msg)) + } +} \ No newline at end of file diff --git a/main.go b/main.go index 96712c5..4563416 100644 --- a/main.go +++ b/main.go @@ -61,10 +61,10 @@ func main() { r.Use(aid.FiberLimiter()) r.Use(aid.FiberCors()) - r.Get("/content/api/pages/fortnite-game", handlers.GetContentPages) - r.Get("/waitingroom/api/waitingroom", handlers.GetWaitingRoomStatus) r.Get("/region", handlers.GetRegion) r.Put("/profile/play_region", handlers.AnyNoContent) + r.Get("/content/api/pages/fortnite-game", handlers.GetContentPages) + r.Get("/waitingroom/api/waitingroom", handlers.GetWaitingRoomStatus) r.Get("/api/v1/search/:accountId", handlers.GetPersonSearch) r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets)