3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Socket/handlers/data/handler.go

67 lines
1.4 KiB
Go
Raw Normal View History

2022-08-12 19:08:47 +00:00
package data
import (
"github.com/Mindgamesnl/socketio"
"github.com/ParadoxPixel/ThemePark-Websocket/data"
"github.com/ParadoxPixel/ThemePark-Websocket/objects"
"log"
"strings"
)
func Load(namespace socketio.Namespace, debug bool) {
namespace.OnEvent("data", func(so socketio.Socket, msg string) {
isServer := so.GetQuery().Get("type") == "server"
2022-08-12 19:08:47 +00:00
var ok bool
var server *objects.Server
var err error
if !isServer {
2022-08-12 19:08:47 +00:00
ok, server = data.HasServer(so.GetQuery().Get("control_id"))
if !ok {
if debug {
log.Println(
"No server found with id:",
so.GetQuery().Get("control_id"),
"for client:",
so.GetQuery().Get("token"),
)
}
return
}
err = server.Socket.Emit("data", msg)
if err != nil && debug {
log.Println("error while sending data to server:", err)
}
2022-08-12 19:08:47 +00:00
return
}
ok, server = data.HasServer(so.GetQuery().Get("id"))
if msg[0] == '{' {
err = server.Socket.EmitError("messages can only be send to clients")
if err != nil && debug {
log.Println("error while sending data to server:", err)
2022-08-12 19:08:47 +00:00
}
return
}
2022-08-12 19:08:47 +00:00
args := strings.SplitN(msg, "@", 2)
ok, session := server.HasSession(args[0])
if !ok {
if debug {
log.Println(
"Unable to send packet from server:",
server.ID,
"to client:",
args[0],
)
}
2022-08-12 19:08:47 +00:00
return
}
err = session.Emit("data", args[1])
if err != nil && debug {
log.Println("error while sending data to server:", err)
}
2022-08-12 19:08:47 +00:00
})
}