Implement map for socket type

This commit is contained in:
Eccentric 2024-01-20 23:08:29 +00:00
parent b935f4798c
commit 4b6f49846c
4 changed files with 47 additions and 22 deletions

View File

@ -103,11 +103,6 @@ func getRandomCharacterImage() image.Image {
continue continue
} }
// newDisplayAsset := "DAv2_" + strings.ReplaceAll(character.ID, "Athena_Commando_", "")
// if !KnownDisplayAssets[newDisplayAsset] {
// continue
// }
found = true found = true
} }

View File

@ -8,13 +8,24 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
type SocketType string
var (
SocketTypeXmpp SocketType = "xmpp"
SocketTypeUnknown SocketType = "unknown"
)
type Socket struct { type Socket struct {
ID string ID string
Type SocketType
Connection *websocket.Conn Connection *websocket.Conn
Person *person.Person Person *person.Person
} }
var ( var (
handles = map[SocketType]func(string) {
SocketTypeXmpp: handlePresenceSocket,
}
sockets = aid.GenericSyncMap[Socket]{} sockets = aid.GenericSyncMap[Socket]{}
) )
@ -23,34 +34,33 @@ func MiddlewareWebsocket(c *fiber.Ctx) error {
return fiber.ErrUpgradeRequired 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("uuid", uuid.New().String())
c.Locals("protocol", protocol)
return c.Next() return c.Next()
} }
func WebsocketConnection(c *websocket.Conn) { func WebsocketConnection(c *websocket.Conn) {
protocol := c.Locals("protocol").(string) protocol := c.Locals("protocol").(SocketType)
uuid := c.Locals("uuid").(string) uuid := c.Locals("uuid").(string)
sockets.Add(uuid, &Socket{ sockets.Add(uuid, &Socket{
ID: uuid, ID: uuid,
Type: protocol,
Connection: c, Connection: c,
}) })
defer close(uuid) defer close(uuid)
switch protocol { if handle, ok := handles[protocol]; ok {
case "xmpp": handle(uuid)
aid.Print("(xmpp) new connection: ", uuid) }
default:
aid.Print("(unknown) new connection: ", uuid)
}
for {
_, _, err := c.ReadMessage()
if err != nil {
break
}
}
} }
func close(id string) { func close(id string) {

20
handlers/xmpp.go Normal file
View File

@ -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))
}
}

View File

@ -61,10 +61,10 @@ func main() {
r.Use(aid.FiberLimiter()) r.Use(aid.FiberLimiter())
r.Use(aid.FiberCors()) 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.Get("/region", handlers.GetRegion)
r.Put("/profile/play_region", handlers.AnyNoContent) 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.Get("/api/v1/search/:accountId", handlers.GetPersonSearch)
r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets) r.Post("/api/v1/assets/Fortnite/:versionId/:assetName", handlers.PostAssets)