forked from fwhezfwhez/tcpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
62 lines (50 loc) · 942 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
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"net"
"github.com/fwhezfwhez/errorx"
//"tcpx"
"github.com/CocoKelam/tcpx"
)
func main() {
message := tcpx.NewURLPatternMessage("/login/", map[string]interface{}{
"username": "Li Hua",
})
b, e := message.Pack(tcpx.JsonMarshaller{})
if e != nil {
panic(e)
}
conn, e := net.Dial("tcp", "localhost:7071")
if e != nil {
panic(e)
return
}
go Recv(conn)
conn.Write(b)
select {}
}
func Recv(conn net.Conn) {
for {
block, e := tcpx.Recv(conn)
if e != nil {
fmt.Println(errorx.Wrap(e).Error())
return
}
urlPattern, e := block.URLPattern()
if e != nil {
fmt.Println(errorx.Wrap(e).Error())
return
}
switch urlPattern {
case "/login/":
type LoginResponse struct {
Token string `json:"token"`
}
var lr LoginResponse
block.BindJSON(&lr)
fmt.Printf("recv login token: %s\n", lr.Token)
default:
fmt.Println("unexpected urlPattern")
}
}
}