From 3789711bed458986aaa990464ac18ebdda8a2aa2 Mon Sep 17 00:00:00 2001 From: lolka1333 Date: Sun, 4 Jan 2026 19:23:03 +0100 Subject: [PATCH] Add WebSocket support for outbounds traffic updates - Implemented WebSocket connection in xray.html to handle real-time updates for outbounds traffic. - Enhanced xray_traffic_job.go to retrieve and broadcast outbounds traffic updates. - Introduced MessageTypeOutbounds in hub.go for managing outbounds messages. - Added BroadcastOutbounds function in notifier.go to facilitate broadcasting outbounds updates to connected clients. --- web/html/xray.html | 11 +++++++++++ web/job/xray_traffic_job.go | 9 +++++++++ web/websocket/hub.go | 1 + web/websocket/notifier.go | 8 ++++++++ 4 files changed, 29 insertions(+) diff --git a/web/html/xray.html b/web/html/xray.html index 1cbde31a..186156ff 100644 --- a/web/html/xray.html +++ b/web/html/xray.html @@ -968,6 +968,17 @@ await this.getXraySetting(); await this.getXrayResult(); await this.getOutboundsTraffic(); + + if (window.wsClient) { + window.wsClient.connect(); + window.wsClient.on('outbounds', (payload) => { + if (payload) { + this.outboundsTraffic = payload; + this.$forceUpdate(); + } + }); + } + while (true) { await PromiseUtil.sleep(800); this.saveBtnDisable = this.oldXraySetting === this.xraySetting; diff --git a/web/job/xray_traffic_job.go b/web/job/xray_traffic_job.go index e7b1b5c0..33a432b5 100644 --- a/web/job/xray_traffic_job.go +++ b/web/job/xray_traffic_job.go @@ -65,6 +65,11 @@ func (j *XrayTrafficJob) Run() { logger.Warning("get all inbounds for websocket failed:", err) } + updatedOutbounds, err := j.outboundService.GetOutboundsTraffic() + if err != nil { + logger.Warning("get all outbounds for websocket failed:", err) + } + // Broadcast traffic update via WebSocket with accumulated values from database trafficUpdate := map[string]interface{}{ "traffics": traffics, @@ -79,6 +84,10 @@ func (j *XrayTrafficJob) Run() { websocket.BroadcastInbounds(updatedInbounds) } + if updatedOutbounds != nil { + websocket.BroadcastOutbounds(updatedOutbounds) + } + } func (j *XrayTrafficJob) informTrafficToExternalAPI(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) { diff --git a/web/websocket/hub.go b/web/websocket/hub.go index 187ebb6f..7ee93405 100644 --- a/web/websocket/hub.go +++ b/web/websocket/hub.go @@ -20,6 +20,7 @@ const ( MessageTypeInbounds MessageType = "inbounds" // Inbounds list update MessageTypeNotification MessageType = "notification" // System notification MessageTypeXrayState MessageType = "xray_state" // Xray state change + MessageTypeOutbounds MessageType = "outbounds" // Outbounds list update ) // Message represents a WebSocket message diff --git a/web/websocket/notifier.go b/web/websocket/notifier.go index cedf56f2..416adaff 100644 --- a/web/websocket/notifier.go +++ b/web/websocket/notifier.go @@ -48,6 +48,14 @@ func BroadcastInbounds(inbounds interface{}) { } } +// BroadcastOutbounds broadcasts outbounds list update to all connected clients +func BroadcastOutbounds(outbounds interface{}) { + 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()