notify_old.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package v1
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/IceWhaleTech/CasaOS/service"
  6. "github.com/IceWhaleTech/CasaOS/types"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gorilla/websocket"
  9. )
  10. var upGrader = websocket.Upgrader{
  11. CheckOrigin: func(r *http.Request) bool {
  12. return true
  13. },
  14. }
  15. // @Summary websocket 接口,连接成功后发送一个"notify"字符串
  16. // @Produce application/json
  17. // @Accept application/json
  18. // @Tags notify
  19. // @Security ApiKeyAuth
  20. // @Param token path string true "token"
  21. // @Success 200 {string} string "ok"
  22. // @Router /notify/ws [get]
  23. func NotifyWS(c *gin.Context) {
  24. //升级get请求为webSocket协议
  25. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  26. if err != nil {
  27. return
  28. }
  29. defer ws.Close()
  30. service.WebSocketConns = append(service.WebSocketConns, ws)
  31. if !service.SocketRun {
  32. service.SocketRun = true
  33. service.SendMeg()
  34. }
  35. for {
  36. mt, message, err := ws.ReadMessage()
  37. fmt.Println(mt, message, err)
  38. }
  39. }
  40. // @Summary 标记notify已读
  41. // @Produce application/json
  42. // @Accept application/json
  43. // @Tags notify
  44. // @Security ApiKeyAuth
  45. // @Success 200 {string} string "ok"
  46. // @Router /notify/read/{id} [put]
  47. func PutNotifyRead(c *gin.Context) {
  48. id := c.Param("id")
  49. // if len(id) == 0 {
  50. // c.JSON(http.StatusOK, model.Result{Success: oasis_err.INVALID_PARAMS, Message: oasis_err.GetMsg(oasis_err.INVALID_PARAMS)})
  51. // return
  52. // }
  53. fmt.Println(id)
  54. service.MyService.Notify().MarkRead(id, types.NOTIFY_READ)
  55. }