package contracts import ( "net/http" "git.fsdpf.net/go/req" "github.com/gorilla/websocket" "github.com/samber/do" ) type WsClientGroup string type WsClientID uint // type WsClient struct { // User // Group WsClientGroup // Socket *websocket.Conn // } type WsClient interface { req.User Socket() *websocket.Conn WsClientID() WsClientID WsClientGroup() WsClientGroup WsClientManage() WsClientManage Lock() Unlock() } type WsClientManage interface { Register(WsClient) bool Unregister(WsClient) GetClients(func(WsClient) bool) []WsClient } type WsChannelManage interface { Channel(string) WsClientManage } type WsController interface { Controller // 获取请求信息 Request() *http.Request // 请求处理 Execute(req.GlobalParams) error // 获取用户信息 User() req.User // 路由信息 Route() req.Route // 获取 Ws 客户端 Client() WsClient // 获取 Ws 标识 ClientId(req.GlobalParams) WsClientID // 获取 Ws 分组 ClientGroup(req.GlobalParams) WsClientGroup } type WsHandleController struct { Controller ws WsClient request *http.Request } func (this WsHandleController) Request() *http.Request { return this.request } func (this WsHandleController) Route() req.Route { return this.Request().Context().Value(req.RouteCtx{Name: "Route"}).(req.Route) } func (this WsHandleController) User() req.User { return this.Request().Context().Value(req.RouteCtx{Name: "User"}).(req.User) } func (this WsHandleController) Execute(req.GlobalParams) error { return ErrFuncNotImplemented } func (this WsHandleController) Client() WsClient { return this.ws } func (WsHandleController) ClientId(req.GlobalParams) WsClientID { wsClientID++ return wsClientID } func (WsHandleController) ClientGroup(req.GlobalParams) WsClientGroup { return defaultWsClientGroup } func NewWsController(container *do.Injector, request *http.Request, ws WsClient) WsController { return &WsHandleController{ Controller: &BaseController{container}, request: request, ws: ws, } }