client-ws2-proxy.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/ws2/proxy"
  14. remoteChannel := "/tinymq/server"
  15. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  16. hostProxy := &conn.HostInfo{
  17. Proto: "ws",
  18. Version: 2,
  19. Host: "127.0.0.1",
  20. Port: 44211,
  21. // Path: "/tinymq",
  22. Path: "/tinymq-xor",
  23. Hash: "xor:1qaz2wsx3edc",
  24. Proxy: true,
  25. }
  26. hostServer := &conn.HostInfo{
  27. Proto: "ws",
  28. Version: 2,
  29. Host: "127.0.0.1",
  30. Port: 34211,
  31. // Path: "/tinymq",
  32. Path: "/tinymq-xor",
  33. Hash: "xor:1qaz2wsx3edc",
  34. }
  35. hub := tinymq.NewHub(
  36. cf,
  37. localChannel,
  38. func(channel string, hostType tinymq.HostType) (hostInfo *conn.HostInfo, err error) {
  39. log.Println("[connectHostFunc]", channel, hostType)
  40. if hostType == tinymq.HostTypeDirect {
  41. return hostServer, nil
  42. }
  43. return hostProxy, nil
  44. },
  45. func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  46. log.Println("[AuthFunc]", host, proto, version, channel, string(remoteAuth))
  47. return []byte("tinymq-client")
  48. },
  49. func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool {
  50. log.Println("[CheckAuthFunc]", host, proto, version, channel, string(auth))
  51. return string(auth) == "tinymq-server"
  52. },
  53. func(conn *tinymq.Line) {
  54. log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Started()))
  55. },
  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(remoteChannel, true, nil, false)
  69. if err != nil {
  70. log.Fatalln("[client ConnectToServer ERROR]", err)
  71. }
  72. log.Println("start get data >>>")
  73. count, err := hub.Get(remoteFilter, "hello", "hello in get model", func(response *tinymq.ResponseData) (ok bool) {
  74. log.Println("get state and data: ", response.State, string(response.Data))
  75. return true
  76. })
  77. log.Println("end get data with count and err:", count, err)
  78. // 获取信息
  79. rsp := hub.GetOne(remoteFilter, "hello", "hello from client,hello from client,hello from client")
  80. if rsp.State != tinymq.STATE_OK {
  81. log.Println("error state:", rsp.State)
  82. return
  83. }
  84. log.Println("[RESULT]<-", string(rsp.Data))
  85. rsp = hub.GetOne(remoteFilter, "hello", func() ([]byte, error) {
  86. return []byte("hello from data function"), nil
  87. })
  88. if rsp.State != tinymq.STATE_OK {
  89. log.Println("error state:", rsp.State)
  90. return
  91. }
  92. log.Println("[RESULT]<-", string(rsp.Data))
  93. // 获取长数据
  94. rsp = hub.GetOne(remoteFilter, "bigdata", nil)
  95. if rsp.State != tinymq.STATE_OK {
  96. log.Println("error state:", rsp.State)
  97. return
  98. } else {
  99. log.Println("get bigdata ok")
  100. }
  101. time.Sleep(time.Second * 5)
  102. hub.Push(remoteFilter, "push", []byte(time.Now().GoString()))
  103. log.Println("test finished")
  104. time.Sleep(time.Second * 10)
  105. log.Println("client exit")
  106. }