// Package omni connects managed WebSocket sessions to a GoOmni application. package omni import ( "errors" "git.yuntuo.hk/k2safe/go-omni/app" "git.yuntuo.hk/k2safe/go-omni/config" "git.yuntuo.hk/k2safe/go-omni/httpx" "git.yuntuo.hk/k2safe/go-omni/ws" ) // Handler preserves the HTTP middleware chain and records the actual upgrade // status. The server owns all session callbacks after this handler returns. // Register Server.Stop/Drain on App or svc Runtime before accepting connections. func Handler(server *ws.Server, handlers ws.Handlers) httpx.Handler { if server == nil { panic("ws/omni: nil server") } return func(c *httpx.Context) error { _, err := server.Accept(c.Raw(), c.Request(), handlers) if err == nil { c.MarkUpgraded() } // Accept has already written any handshake failure response. return nil } } // Mount registers a GET endpoint and its shutdown barriers on a standalone App. // Use Handler with svc.OnStop/OnDrain when the service Runtime owns dependencies. // Mount once per Server. Configure the App before calling it. func Mount(a *app.App, path string, server *ws.Server, handlers ws.Handlers) error { if a == nil || server == nil { return errors.New("ws/omni: app and server required") } if a.Config().Transport != config.TransportNetHTTP && a.Config().Transport != "" { return ws.ErrUnsupportedTransport } a.GET(path, Handler(server, handlers)) a.OnStop("ws:"+path, server.Stop) a.OnDrain("ws:"+path, server.Drain) return nil }