2024-01-28 22:04:40 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ectrc/snow/aid"
|
|
|
|
"github.com/ectrc/snow/socket"
|
|
|
|
"github.com/gofiber/contrib/websocket"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MiddlewareWebsocket(c *fiber.Ctx) error {
|
|
|
|
if !websocket.IsWebSocketUpgrade(c) {
|
|
|
|
return fiber.ErrUpgradeRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
var protocol string
|
|
|
|
|
|
|
|
switch c.Get("Sec-WebSocket-Protocol") {
|
|
|
|
case "xmpp":
|
|
|
|
protocol = "jabber"
|
|
|
|
default:
|
|
|
|
protocol = "matchmaking"
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:14:14 +00:00
|
|
|
c.Locals("identifier", "ws-"+aid.RandomString(8))
|
2024-01-28 22:04:40 +00:00
|
|
|
c.Locals("protocol", protocol)
|
|
|
|
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func WebsocketConnection(c *websocket.Conn) {
|
|
|
|
protocol := c.Locals("protocol").(string)
|
|
|
|
identifier := c.Locals("identifier").(string)
|
|
|
|
|
|
|
|
switch protocol {
|
|
|
|
case "jabber":
|
|
|
|
socket.JabberSockets.Set(identifier, socket.NewJabberSocket(c, identifier, socket.JabberData{}))
|
|
|
|
socket.HandleNewJabberSocket(identifier)
|
|
|
|
case "matchmaking":
|
|
|
|
// socket.MatchmakerSockets.Set(identifier, socket.NewMatchmakerSocket(c, socket.MatchmakerData{}))
|
|
|
|
default:
|
|
|
|
aid.Print("Invalid protocol: " + protocol)
|
|
|
|
}
|
2024-01-29 23:46:22 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 20:29:02 +00:00
|
|
|
func GetSnowConnectedSockets(c *fiber.Ctx) error {
|
2024-01-31 16:21:19 +00:00
|
|
|
jabber := aid.JSON{}
|
2024-01-29 23:46:22 +00:00
|
|
|
socket.JabberSockets.Range(func(key string, value *socket.Socket[socket.JabberData]) bool {
|
2024-01-31 16:21:19 +00:00
|
|
|
jabber[key] = value
|
2024-01-29 23:46:22 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2024-01-31 16:21:19 +00:00
|
|
|
matchmaking := aid.JSON{}
|
2024-01-29 23:46:22 +00:00
|
|
|
socket.MatchmakerSockets.Range(func(key string, value *socket.Socket[socket.MatchmakerData]) bool {
|
2024-01-31 16:21:19 +00:00
|
|
|
matchmaking[key] = value
|
2024-01-29 23:46:22 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return c.Status(200).JSON(aid.JSON{
|
|
|
|
"jabber": jabber,
|
|
|
|
"matchmaking": matchmaking,
|
|
|
|
})
|
2024-01-28 22:04:40 +00:00
|
|
|
}
|