client-tcp2.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //go:build ignore
  2. // +build ignore
  3. package main
  4. import (
  5. "log"
  6. "time"
  7. "git.me9.top/git/tinymq"
  8. "git.me9.top/git/tinymq/config"
  9. "git.me9.top/git/tinymq/conn"
  10. )
  11. func main() {
  12. cf := config.NewConfig()
  13. localChannel := "/tinymq/client/tcp2"
  14. remoteChannel := "/tinymq/server"
  15. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  16. host := &conn.HostInfo{
  17. Proto: "tcp",
  18. Version: 2,
  19. Host: "127.0.0.1",
  20. Port: 34222,
  21. Hash: "xor:1qaz2wsx3",
  22. }
  23. hub := tinymq.NewHub(
  24. cf,
  25. localChannel,
  26. func(channel string, hostType tinymq.HostType) (hostInfo *conn.HostInfo, err error) {
  27. return host, nil
  28. },
  29. func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  30. log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
  31. return []byte("tinymq-client")
  32. },
  33. func(client bool, proto string, version uint8, channel string, auth []byte) bool {
  34. log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
  35. return string(auth) == "tinymq-server"
  36. },
  37. func(conn *tinymq.Line) {
  38. log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  39. },
  40. )
  41. hub.SetFilterToChannelFunc(func(filter tinymq.FilterFunc) (channel string) {
  42. return remoteChannel
  43. })
  44. // 订阅频道
  45. hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {
  46. log.Println("[client RECV]<-", string(request.Data))
  47. return 1, "tiny client"
  48. },
  49. )
  50. hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
  51. log.Println("[client RECV]<-", string(request.Data))
  52. return 1, nil
  53. },
  54. )
  55. // err := hub.ConnectToServer("/tinymq/server", true, nil)
  56. // if err != nil {
  57. // log.Fatalln("[client ConnectToServer ERROR]", err)
  58. // }
  59. // 获取信息
  60. rsp := hub.GetOne(remoteFilter, "hello", []byte("hello from client, hello from client, hello from client"))
  61. if rsp.State != tinymq.STATE_OK {
  62. log.Println("error state:", rsp.State)
  63. return
  64. }
  65. log.Println("[RESULT]<-", string(rsp.Data))
  66. time.Sleep(time.Second * 30)
  67. log.Println("client exit")
  68. }