TerraOceanPlugin/GoBot/server/hub.go

68 lines
1.3 KiB
Go

package server
import (
"fmt"
"strings"
"github.com/gorilla/websocket"
)
var clients = make(map[*websocket.Conn]bool)
var connectedUsers = make(map[string]bool)
func newSession(who *websocket.Conn) {
clients[who] = true
who.WriteMessage(websocket.TextMessage, []byte("sync"))
}
func disconnectSession(who *websocket.Conn) {
delete(clients, who)
}
func handleIncomingMessage(who *websocket.Conn, msg string) {
message := strings.SplitN(msg, " ", 2)
switch message[0] {
case "query":
if len(message) < 2 {
return
}
if !connectedUsers[message[1]] {
fmt.Println(message[1], "tried to connect. Disconnecting!")
fmt.Println(connectedUsers)
who.WriteMessage(websocket.TextMessage, []byte("leave "+message[1]))
}
default:
fmt.Println("Unknown message:" + msg)
}
}
func broadcast(message string) {
for k, _ := range clients {
k.WriteMessage(websocket.TextMessage, []byte(message))
}
}
func ConnectUser(mcUsername string) {
connectedUsers[mcUsername] = true
}
func DisconnectUser(mcUsername string) {
delete(connectedUsers, mcUsername)
}
func RequestSync() {
broadcast("sync")
}
func VoteNotification() {
broadcast("vote")
}
func WhitelistAdd(minecraftUsername string) {
broadcast("whitelistadd " + minecraftUsername)
}
func WhitelistRemove(minecraftUsername string) {
broadcast("whitelistremove " + minecraftUsername)
}