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
}
// newDisplayAsset := "DAv2_" + strings.ReplaceAll(character.ID, "Athena_Commando_", "")
// if !KnownDisplayAssets[newDisplayAsset] {
// continue
// }
found = true
}

View File

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

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.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)