notifier.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Package websocket provides WebSocket hub for real-time updates and notifications.
  2. package websocket
  3. import (
  4. "github.com/mhsanaei/3x-ui/v2/logger"
  5. "github.com/mhsanaei/3x-ui/v2/web/global"
  6. )
  7. // GetHub returns the global WebSocket hub instance
  8. func GetHub() *Hub {
  9. webServer := global.GetWebServer()
  10. if webServer == nil {
  11. return nil
  12. }
  13. hub := webServer.GetWSHub()
  14. if hub == nil {
  15. return nil
  16. }
  17. wsHub, ok := hub.(*Hub)
  18. if !ok {
  19. logger.Warning("WebSocket hub type assertion failed")
  20. return nil
  21. }
  22. return wsHub
  23. }
  24. // BroadcastStatus broadcasts server status update to all connected clients
  25. func BroadcastStatus(status interface{}) {
  26. hub := GetHub()
  27. if hub != nil {
  28. hub.Broadcast(MessageTypeStatus, status)
  29. }
  30. }
  31. // BroadcastTraffic broadcasts traffic statistics update to all connected clients
  32. func BroadcastTraffic(traffic interface{}) {
  33. hub := GetHub()
  34. if hub != nil {
  35. hub.Broadcast(MessageTypeTraffic, traffic)
  36. }
  37. }
  38. // BroadcastInbounds broadcasts inbounds list update to all connected clients
  39. func BroadcastInbounds(inbounds interface{}) {
  40. hub := GetHub()
  41. if hub != nil {
  42. hub.Broadcast(MessageTypeInbounds, inbounds)
  43. }
  44. }
  45. // BroadcastNotification broadcasts a system notification to all connected clients
  46. func BroadcastNotification(title, message, level string) {
  47. hub := GetHub()
  48. if hub != nil {
  49. notification := map[string]string{
  50. "title": title,
  51. "message": message,
  52. "level": level, // info, warning, error, success
  53. }
  54. hub.Broadcast(MessageTypeNotification, notification)
  55. }
  56. }
  57. // BroadcastXrayState broadcasts Xray state change to all connected clients
  58. func BroadcastXrayState(state string, errorMsg string) {
  59. hub := GetHub()
  60. if hub != nil {
  61. stateUpdate := map[string]string{
  62. "state": state,
  63. "errorMsg": errorMsg,
  64. }
  65. hub.Broadcast(MessageTypeXrayState, stateUpdate)
  66. }
  67. }