websocket.go 710 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package global
  2. import (
  3. "errors"
  4. "eta/eta_api/utils"
  5. "strconv"
  6. "time"
  7. "github.com/gorilla/websocket"
  8. )
  9. var MonitorMessageConn = make(map[int]*websocket.Conn)
  10. var (
  11. EDB_MONITOR_MESSAGE_CONNECT_CACHE = "edb_monitor_message_cache:"
  12. )
  13. func EdbMonitorMessageHealth(adminId int) (isClose bool, err error) {
  14. conn := MonitorMessageConn[adminId]
  15. if conn == nil {
  16. err = errors.New("no connection")
  17. isClose = true
  18. return
  19. }
  20. _, msg, err := conn.ReadMessage()
  21. if err != nil {
  22. isClose = true
  23. return
  24. }
  25. if string(msg) == "ping" {
  26. healthKey := EDB_MONITOR_MESSAGE_CONNECT_CACHE + strconv.Itoa(adminId)
  27. err = utils.Rc.Put(healthKey, "1", time.Minute*1)
  28. if err != nil {
  29. return
  30. }
  31. }
  32. return
  33. }