TerraOceanPlugin/GoBot/server/hub.go

65 lines
1.2 KiB
Go

2019-10-12 14:07:40 +07:00
package server
import (
"fmt"
"strings"
"github.com/gorilla/websocket"
)
2019-10-12 14:07:40 +07:00
var clients = make(map[*websocket.Conn]bool)
var connectedUsers = make(map[string]bool)
2019-10-12 14:07:40 +07:00
func newSession(who *websocket.Conn) {
clients[who] = true
}
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]] {
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")
2019-10-12 14:07:40 +07:00
}
func VoteNotification() {
broadcast("vote")
}
func WhitelistAdd(minecraftUsername string) {
broadcast("whitelistadd " + minecraftUsername)
}
func WhitelistRemove(minecraftUsername string) {
broadcast("whitelistremove " + minecraftUsername)
}