client-tcp2-proxy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/proxy"
  14. remoteChannel := "/tinymq/server"
  15. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  16. hostProxy := &conn.HostInfo{
  17. Proto: "tcp",
  18. Version: 2,
  19. Host: "127.0.0.1",
  20. Port: 44222,
  21. Hash: "xor:1qaz2wsx3",
  22. Proxy: true,
  23. }
  24. hostServer := &conn.HostInfo{
  25. Proto: "tcp",
  26. Version: 2,
  27. Host: "127.0.0.1",
  28. Port: 34222,
  29. Hash: "xor:1qaz2wsx3",
  30. }
  31. hub := tinymq.NewHub(
  32. cf,
  33. localChannel,
  34. func(channel string, hostType tinymq.HostType) (hostInfo *conn.HostInfo, err error) {
  35. log.Println("[connectHostFunc]", channel, hostType)
  36. if hostType == tinymq.HostTypeDirect {
  37. return hostServer, nil
  38. }
  39. return hostProxy, nil
  40. },
  41. func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  42. log.Println("[AuthFunc]", host, proto, version, channel, string(remoteAuth))
  43. return []byte("tinymq-client")
  44. },
  45. func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool {
  46. log.Println("[CheckAuthFunc]", host, proto, version, channel, string(auth))
  47. return string(auth) == "tinymq-server"
  48. },
  49. func(conn *tinymq.Line) {
  50. log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Started()))
  51. },
  52. )
  53. // 从过滤器到频道字符串
  54. hub.SetFilterToChannelFunc(func(filter tinymq.FilterFunc) (channel string) {
  55. return remoteChannel
  56. })
  57. // 订阅频道
  58. hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {
  59. log.Println("[client RECV]<-", string(request.Data))
  60. return 1, "tiny client"
  61. },
  62. )
  63. hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
  64. log.Println("[client RECV]<-", string(request.Data))
  65. return 1, nil
  66. },
  67. )
  68. // err := hub.ConnectToServer("/tinymq/server", true, nil)
  69. // if err != nil {
  70. // log.Fatalln("[client ConnectToServer ERROR]", err)
  71. // }
  72. // 获取信息
  73. rsp := hub.GetOne(remoteFilter, "hello", []byte("hello from client, hello from client, hello from client"))
  74. if rsp.State != tinymq.STATE_OK {
  75. log.Println("error state:", rsp.State)
  76. return
  77. }
  78. log.Println("[RESULT]<-", string(rsp.Data))
  79. log.Println("test finished")
  80. time.Sleep(time.Second * 10)
  81. log.Println("client exit")
  82. }