Document of RCON Connection

This commit is contained in:
Tnze
2021-11-23 15:46:01 +08:00
parent 9d0a48832f
commit b1b57e067b
2 changed files with 74 additions and 0 deletions

View File

@ -69,3 +69,69 @@ func client(t *testing.T) {
}
t.Logf("Server response: %q", resp)
}
func ExampleListenRCON() {
l, err := ListenRCON("localhost:25575")
if err != nil {
panic(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Printf("Accept connection error: %v", err)
}
go func(conn RCONServerConn) {
err = conn.AcceptLogin("CORRECT_PASSWORD")
if err != nil {
fmt.Printf("Login fail: %v", err)
}
defer conn.Close()
// The client is login, we are accepting its command
for {
cmd, err := conn.AcceptCmd()
if err != nil {
fmt.Printf("Read command fail: %v", err)
break
}
resp := handleCommand(cmd)
// Return the result of command.
// It's allowed to call RespCmd multiple times for one command.
err = conn.RespCmd(resp)
if err != nil {
fmt.Printf("Response command fail: %v", err)
break
}
}
}(conn)
}
}
func ExampleDialRCON() {
conn, err := DialRCON("localhost:25575", "CORRECT_PASSWORD")
if err != nil {
panic(err)
}
defer conn.Close()
err = conn.Cmd("TEST COMMAND")
if err != nil {
panic(err)
}
for {
// Server may send the result in more(or less) than one packet.
// See: https://wiki.vg/RCON#Fragmentation
resp, err := conn.Resp()
if err != nil {
fmt.Print(err)
}
fmt.Printf("Server response: %q", resp)
break
}
}