//go:build ignore // +build ignore package main import ( "log" "regexp" "time" "git.me9.top/git/tinymq" "git.me9.top/git/tinymq/config" ) func main() { cf := config.NewConfig() channel := "/tinymq/client/tpv2" host := &tinymq.HostInfo{ Proto: "tp", Version: 2, Host: "127.0.0.1", Port: 34222, Hash: "xor:1qaz2wsx3", } hub := tinymq.NewHub(cf, channel, func(channel string) (hostInfo *tinymq.HostInfo, err error) { return host, nil }, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) { // 从 remoteAuth 是否为空来判断是否需要返回信息 if len(remoteAuth) <= 0 { // 客户端调用,返回验证信息 return []byte("tinymq") } else { // 服务端调用,返回验证token,或者其他信息 return nil } }, func(proto string, version uint8, channel string, auth []byte) bool { return true }, func(conn *tinymq.Line) { log.Println("connect state", conn.State()) }) // 订阅频道 hub.Subscribe(&tinymq.SubscribeData{ Channel: regexp.MustCompile("/tinymq/server"), Cmd: "hello", BackFunc: func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[client RECV]<-", string(request.Data)) return 1, []byte("tiny client") }, }) hub.Subscribe(&tinymq.SubscribeData{ Channel: regexp.MustCompile("/tinymq/server"), Cmd: "nodata", BackFunc: func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[client RECV]<-", string(request.Data)) return 1, nil }, }) err := hub.ConnectToServer("/tinymq/server", true) if err != nil { log.Fatalln("[client ConnectToServer ERROR]", err) } // 获取信息 rsp := hub.GetOne(&tinymq.GetData{ Channel: regexp.MustCompile("/tinymq/server"), Cmd: "hello", Data: []byte("hello from client"), }) if rsp.State != config.STATE_OK { log.Println("error state:", rsp.State) return } log.Println("[RESULT]<-", string(rsp.Data)) time.Sleep(time.Second * 300) log.Println("client exit") }