server.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //go:build ignore
  2. // +build ignore
  3. package main
  4. import (
  5. "errors"
  6. "log"
  7. "os"
  8. "os/signal"
  9. "regexp"
  10. "syscall"
  11. "git.me9.top/git/tinymq"
  12. "git.me9.top/git/tinymq/config"
  13. )
  14. func main() {
  15. cf := config.NewConfig()
  16. localChannel := "/tinymq/server"
  17. remoteChannel := "/tinymq/client"
  18. var hub *tinymq.Hub
  19. hub = tinymq.NewHub(cf, localChannel,
  20. func(channel string) (hostInfo *tinymq.HostInfo, err error) {
  21. return nil, errors.New("not host found")
  22. }, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  23. // 从 remoteAuth 是否为空来判断是否需要返回信息
  24. if len(remoteAuth) <= 0 {
  25. // 客户端调用,返回验证信息
  26. return []byte("tinymq")
  27. } else {
  28. // 服务端调用,返回验证token,或者其他信息
  29. return nil
  30. }
  31. }, func(proto string, version uint8, channel string, auth []byte) bool {
  32. return string(auth) == "tinymq"
  33. }, func(conn *tinymq.Line) {
  34. log.Println("[Connect state change]", conn.Channel(), conn.State(), conn.Started(), conn.Updated())
  35. if conn.State() == tinymq.Connected {
  36. go hub.Get(&tinymq.GetData{
  37. Channel: regexp.MustCompile(remoteChannel),
  38. Cmd: "hello",
  39. Data: []byte("hello from server push"),
  40. }, func(response *tinymq.ResponseData) (ok bool) {
  41. log.Println("[hello response]", response.State, string(response.Data))
  42. return true
  43. })
  44. go hub.Get(&tinymq.GetData{
  45. Channel: regexp.MustCompile(remoteChannel),
  46. Cmd: "nodata",
  47. }, func(response *tinymq.ResponseData) (ok bool) {
  48. log.Println("[nodata response]", response.State, string(response.Data))
  49. return true
  50. })
  51. }
  52. },
  53. )
  54. // tpv2协议
  55. bindTpv2Info := &tinymq.HostInfo{
  56. Proto: "tp",
  57. Version: 2,
  58. Bind: "127.0.0.1",
  59. Port: 34222,
  60. Hash: "xor:1qaz2wsx3",
  61. }
  62. hub.BindForServer(bindTpv2Info)
  63. // wsv2协议
  64. bindwsv2Info := &tinymq.HostInfo{
  65. Proto: "ws",
  66. Version: 2,
  67. Bind: "127.0.0.1",
  68. Port: 34211,
  69. Path: "/tinymq-xor",
  70. Hash: "xor:1qaz2wsx3edc",
  71. }
  72. hub.BindForServer(bindwsv2Info)
  73. // wsv2协议,没有加密算法
  74. bindInfo := &tinymq.HostInfo{
  75. Proto: "ws",
  76. Version: 2,
  77. // Bind: "127.0.0.1",
  78. Port: 34211,
  79. Path: "/tinymq",
  80. }
  81. hub.BindForServer(bindInfo)
  82. // 订阅频道
  83. hub.Subscribe(&tinymq.SubscribeData{
  84. Channel: regexp.MustCompile(remoteChannel),
  85. Cmd: "hello",
  86. BackFunc: func(request *tinymq.RequestData) (state uint8, result []byte) {
  87. log.Println("[server RECV]<-", string(request.Data))
  88. return 1, []byte("tiny server")
  89. },
  90. })
  91. hub.Subscribe(&tinymq.SubscribeData{
  92. Channel: regexp.MustCompile(remoteChannel),
  93. Cmd: "nodata",
  94. BackFunc: func(request *tinymq.RequestData) (state uint8, result []byte) {
  95. log.Println("[server RECV]<-", string(request.Data))
  96. return 1, nil
  97. },
  98. })
  99. // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindwsv2Info.Bind, strconv.Itoa(int(bindwsv2Info.Port))), nil))
  100. // 初始化一个channel
  101. exit := make(chan os.Signal, 3)
  102. //notify方法用来监听收到的信号
  103. signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  104. sig := <-exit
  105. log.Println("[Exist with]", sig.String())
  106. }