This commit is contained in:
孟帅
2023-05-10 23:54:50 +08:00
parent bbe655a4d8
commit 49a96750bf
314 changed files with 15138 additions and 6244 deletions

View File

@@ -4,71 +4,84 @@ import (
"context"
"github.com/gogf/gf/v2/frame/g"
"hotgo/internal/library/network/tcp"
"hotgo/internal/model/input/msgin"
"hotgo/internal/service"
"hotgo/utility/simple"
)
// tcp授权
type sTCPAuth struct {
client *tcp.Client
// tcp客户端
type sAuthClient struct {
client *tcp.Client
summary *msgin.AuthSummaryData
}
func init() {
service.RegisterTCPAuth(newTCPAuth())
service.RegisterAuthClient(newAuthClient())
}
func newTCPAuth() *sTCPAuth {
return &sTCPAuth{}
func newAuthClient() *sAuthClient {
return &sAuthClient{}
}
// Start 启动服务
func (s *sTCPAuth) Start(ctx context.Context) {
g.Log().Debug(ctx, "TCPAuth start..")
func (s *sAuthClient) Start(ctx context.Context) {
g.Log().Debug(ctx, "AuthClient start..")
config, err := service.SysConfig().GetLoadTCP(ctx)
if err != nil {
g.Log().Errorf(ctx, "AuthClient start fail:%+v", err)
return
}
if config.Client == nil || config.Client.Auth == nil {
g.Log().Errorf(ctx, "AuthClient config is invalid")
return
}
simple.SafeGo(ctx, func(ctx context.Context) {
client, err := tcp.NewClient(&tcp.ClientConfig{
Addr: "127.0.0.1:8099",
s.client, err = tcp.NewClient(&tcp.ClientConfig{
Addr: config.Client.Auth.Address,
Auth: &tcp.AuthMeta{
Group: "auth",
Name: "auth1",
AppId: "mengshuai",
SecretKey: "123456",
Group: config.Client.Auth.Group,
Name: config.Client.Auth.Name,
AppId: config.Client.Auth.AppId,
SecretKey: config.Client.Auth.SecretKey,
},
LoginEvent: s.onLoginEvent,
CloseEvent: s.onCloseEvent,
})
if err != nil {
g.Log().Infof(ctx, "TCPAuth NewClient fail%+v", err)
g.Log().Errorf(ctx, "AuthClient NewClient fail%+v", err)
return
}
s.client = client
err = s.client.RegisterRouter(map[string]tcp.RouterHandler{
// ...
"ResponseAuthSummary": s.OnResponseAuthSummary,
})
if err != nil {
g.Log().Infof(ctx, "TCPAuth RegisterRouter fail%+v", err)
g.Log().Errorf(ctx, "AuthClient RegisterRouter fail%+v", err)
return
}
if err = s.client.Start(); err != nil {
g.Log().Infof(ctx, "TCPAuth Start fail%+v", err)
g.Log().Errorf(ctx, "AuthClient Start fail%+v", err)
return
}
})
}
// Stop 停止服务
func (s *sTCPAuth) Stop(ctx context.Context) {
func (s *sAuthClient) Stop(ctx context.Context) {
if s.client != nil {
s.client.Stop()
g.Log().Debug(ctx, "TCPAuth stop..")
g.Log().Debug(ctx, "AuthClient stop..")
}
}
// IsLogin 是否已登录认证
func (s *sTCPAuth) IsLogin() bool {
func (s *sAuthClient) IsLogin() bool {
if s.client == nil {
return false
}
@@ -76,11 +89,13 @@ func (s *sTCPAuth) IsLogin() bool {
}
// onLoginEvent 登录认证成功事件
func (s *sTCPAuth) onLoginEvent() {
// ...
func (s *sAuthClient) onLoginEvent() {
// 获取授权数据
s.client.Send(s.client.Ctx, &msgin.AuthSummary{})
}
// onCloseEvent 连接关闭回调事件
func (s *sTCPAuth) onCloseEvent() {
func (s *sAuthClient) onCloseEvent() {
// ...
}