htmgo/extensions/websocket/init.go

24 lines
682 B
Go
Raw Normal View History

2024-10-16 20:23:36 +00:00
package websocket
import (
2024-10-16 20:29:01 +00:00
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/ws"
2024-10-16 20:23:36 +00:00
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
)
type WsExtensionOpts struct {
WsPath string
}
func EnableExtension(app *h.App, opts WsExtensionOpts) {
if app.Opts.ServiceLocator == nil {
app.Opts.ServiceLocator = service.NewLocator()
}
service.Set[wsutil.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *wsutil.SocketManager {
return wsutil.NewSocketManager()
})
ws.StartListener(app.Opts.ServiceLocator)
app.Router.Handle(opts.WsPath, wsutil.WsHttpHandler())
}