main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //usr/bin/env go run "$0" "$@"; exit
  2. package main
  3. import (
  4. "bufio"
  5. "context"
  6. "fmt"
  7. "log"
  8. "os"
  9. "os/signal"
  10. "strings"
  11. "time"
  12. "github.com/vladimirok5959/golang-twitch/pubsub"
  13. )
  14. func main() {
  15. ps := pubsub.New()
  16. defer ps.Close()
  17. ps.OnConnect(func(c *pubsub.Connection) {
  18. log.Printf("OnConnect (ID: %d)\n", c.ID)
  19. })
  20. ps.OnDisconnect(func(c *pubsub.Connection) {
  21. log.Printf("OnDisconnect (ID: %d)\n", c.ID)
  22. })
  23. ps.OnError(func(c *pubsub.Connection, err error) {
  24. log.Printf("OnError (ID: %d), err: %s\n", c.ID, err)
  25. })
  26. ps.OnInfo(func(c *pubsub.Connection, str string) {
  27. log.Printf("OnInfo (ID: %d), str: %s\n", c.ID, str)
  28. })
  29. ps.OnMessage(func(c *pubsub.Connection, msg *pubsub.Answer) {
  30. log.Printf("OnMessage (ID: %d), msg: %#v\n", c.ID, msg)
  31. })
  32. ps.OnPing(func(c *pubsub.Connection, start time.Time) {
  33. log.Printf("OnPing (ID: %d), start: %d\n", c.ID, start.Unix())
  34. })
  35. ps.OnPong(func(c *pubsub.Connection, start, end time.Time) {
  36. log.Printf("OnPong (ID: %d), start: %d, end: %d\n", c.ID, start.Unix(), end.Unix())
  37. })
  38. interrupt := make(chan os.Signal, 1)
  39. signal.Notify(interrupt, os.Interrupt)
  40. ctx := context.Background()
  41. go func(interrupt chan os.Signal) {
  42. reader := bufio.NewReader(os.Stdin)
  43. for {
  44. select {
  45. case <-interrupt:
  46. return
  47. default:
  48. fmt.Print("Enter command: ")
  49. cmd, _ := reader.ReadString('\n')
  50. cmd = strings.TrimSuffix(strings.TrimSuffix(cmd, "\r\n"), "\n")
  51. if strings.HasPrefix(cmd, "listen") {
  52. if len([]rune(cmd)) > 7 {
  53. param := string([]rune(cmd)[7:len([]rune(cmd))])
  54. fmt.Printf("Listen: (%s)\n", param)
  55. ps.Listen(ctx, param)
  56. } else {
  57. fmt.Printf("Parameter is not set\n")
  58. }
  59. } else if strings.HasPrefix(cmd, "unlisten") {
  60. if len([]rune(cmd)) > 9 {
  61. param := string([]rune(cmd)[9:len([]rune(cmd))])
  62. fmt.Printf("Unlisten: (%s)\n", param)
  63. ps.Unlisten(ctx, param)
  64. } else {
  65. fmt.Printf("Parameter is not set\n")
  66. }
  67. } else if strings.HasPrefix(cmd, "has") {
  68. if len([]rune(cmd)) > 4 {
  69. param := string([]rune(cmd)[4:len([]rune(cmd))])
  70. fmt.Printf("HasTopic: (%#v)\n", ps.HasTopic(param))
  71. } else {
  72. fmt.Printf("Parameter is not set\n")
  73. }
  74. } else if cmd == "status" {
  75. fmt.Printf("Status:\n")
  76. fmt.Printf(" - Connections count: (%d)\n", len(ps.Connections))
  77. fmt.Printf(" - Topics: (%#v)\n", ps.Topics())
  78. fmt.Printf(" - TopicsCount: (%d)\n", ps.TopicsCount())
  79. } else if cmd == "help" {
  80. fmt.Printf("Help:\n")
  81. fmt.Printf(" - listen <topic>\n")
  82. fmt.Printf(" - unlisten <topic>\n")
  83. fmt.Printf(" - has <topic>\n")
  84. fmt.Printf(" - status\n")
  85. fmt.Printf(" - help\n")
  86. fmt.Printf(" - close\n")
  87. } else if cmd == "close" {
  88. fmt.Printf("Closing...\n")
  89. ps.Close()
  90. } else {
  91. fmt.Printf("Unknown command\n")
  92. }
  93. }
  94. }
  95. }(interrupt)
  96. <-interrupt
  97. log.Println("Done")
  98. }