client-ws2.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. )
  42. // 订阅频道
  43. hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {
  44. log.Println("[client RECV]<-", string(request.Data))
  45. return 1, "tiny client"
  46. },
  47. )
  48. hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
  49. log.Println("[client RECV]<-", string(request.Data))
  50. return 1, nil
  51. },
  52. )
  53. err := hub.ConnectToServer(remoteChannel, true, nil)
  54. if err != nil {
  55. log.Fatalln("[client ConnectToServer ERROR]", err)
  56. }
  57. // 获取信息
  58. rsp := hub.GetOne(remoteFilter, "hello", "hello from client,hello from client,hello from client")
  59. if rsp.State != config.STATE_OK {
  60. log.Println("error state:", rsp.State)
  61. return
  62. }
  63. log.Println("[RESULT]<-", string(rsp.Data))
  64. rsp = hub.GetOne(remoteFilter, "hello", func() ([]byte, error) {
  65. return []byte("hello from data function"), nil
  66. })
  67. if rsp.State != config.STATE_OK {
  68. log.Println("error state:", rsp.State)
  69. return
  70. }
  71. log.Println("[RESULT]<-", string(rsp.Data))
  72. // 获取长数据
  73. rsp = hub.GetOne(remoteFilter, "bigdata", nil)
  74. if rsp.State != config.STATE_OK {
  75. log.Println("error state:", rsp.State)
  76. return
  77. } else {
  78. log.Println("get bigdata ok")
  79. }
  80. time.Sleep(time.Second * 5)
  81. hub.Push(remoteFilter, "push", []byte(time.Now().GoString()))
  82. time.Sleep(time.Second * 300)
  83. log.Println("client exit")
  84. }