2024-05-09 13:15:24 +08:00
|
|
|
package contracts
|
|
|
|
|
|
|
|
import (
|
2024-05-10 21:18:21 +08:00
|
|
|
"net/http"
|
|
|
|
|
2024-05-09 13:15:24 +08:00
|
|
|
"git.fsdpf.net/go/req"
|
|
|
|
"github.com/gorilla/websocket"
|
2024-05-10 21:18:21 +08:00
|
|
|
"github.com/samber/do"
|
2024-05-09 13:15:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-05-10 21:18:21 +08:00
|
|
|
|
|
|
|
type WsController interface {
|
2024-05-10 21:55:09 +08:00
|
|
|
Controller
|
2024-05-10 21:18:21 +08:00
|
|
|
// 获取请求信息
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|