Websocket.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package Server
  2. import (
  3. "App_Warn/lib"
  4. "App_Warn/logs"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gorilla/websocket"
  8. "net/http"
  9. "time"
  10. )
  11. var upGrader = websocket.Upgrader{
  12. ReadBufferSize: 1024,
  13. WriteBufferSize: 1024,
  14. HandshakeTimeout: 10 * time.Second,
  15. CheckOrigin: func(r *http.Request) bool {
  16. return true
  17. },
  18. }
  19. func WsHandle(c *gin.Context) {
  20. //升级get请求为webSocket协议
  21. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  22. if err != nil {
  23. c.Writer.Write([]byte(err.Error()))
  24. return
  25. }
  26. defer func() {
  27. ClearSubscription(ws) // 清楚订阅
  28. ws.Close()
  29. }()
  30. uuid, _ := c.GetQuery("uuid")
  31. logs.Println("ws上线:", fmt.Sprintf("%p", ws), "uuid:", uuid)
  32. // 追加 订阅UUID
  33. SubConn := []*websocket.Conn{}
  34. // 从map中读取数据
  35. val, ok := lib.SubClinets.Load(uuid)
  36. if ok {
  37. SubConn = val.([]*websocket.Conn)
  38. }
  39. SubConn = append(SubConn, ws)
  40. // 写入map
  41. lib.SubClinets.Store(uuid, SubConn)
  42. for {
  43. //读取ws中的数据
  44. _, _, err := ws.ReadMessage()
  45. if err != nil {
  46. c.Writer.Write([]byte(err.Error()))
  47. break
  48. }
  49. //fmt.Println("client message " + string(message) +"|"+ string(mt))
  50. //fmt.Println(mt)
  51. ////time.Sleep(time.Second * 10)
  52. //写入ws数据
  53. err = ws.WriteMessage(1, []byte(time.Now().String()))
  54. if err != nil {
  55. ClearSubscription(ws) // 清楚订阅
  56. ws.Close()
  57. break
  58. }
  59. //fmt.Println("system message " + time.Now().String())
  60. }
  61. }
  62. // 发送 数据
  63. func WebsocketWriteJSON(client *websocket.Conn, msg interface{}) error {
  64. err := client.WriteJSON(msg)
  65. if err != nil {
  66. ClearSubscription(client) // 清楚订阅
  67. client.Close()
  68. }
  69. return nil
  70. }
  71. func App_Warn_Sand(Uuid string, msg interface{}) string {
  72. val, ok := lib.SubClinets.Load(Uuid)
  73. if ok {
  74. SubConn := val.([]*websocket.Conn)
  75. for _, v := range SubConn {
  76. WebsocketWriteJSON(v, msg)
  77. }
  78. } else {
  79. return "发送无手机在线!"
  80. }
  81. return "发送成功!"
  82. }
  83. // 清楚订阅
  84. func ClearSubscription(client *websocket.Conn) {
  85. logs.Println("清楚订阅:", fmt.Sprintf("%p", client))
  86. // 编列所有订阅号
  87. lib.SubClinets.Range(func(key, value interface{}) bool {
  88. ConnList := value.([]*websocket.Conn)
  89. Sn := key.(string)
  90. logs.Println("SubClinets_数量|", Sn, len(ConnList))
  91. for i, v := range ConnList {
  92. if v == client {
  93. logs.Println("删除订阅:", fmt.Sprintf("%p", v), Sn)
  94. ConnList = append(ConnList[:i], ConnList[i+1:]...)
  95. lib.SubClinets.Store(Sn, ConnList)
  96. break
  97. }
  98. }
  99. return true
  100. })
  101. }