Websocket.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package controllers
  2. import (
  3. beego "github.com/beego/beego/v2/server/web"
  4. "github.com/gorilla/websocket"
  5. "time"
  6. )
  7. type MySocketController struct {
  8. beego.Controller
  9. }
  10. var upgrader = websocket.Upgrader{
  11. ReadBufferSize: 1024,
  12. WriteBufferSize: 1024,
  13. HandshakeTimeout: 10 * time.Second,
  14. }
  15. type RecvWs struct {
  16. Type int `json:"T_type"` // 必填 1 订阅SN数据 2 解绑SN数据 3 项设备发送数据
  17. Sn string `json:"T_sn"` // 必填
  18. Password string `json:"T_ckey"` // 必填
  19. Json map[string]interface{} `json:"T_json"` // Type == 3 时 必填
  20. }
  21. // NGINX 超时配置
  22. // proxy_connect_timeout 4s;
  23. // proxy_read_timeout 120s;
  24. // proxy_send_timeout 120s;
  25. func init() {
  26. //go SubClinetsClear()
  27. }
  28. //循环清理
  29. //func SubClinetsClear() {
  30. // for true {
  31. // SubClinetsClearMap := make(map[*websocket.Conn]bool)
  32. // time.Sleep(time.Second * 10)
  33. // lib.SubClinets.Range(func(key, value interface{}) bool {
  34. // ConnList := value.([]*websocket.Conn)
  35. // Sn := key.(string)
  36. // logs.Println("SubClinets|", Sn, len(ConnList))
  37. // for _, v := range ConnList {
  38. // if _,ok :=SubClinetsClearMap[v];!ok{
  39. // logs.Println("Websocket 心跳|", fmt.Sprintf("%p", v))
  40. // lib.WebsocketWriteJSON(v,"")
  41. // }
  42. // }
  43. //
  44. // return true
  45. // })
  46. //
  47. // }
  48. //}