-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.go
52 lines (40 loc) · 1.09 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"github.com/obsilp/rmnp"
"net"
"fmt"
)
func main() {
server := rmnp.NewServer(":10001")
server.ClientConnect = clientConnect
server.ClientDisconnect = clientDisconnect
server.ClientTimeout = clientTimeout
server.ClientValidation = validateClient
server.PacketHandler = handleServerPacket
server.Start()
fmt.Println("server started")
select {}
}
func clientConnect(conn *rmnp.Connection, data []byte) {
fmt.Println("client connection with:", data)
if data[0] != 0 {
conn.Disconnect([]byte("not allowed"))
}
}
func clientDisconnect(conn *rmnp.Connection, data []byte) {
fmt.Println("client disconnect")
}
func clientTimeout(conn *rmnp.Connection, data []byte) {
fmt.Println("client timeout")
}
func validateClient(addr *net.UDPAddr, data []byte) bool {
return len(data) == 3
}
func handleServerPacket(conn *rmnp.Connection, data []byte, channel rmnp.Channel) {
str := string(data)
fmt.Println("'"+str+"'", "from", conn.Addr.String(), "on channel", channel)
if str == "ping" {
conn.SendReliableOrdered([]byte("pong"))
conn.Disconnect([]byte("session end"))
}
}