//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/tcp2" host := &tinymq.HostInfo{ Proto: "tcp", Version: 2, Host: "127.0.0.1", Port: 34222, Hash: "xor:1qaz2wsx3", } hub := tinymq.NewHub(cf, channel, func(channel string, proxy bool) (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.Channel(), conn.State(), time.Since(conn.Updated())) }) // 订阅频道 hub.Subscribe(regexp.MustCompile("/tinymq/server"), "hello", func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[client RECV]<-", string(request.Data)) return 1, []byte("tiny client") }, ) hub.Subscribe(regexp.MustCompile("/tinymq/server"), "nodata", func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[client RECV]<-", string(request.Data)) return 1, nil }, ) err := hub.ConnectToServer("/tinymq/server", true, nil) if err != nil { log.Fatalln("[client ConnectToServer ERROR]", err) } // 获取信息 rsp := hub.GetOne(regexp.MustCompile("/tinymq/server"), "hello", []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") }