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.2 kB

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