[feat] 重新梳理 ws

This commit is contained in:
what 2024-05-10 21:18:21 +08:00
parent 6d3b2267b9
commit 584571513c
2 changed files with 63 additions and 41 deletions

41
http.go
View File

@ -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,
}
}

63
ws.go
View File

@ -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,
}
}