client-ws2.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. )
  10. func main() {
  11. cf := config.NewConfig()
  12. localChannel := "/tinymq/client/ws2"
  13. remoteChannel := "/tinymq/server"
  14. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  15. host := &tinymq.HostInfo{
  16. Proto: "ws",
  17. Version: 2,
  18. Host: "127.0.0.1",
  19. Port: 34211,
  20. // Path: "/tinymq",
  21. Path: "/tinymq-xor",
  22. Hash: "xor:1qaz2wsx3edc",
  23. }
  24. hub := tinymq.NewHub(
  25. cf,
  26. localChannel,
  27. func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
  28. return host, nil
  29. },
  30. func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  31. log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
  32. return []byte("tinymq-client")
  33. },
  34. func(client bool, proto string, version uint8, channel string, auth []byte) bool {
  35. log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
  36. return string(auth) == "tinymq-server"
  37. },
  38. func(conn *tinymq.Line) {
  39. log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  40. },
  41. nil,
  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)
  55. if err != nil {
  56. log.Fatalln("[client ConnectToServer ERROR]", err)
  57. }
  58. // 获取信息
  59. rsp := hub.GetOne(remoteFilter, "hello", "hello from client,hello from client,hello from client")
  60. if rsp.State != config.STATE_OK {
  61. log.Println("error state:", rsp.State)
  62. return
  63. }
  64. log.Println("[RESULT]<-", string(rsp.Data))
  65. rsp = hub.GetOne(remoteFilter, "hello", func() ([]byte, error) {
  66. return []byte("hello from data function"), nil
  67. })
  68. if rsp.State != config.STATE_OK {
  69. log.Println("error state:", rsp.State)
  70. return
  71. }
  72. log.Println("[RESULT]<-", string(rsp.Data))
  73. // 获取长数据
  74. rsp = hub.GetOne(remoteFilter, "bigdata", nil)
  75. if rsp.State != config.STATE_OK {
  76. log.Println("error state:", rsp.State)
  77. return
  78. } else {
  79. log.Println("get bigdata ok")
  80. }
  81. time.Sleep(time.Second * 5)
  82. hub.Push(remoteFilter, "push", []byte(time.Now().GoString()))
  83. time.Sleep(time.Second * 300)
  84. log.Println("client exit")
  85. }