forked from chanbakjsd/TerraOceanPlugin
53 lines
968 B
Go
53 lines
968 B
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
|
|
}
|
|
|
|
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")
|
|
}
|