diff options
Diffstat (limited to 'web/web.go')
| -rw-r--r-- | web/web.go | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -25,6 +25,7 @@ import ( "github.com/mhsanaei/3x-ui/v2/web/middleware" "github.com/mhsanaei/3x-ui/v2/web/network" "github.com/mhsanaei/3x-ui/v2/web/service" + "github.com/mhsanaei/3x-ui/v2/web/websocket" "github.com/gin-contrib/gzip" "github.com/gin-contrib/sessions" @@ -98,11 +99,14 @@ type Server struct { index *controller.IndexController panel *controller.XUIController api *controller.APIController + ws *controller.WebSocketController xrayService service.XrayService settingService service.SettingService tgbotService service.Tgbot + wsHub *websocket.Hub + cron *cron.Cron ctx context.Context @@ -266,6 +270,15 @@ func (s *Server) initRouter() (*gin.Engine, error) { s.panel = controller.NewXUIController(g) s.api = controller.NewAPIController(g) + // Initialize WebSocket hub + s.wsHub = websocket.NewHub() + go s.wsHub.Run() + + // Initialize WebSocket controller + s.ws = controller.NewWebSocketController(s.wsHub) + // Register WebSocket route with basePath (g already has basePath prefix) + g.GET("/ws", s.ws.HandleWebSocket) + // Chrome DevTools endpoint for debugging web apps engine.GET("/.well-known/appspecific/com.chrome.devtools.json", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) @@ -448,6 +461,10 @@ func (s *Server) Stop() error { if s.tgbotService.IsRunning() { s.tgbotService.Stop() } + // Gracefully stop WebSocket hub + if s.wsHub != nil { + s.wsHub.Stop() + } var err1 error var err2 error if s.httpServer != nil { @@ -468,3 +485,8 @@ func (s *Server) GetCtx() context.Context { func (s *Server) GetCron() *cron.Cron { return s.cron } + +// GetWSHub returns the WebSocket hub instance. +func (s *Server) GetWSHub() interface{} { + return s.wsHub +} |
