//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/ws2" remoteChannel := "/tinymq/server" remoteFilter := tinymq.StrChannelFilter(remoteChannel) host := &tinymq.HostInfo{ Proto: "ws", Version: 2, Host: "127.0.0.1", Port: 34211, // Path: "/tinymq", Path: "/tinymq-xor", Hash: "xor:1qaz2wsx3edc", } 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.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(remoteChannel, true, nil) if err != nil { log.Fatalln("[client ConnectToServer ERROR]", err) } // 获取信息 rsp := hub.GetOne(remoteFilter, "hello", "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)) rsp = hub.GetOne(remoteFilter, "hello", func() ([]byte, error) { return []byte("hello from data function"), nil }) if rsp.State != config.STATE_OK { log.Println("error state:", rsp.State) return } log.Println("[RESULT]<-", string(rsp.Data)) // 获取长数据 rsp = hub.GetOne(remoteFilter, "bigdata", nil) if rsp.State != config.STATE_OK { log.Println("error state:", rsp.State) return } else { log.Println("get bigdata ok") } time.Sleep(time.Second * 5) hub.Push(remoteFilter, "push", []byte(time.Now().GoString())) time.Sleep(time.Second * 300) log.Println("client exit") }