2023-07-20 18:01:10 +08:00
|
|
|
|
// Package tcpclient
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
2023-03-13 17:00:46 +08:00
|
|
|
|
package tcpclient
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2023-07-20 18:01:10 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
|
"hotgo/api/servmsg"
|
2023-03-13 17:00:46 +08:00
|
|
|
|
"hotgo/internal/library/network/tcp"
|
2023-07-20 18:01:10 +08:00
|
|
|
|
"hotgo/internal/model/input/servmsgin"
|
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
|
2023-07-20 18:01:10 +08:00
|
|
|
|
summary *servmsgin.AuthSummaryModel
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// Instance 获取实例
|
|
|
|
|
func (s *sAuthClient) Instance() *tcp.Client {
|
|
|
|
|
return s.client
|
|
|
|
|
}
|
|
|
|
|
|
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-07-20 18:01:10 +08:00
|
|
|
|
// 创建客户端配置
|
|
|
|
|
clientConfig := &tcp.ClientConfig{
|
|
|
|
|
Addr: config.Client.Auth.Address,
|
|
|
|
|
AutoReconnect: true,
|
|
|
|
|
Auth: &tcp.AuthMeta{
|
|
|
|
|
Name: config.Client.Auth.Name,
|
|
|
|
|
Extra: g.Map{
|
|
|
|
|
"test": 13,
|
2023-03-13 17:00:46 +08:00
|
|
|
|
},
|
2023-07-20 18:01:10 +08:00
|
|
|
|
Group: config.Client.Auth.Group,
|
|
|
|
|
AppId: config.Client.Auth.AppId,
|
|
|
|
|
SecretKey: config.Client.Auth.SecretKey,
|
|
|
|
|
},
|
|
|
|
|
LoginEvent: s.onLoginEvent,
|
|
|
|
|
CloseEvent: s.onCloseEvent,
|
|
|
|
|
}
|
2023-03-13 17:00:46 +08:00
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
|
|
|
|
s.client = tcp.NewClient(clientConfig)
|
2023-03-13 17:00:46 +08:00
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// 注册路由
|
|
|
|
|
s.client.RegisterRouter(
|
|
|
|
|
s.OnResponseAuthSummary, // 响应授权信息
|
|
|
|
|
s.OnResponseExampleHello, // 一个tcp请求例子
|
|
|
|
|
)
|
2023-03-13 17:00:46 +08:00
|
|
|
|
|
|
|
|
|
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-03-16 15:35:02 +08:00
|
|
|
|
// Stop 停止服务
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) Stop(ctx context.Context) {
|
2023-05-15 18:37:40 +08:00
|
|
|
|
if s.client != nil && !s.client.IsStop() {
|
2023-03-13 17:00:46 +08:00
|
|
|
|
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
|
|
|
|
// onLoginEvent 登录认证成功事件
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sAuthClient) onLoginEvent() {
|
2023-07-20 18:01:10 +08:00
|
|
|
|
ctx := gctx.New()
|
|
|
|
|
|
|
|
|
|
// 获取授权信息
|
2023-07-24 09:35:30 +08:00
|
|
|
|
_ = s.client.Send(ctx, &servmsg.AuthSummaryReq{})
|
2023-07-20 18:01:10 +08:00
|
|
|
|
|
|
|
|
|
// 测试例子,实际使用时可以注释掉
|
|
|
|
|
s.testExample(ctx)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
g.Log().Debug(ctx, "AuthClient login succeed.")
|
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-07-20 18:01:10 +08:00
|
|
|
|
g.Log().Debug(gctx.New(), "AuthClient closed.")
|
2023-03-13 17:00:46 +08:00
|
|
|
|
}
|
2024-04-22 23:08:40 +08:00
|
|
|
|
|
|
|
|
|
// Summary 获取授权信息
|
|
|
|
|
func (s *sAuthClient) Summary() *servmsgin.AuthSummaryModel {
|
|
|
|
|
if s.summary == nil {
|
|
|
|
|
s.summary = new(servmsgin.AuthSummaryModel)
|
|
|
|
|
}
|
|
|
|
|
return s.summary
|
|
|
|
|
}
|