server.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(regexp.MustCompile(remoteChannel), "hello", []byte("hello from server push"),
  37. func(response *tinymq.ResponseData) (ok bool) {
  38. log.Println("[hello response]", response.State, string(response.Data))
  39. return true
  40. })
  41. go hub.Get(regexp.MustCompile(remoteChannel), "nodata", nil,
  42. func(response *tinymq.ResponseData) (ok bool) {
  43. log.Println("[nodata response]", response.State, string(response.Data))
  44. return true
  45. })
  46. }
  47. },
  48. )
  49. // tpv2协议
  50. bindTpv2Info := &tinymq.HostInfo{
  51. Proto: "tp",
  52. Version: 2,
  53. Bind: "127.0.0.1",
  54. Port: 34222,
  55. Hash: "xor:1qaz2wsx3",
  56. }
  57. hub.BindForServer(bindTpv2Info)
  58. // wsv2协议
  59. bindwsv2Info := &tinymq.HostInfo{
  60. Proto: "ws",
  61. Version: 2,
  62. Bind: "127.0.0.1",
  63. Port: 34211,
  64. Path: "/tinymq-xor",
  65. Hash: "xor:1qaz2wsx3edc",
  66. }
  67. hub.BindForServer(bindwsv2Info)
  68. // wsv2协议,没有加密算法
  69. bindInfo := &tinymq.HostInfo{
  70. Proto: "ws",
  71. Version: 2,
  72. // Bind: "127.0.0.1",
  73. Port: 34211,
  74. Path: "/tinymq",
  75. }
  76. hub.BindForServer(bindInfo)
  77. // 中间件
  78. hub.UseMiddle(func(request *tinymq.RequestData) (response *tinymq.ResponseData) {
  79. log.Println("[Middle]", request.Conn().Channel(), request.Cmd)
  80. return nil
  81. })
  82. // 订阅频道
  83. hub.Subscribe(regexp.MustCompile(remoteChannel), "hello",
  84. func(request *tinymq.RequestData) (state uint8, result []byte) {
  85. log.Println("[server RECV]<-", string(request.Data))
  86. return 1, []byte("tiny server")
  87. },
  88. )
  89. hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata",
  90. func(request *tinymq.RequestData) (state uint8, result []byte) {
  91. log.Println("[server RECV]<-", string(request.Data))
  92. return 1, nil
  93. },
  94. )
  95. // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindwsv2Info.Bind, strconv.Itoa(int(bindwsv2Info.Port))), nil))
  96. // 初始化一个channel
  97. exit := make(chan os.Signal, 3)
  98. //notify方法用来监听收到的信号
  99. signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  100. sig := <-exit
  101. log.Println("[Exist with]", sig.String())
  102. }