/
client.go
67 lines (57 loc) · 1.34 KB
/
client.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/obsilp/rmnp"
)
var debug = log.New(os.Stdout, "\x1b[36m[DEBU]\x1b[0m ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
var sendChannel = make(chan sendmsg, 1000)
type sendmsg struct {
msgType uint8
msgData []byte
}
func main() {
client := rmnp.NewClient("127.0.0.1:10001")
client.ConnectWithData([]byte{0, 1, 2})
client.ServerConnect = serverConnect
client.ServerTimeout = serverTimeout
client.PacketHandler = handleClientPacket
// client.ServerDisconnect = serverDisconnect
sendText()
select {}
}
func serverConnect(conn *rmnp.Connection, data []byte) {
debug.Println("connected to server")
var msg sendmsg
for {
msg = <-sendChannel
switch msg.msgType {
case 0:
conn.SendUnreliable(msg.msgData)
case 1:
case 2:
case 3:
conn.SendReliableOrdered(msg.msgData)
}
}
}
func serverDisconnect(conn *rmnp.Connection, data []byte) {
fmt.Println("disconnected from server:", string(data))
}
func serverTimeout(conn *rmnp.Connection, data []byte) {
fmt.Println("server timeout")
}
func handleClientPacket(conn *rmnp.Connection, data []byte, channel rmnp.Channel) {
fmt.Println("'"+string(data)+"'", "on channel", channel)
}
func sendText() {
var newMsg = &sendmsg{}
for {
newMsg.msgType = 0
newMsg.msgData = []byte("PING")
sendChannel <- *newMsg
time.Sleep(60e6)
}
}