From 584571513cf7d483bf6a7a6ec59be5b2dd33b964 Mon Sep 17 00:00:00 2001 From: what Date: Fri, 10 May 2024 21:18:21 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E9=87=8D=E6=96=B0=E6=A2=B3=E7=90=86?= =?UTF-8?q?=20ws?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http.go | 41 ------------------------------------- ws.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/http.go b/http.go index 5bb8ff6..0c24d38 100644 --- a/http.go +++ b/http.go @@ -40,28 +40,11 @@ type HttpController interface { AuthDB() req.ResAuthDB } -type WsController interface { - HttpController - // 请求处理 - ExecuteWS(req.GlobalParams) error - // 获取 Ws 客户端 - WsClient() WsClient - // 获取 Ws 标识 - WsClientId(req.GlobalParams) WsClientID - // 获取 Ws 分组 - WsClientGroup(req.GlobalParams) WsClientGroup -} - type HttpHandleController struct { Controller request *http.Request } -type WsHandleController struct { - HttpController - ws WsClient -} - func (this HttpHandleController) Request() *http.Request { return this.request } @@ -86,33 +69,9 @@ func (HttpHandleController) AuthDB() req.ResAuthDB { return req.ResAuthOn } -func (WsHandleController) ExecuteWS(params req.GlobalParams) error { - return nil -} - -func (this WsHandleController) WsClient() WsClient { - return this.ws -} - -func (WsHandleController) WsClientId(req.GlobalParams) WsClientID { - wsClientID++ - return wsClientID -} - -func (WsHandleController) WsClientGroup(req.GlobalParams) WsClientGroup { - return defaultWsClientGroup -} - func NewHttpController(container *do.Injector, request *http.Request) HttpController { return &HttpHandleController{ Controller: &BaseController{container}, request: request, } } - -func NewWsController(ws WsClient, ctr HttpController) WsController { - return &WsHandleController{ - HttpController: ctr, - ws: ws, - } -} diff --git a/ws.go b/ws.go index 0cb19e5..225d536 100644 --- a/ws.go +++ b/ws.go @@ -1,8 +1,11 @@ package contracts import ( + "net/http" + "git.fsdpf.net/go/req" "github.com/gorilla/websocket" + "github.com/samber/do" ) type WsClientGroup string @@ -33,3 +36,63 @@ type WsClientManage interface { type WsChannelManage interface { Channel(string) WsClientManage } + +type WsController interface { + // 获取请求信息 + 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, + } +}