-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (39 loc) · 979 Bytes
/
main.go
File metadata and controls
41 lines (39 loc) · 979 Bytes
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
package main
import (
"context"
"github.com/Li-giegie/node"
"github.com/Li-giegie/node/pkg/client"
"github.com/Li-giegie/node/pkg/conn"
"github.com/Li-giegie/node/pkg/message"
"github.com/Li-giegie/node/pkg/reply"
"log"
)
func main() {
c := node.NewClientOption(2, 1)
client.OnMessage(func(r *reply.Reply, m *message.Message) (next bool) {
log.Println(m.String())
r.Write(message.StateCode_Success, m.Data)
return true
})
client.OnClose(func(conn *conn.Conn, err error) (next bool) {
log.Println("OnClose", err)
return true
})
err := c.Connect("tcp://127.0.0.1:7890", nil)
if err != nil {
log.Fatal("connect err:", err)
return
}
log.Println("connect: 7890")
defer c.Close()
if err = c.Send([]byte("hello")); err != nil {
log.Println("send err:", err)
return
}
code, data, err := c.Request(context.TODO(), []byte("world"))
if err != nil {
log.Println("request err:", err)
return
}
log.Println("code:", code, "data:", string(data))
}