//go:build ignore // +build ignore package main import ( "log" "time" "git.me9.top/git/tinymq" "git.me9.top/git/tinymq/config" ) func main() { cf := config.NewConfig() localChannel := "/tinymq/client/tcp2" remoteChannel := "/tinymq/server" remoteFilter := tinymq.StrChannelFilter(remoteChannel) host := &tinymq.HostInfo{ Proto: "tcp", Version: 2, Host: "127.0.0.1", Port: 34222, Hash: "xor:1qaz2wsx3", } hub := tinymq.NewHub( cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) { return host, nil }, func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) { log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth)) return []byte("tinymq-client") }, func(client bool, proto string, version uint8, channel string, auth []byte) bool { log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth)) return string(auth) == "tinymq-server" }, func(conn *tinymq.Line) { log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated())) }, ) hub.SetFilterToChannelFunc(func(filter tinymq.FilterFunc) (channel string) { return remoteChannel }) // 订阅频道 hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) { log.Println("[client RECV]<-", string(request.Data)) return 1, "tiny client" }, ) hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) { 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(remoteFilter, "hello", []byte("hello from client, hello from client, 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") }