client-tcp2.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/tcp2"
  13. remoteChannel := "/tinymq/server"
  14. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  15. host := &tinymq.HostInfo{
  16. Proto: "tcp",
  17. Version: 2,
  18. Host: "127.0.0.1",
  19. Port: 34222,
  20. Hash: "xor:1qaz2wsx3",
  21. }
  22. hub := tinymq.NewHub(
  23. cf,
  24. localChannel,
  25. func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
  26. return host, nil
  27. },
  28. func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  29. log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
  30. return []byte("tinymq-client")
  31. },
  32. func(client bool, proto string, version uint8, channel string, auth []byte) bool {
  33. log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
  34. return string(auth) == "tinymq-server"
  35. },
  36. func(conn *tinymq.Line) {
  37. log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  38. },
  39. )
  40. hub.SetFilterToChannelFunc(func(filter tinymq.FilterFunc) (channel string) {
  41. return remoteChannel
  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("/tinymq/server", true, nil)
  55. // if err != nil {
  56. // log.Fatalln("[client ConnectToServer ERROR]", err)
  57. // }
  58. // 获取信息
  59. rsp := hub.GetOne(remoteFilter, "hello", []byte("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. time.Sleep(time.Second * 300)
  66. log.Println("client exit")
  67. }