You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

actionnotification.go 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package routers
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/context"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/services/socketwrap"
  7. "github.com/gorilla/websocket"
  8. )
  9. var upgrader = websocket.Upgrader{
  10. ReadBufferSize: 1024,
  11. WriteBufferSize: 1024,
  12. }
  13. var SocketManager = socketwrap.NewClientsManager()
  14. func ActionNotification(ctx *context.Context) {
  15. conn, err := upgrader.Upgrade(ctx.Resp, ctx.Req.Request, nil)
  16. if err != nil {
  17. log.Warn("can not create connection.", err)
  18. return
  19. }
  20. client := &socketwrap.Client{Manager: SocketManager, Conn: conn, Send: make(chan *models.Action, 256)}
  21. WriteLastTenActionsIfHave(conn)
  22. client.Manager.Register <- client
  23. go client.WritePump()
  24. }
  25. func WriteLastTenActionsIfHave(conn *websocket.Conn) {
  26. socketwrap.LastTenActionsQueue.Mutex.RLock()
  27. {
  28. size := socketwrap.LastTenActionsQueue.Queue.Len()
  29. if size > 0 {
  30. tempE := socketwrap.LastTenActionsQueue.Queue.Front()
  31. conn.WriteJSON(tempE)
  32. for i := 1; i < size; i++ {
  33. tempE = tempE.Next()
  34. conn.WriteJSON(tempE)
  35. }
  36. }
  37. }
  38. socketwrap.LastTenActionsQueue.Mutex.RUnlock()
  39. }