client-ws2.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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"
  14. remoteChannel := "/tinymq/server"
  15. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  16. host := &conn.HostInfo{
  17. Proto: "ws",
  18. Version: 2,
  19. Host: "127.0.0.1",
  20. Port: 34211,
  21. // Path: "/tinymq",
  22. Path: "/tinymq-xor",
  23. Hash: "xor:1qaz2wsx3edc",
  24. }
  25. hub := tinymq.NewHub(
  26. cf,
  27. localChannel,
  28. func(channel string, hostType tinymq.HostType) (hostInfo *conn.HostInfo, err error) {
  29. return host, nil
  30. },
  31. func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  32. log.Println("[AuthFunc]", host, proto, version, channel, string(remoteAuth))
  33. return []byte("tinymq-client")
  34. },
  35. func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool {
  36. log.Println("[CheckAuthFunc]", host, proto, version, channel, string(auth))
  37. return string(auth) == "tinymq-server"
  38. },
  39. func(conn *tinymq.Line) {
  40. log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  41. },
  42. )
  43. // 订阅频道
  44. hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {
  45. log.Println("[client RECV]<-", string(request.Data))
  46. return 1, "tiny client"
  47. },
  48. )
  49. hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
  50. log.Println("[client RECV]<-", string(request.Data))
  51. return 1, nil
  52. },
  53. )
  54. err := hub.ConnectToServer(remoteChannel, true, nil, false)
  55. if err != nil {
  56. log.Fatalln("[client ConnectToServer ERROR]", err)
  57. }
  58. log.Println("start get data")
  59. count, err := hub.Get(remoteFilter, "hello", "hello in get model", func(response *tinymq.ResponseData) (ok bool) {
  60. log.Println("get state and data: ", response.State, string(response.Data))
  61. return true
  62. })
  63. log.Println("end get data with count and err:", count, err)
  64. // 获取信息
  65. rsp := hub.GetOne(remoteFilter, "hello", "hello from client,hello from client,hello from client")
  66. if rsp.State != tinymq.STATE_OK {
  67. log.Println("error state:", rsp.State)
  68. return
  69. }
  70. log.Println("[RESULT]<-", string(rsp.Data))
  71. rsp = hub.GetOne(remoteFilter, "hello", func() ([]byte, error) {
  72. return []byte("hello from data function"), nil
  73. })
  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. // 获取长数据
  80. rsp = hub.GetOne(remoteFilter, "bigdata", nil)
  81. if rsp.State != tinymq.STATE_OK {
  82. log.Println("error state:", rsp.State)
  83. return
  84. } else {
  85. log.Println("get bigdata ok")
  86. }
  87. time.Sleep(time.Second * 5)
  88. hub.Push(remoteFilter, "push", []byte(time.Now().GoString()))
  89. time.Sleep(time.Second * 30)
  90. log.Println("client exit")
  91. }