forked from fwhezfwhez/tcpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
52 lines (41 loc) · 731 Bytes
/
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
package main
import (
"fmt"
"os"
"net"
"github.com/CocoKelam/tcpx"
//"tcpx"
"time"
)
func main() {
conn, e := net.Dial("tcp", "localhost:8102")
if e != nil {
panic(e)
}
go Recv(conn)
onlineBuf, e := tcpx.PackWithMarshaller(tcpx.Message{
MessageID: 1,
Header: nil,
Body: struct {
Username string `json:"username"`
}{"ft"},
}, tcpx.JsonMarshaller{})
offlineBuf := tcpx.PackStuff(3)
conn.Write(onlineBuf)
time.Sleep(5 * time.Second)
conn.Write(offlineBuf)
select {}
}
func Recv(conn net.Conn) {
var buf = make([]byte, 500)
for {
n, e := conn.Read(buf)
if e != nil {
os.Exit(0)
break
}
fmt.Println(buf[:n])
bf, _ := tcpx.BodyBytesOf(buf[:n])
fmt.Println(string(bf))
}
}