Thomas van Weert
9edcb83fc4
CHG: use concurrent map instead of regular map with lock DEL: global session control, moved to per server only DEL: unused command system
62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package user
|
|
|
|
import (
|
|
"github.com/Mindgamesnl/socketio"
|
|
"github.com/ParadoxPixel/ThemePark-Websocket/data"
|
|
"github.com/ParadoxPixel/ThemePark-Websocket/objects"
|
|
"log"
|
|
"sync"
|
|
)
|
|
|
|
func serverLogin(so socketio.Socket) {
|
|
id := so.GetQuery().Get("id")
|
|
if !data.CanServer(id) {
|
|
if debug {
|
|
log.Println(
|
|
"Server with id:",
|
|
so.GetQuery().Get("id"),
|
|
"already connected",
|
|
)
|
|
}
|
|
|
|
_ = so.Close()
|
|
return
|
|
}
|
|
|
|
data.AddServer(&objects.Server{
|
|
ID: id,
|
|
Sessions: make(map[string]socketio.Socket),
|
|
Attraction: make(map[string]string),
|
|
Socket: so,
|
|
Mux: new(sync.RWMutex),
|
|
})
|
|
if debug {
|
|
log.Println(
|
|
"Server with id:",
|
|
so.GetQuery().Get("id"),
|
|
"connected",
|
|
)
|
|
}
|
|
}
|
|
|
|
func serverQuit(so socketio.Socket) {
|
|
session := so.GetQuery().Get("id")
|
|
obj := data.RemoveServer(session)
|
|
if obj == nil {
|
|
return
|
|
}
|
|
|
|
if debug {
|
|
log.Println(
|
|
"Server with id:",
|
|
so.GetQuery().Get("id"),
|
|
"disconnected",
|
|
)
|
|
}
|
|
|
|
// Disconnect users
|
|
for _, user := range obj.Sessions {
|
|
_ = user.Emit("close", "SERVER_ERROR")
|
|
_ = user.Close()
|
|
}
|
|
}
|