Welcome to mirror list, hosted at ThFree Co, Russian Federation.

notifier.go « websocket « web - github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74cf61b239d570f6bc6a6449c296eb3f914e255d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package websocket provides WebSocket hub for real-time updates and notifications.
package websocket

import (
	"github.com/mhsanaei/3x-ui/v2/logger"
	"github.com/mhsanaei/3x-ui/v2/web/global"
)

// GetHub returns the global WebSocket hub instance
func GetHub() *Hub {
	webServer := global.GetWebServer()
	if webServer == nil {
		return nil
	}
	hub := webServer.GetWSHub()
	if hub == nil {
		return nil
	}
	wsHub, ok := hub.(*Hub)
	if !ok {
		logger.Warning("WebSocket hub type assertion failed")
		return nil
	}
	return wsHub
}

// BroadcastStatus broadcasts server status update to all connected clients
func BroadcastStatus(status any) {
	hub := GetHub()
	if hub != nil {
		hub.Broadcast(MessageTypeStatus, status)
	}
}

// BroadcastTraffic broadcasts traffic statistics update to all connected clients
func BroadcastTraffic(traffic any) {
	hub := GetHub()
	if hub != nil {
		hub.Broadcast(MessageTypeTraffic, traffic)
	}
}

// BroadcastInbounds broadcasts inbounds list update to all connected clients
func BroadcastInbounds(inbounds any) {
	hub := GetHub()
	if hub != nil {
		hub.Broadcast(MessageTypeInbounds, inbounds)
	}
}

// BroadcastOutbounds broadcasts outbounds list update to all connected clients
func BroadcastOutbounds(outbounds any) {
	hub := GetHub()
	if hub != nil {
		hub.Broadcast(MessageTypeOutbounds, outbounds)
	}
}

// BroadcastNotification broadcasts a system notification to all connected clients
func BroadcastNotification(title, message, level string) {
	hub := GetHub()
	if hub != nil {
		notification := map[string]string{
			"title":   title,
			"message": message,
			"level":   level, // info, warning, error, success
		}
		hub.Broadcast(MessageTypeNotification, notification)
	}
}

// BroadcastXrayState broadcasts Xray state change to all connected clients
func BroadcastXrayState(state string, errorMsg string) {
	hub := GetHub()
	if hub != nil {
		stateUpdate := map[string]string{
			"state":    state,
			"errorMsg": errorMsg,
		}
		hub.Broadcast(MessageTypeXrayState, stateUpdate)
	}
}