2023-03-13 17:00:46 +08:00
|
|
|
|
package tcpclient
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"hotgo/internal/library/network/tcp"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"hotgo/internal/model/input/msgin"
|
2023-03-13 17:00:46 +08:00
|
|
|
|
"hotgo/internal/service"
|
|
|
|
|
"hotgo/utility/simple"
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// tcp客户端
|
|
|
|
|
type sAuthClient struct {
|
|
|
|
|
client *tcp.Client
|
|
|
|
|
summary *msgin.AuthSummaryData
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
service.RegisterAuthClient(newAuthClient())
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func newAuthClient() *sAuthClient {
|
|
|
|
|
return &sAuthClient{}
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start 启动服务
|
2023-05-10 23:54:50 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 17:00:46 +08:00
|
|
|
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
s.client, err = tcp.NewClient(&tcp.ClientConfig{
|
|
|
|
|
Addr: config.Client.Auth.Address,
|
2023-03-13 17:00:46 +08:00
|
|
|
|
Auth: &tcp.AuthMeta{
|
2023-05-10 23:54:50 +08:00
|
|
|
|
Group: config.Client.Auth.Group,
|
|
|
|
|
Name: config.Client.Auth.Name,
|
|
|
|
|
AppId: config.Client.Auth.AppId,
|
|
|
|
|
SecretKey: config.Client.Auth.SecretKey,
|
2023-03-13 17:00:46 +08:00
|
|
|
|
},
|
2023-03-16 15:35:02 +08:00
|
|
|
|
LoginEvent: s.onLoginEvent,
|
|
|
|
|
CloseEvent: s.onCloseEvent,
|
2023-03-13 17:00:46 +08:00
|
|
|
|
})
|
2023-05-14 23:55:16 +08:00
|
|
|
|
|
2023-03-13 17:00:46 +08:00
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Errorf(ctx, "AuthClient NewClient fail:%+v", err)
|
2023-03-13 17:00:46 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.client.RegisterRouter(map[string]tcp.RouterHandler{
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"ResponseAuthSummary": s.OnResponseAuthSummary,
|
2023-03-13 17:00:46 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Errorf(ctx, "AuthClient RegisterRouter fail:%+v", err)
|
2023-03-13 17:00:46 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = s.client.Start(); err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Errorf(ctx, "AuthClient Start fail:%+v", err)
|
2023-03-13 17:00:46 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 15:35:02 +08:00
|
|
|
|
// Stop 停止服务
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) Stop(ctx context.Context) {
|
2023-03-13 17:00:46 +08:00
|
|
|
|
if s.client != nil {
|
|
|
|
|
s.client.Stop()
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Debug(ctx, "AuthClient stop..")
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 15:35:02 +08:00
|
|
|
|
// IsLogin 是否已登录认证
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) IsLogin() bool {
|
2023-04-05 12:29:33 +08:00
|
|
|
|
if s.client == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-03-16 15:35:02 +08:00
|
|
|
|
return s.client.IsLogin
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 15:35:02 +08:00
|
|
|
|
// onLoginEvent 登录认证成功事件
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) onLoginEvent() {
|
|
|
|
|
|
|
|
|
|
// 获取授权数据
|
|
|
|
|
s.client.Send(s.client.Ctx, &msgin.AuthSummary{})
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 15:35:02 +08:00
|
|
|
|
// onCloseEvent 连接关闭回调事件
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) onCloseEvent() {
|
2023-03-16 15:35:02 +08:00
|
|
|
|
// ...
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|