This commit is contained in:
孟帅
2023-07-20 18:01:10 +08:00
parent 9113fc5297
commit 373d9627fb
492 changed files with 12170 additions and 6982 deletions

View File

@@ -1,8 +1,14 @@
// 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
package tcpclient
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"hotgo/internal/library/network/tcp"
"hotgo/internal/service"
"hotgo/utility/simple"
@@ -21,6 +27,11 @@ func newCronClient() *sCronClient {
return &sCronClient{}
}
// Instance 获取实例
func (s *sCronClient) Instance() *tcp.Client {
return s.client
}
// Start 启动服务
func (s *sCronClient) Start(ctx context.Context) {
g.Log().Debug(ctx, "CronClient start..")
@@ -36,42 +47,39 @@ func (s *sCronClient) Start(ctx context.Context) {
return
}
// 创建客户端配置
clientConfig := &tcp.ClientConfig{
Addr: config.Client.Cron.Address,
AutoReconnect: true,
Auth: &tcp.AuthMeta{
Name: config.Client.Cron.Name,
Group: config.Client.Cron.Group,
AppId: config.Client.Cron.AppId,
SecretKey: config.Client.Cron.SecretKey,
},
LoginEvent: s.onLoginEvent,
CloseEvent: s.onCloseEvent,
}
simple.SafeGo(ctx, func(ctx context.Context) {
s.client, err = tcp.NewClient(&tcp.ClientConfig{
Addr: config.Client.Cron.Address,
Auth: &tcp.AuthMeta{
Group: config.Client.Cron.Group,
Name: config.Client.Cron.Name,
AppId: config.Client.Cron.AppId,
SecretKey: config.Client.Cron.SecretKey,
},
LoginEvent: s.onLoginEvent,
CloseEvent: s.onCloseEvent,
})
s.client = tcp.NewClient(clientConfig)
if err != nil {
g.Log().Errorf(ctx, "CronClient NewClient fail%+v", err)
return
}
// 注册RPC路由
s.client.RegisterRPCRouter(
s.OnCronDelete, // 删除任务
s.OnCronEdit, // 编辑任务
s.OnCronStatus, // 修改任务状态
s.OnCronOnlineExec, // 执行一次任务
)
err = s.client.RegisterRouter(map[string]tcp.RouterHandler{
"CronDelete": s.OnCronDelete, // 删除任务
"CronEdit": s.OnCronEdit, // 编辑任务
"CronStatus": s.OnCronStatus, // 修改任务状态
"CronOnlineExec": s.OnCronOnlineExec, // 执行一次任务
})
if err != nil {
g.Log().Errorf(ctx, "CronClient RegisterRouter fail%+v", err)
return
}
// 注册拦截器
s.client.RegisterInterceptor(s.DefaultInterceptor)
if err = s.client.Start(); err != nil {
g.Log().Errorf(ctx, "CronClient Start fail%+v", err)
return
}
})
}
// Stop 停止服务
@@ -82,20 +90,12 @@ func (s *sCronClient) Stop(ctx context.Context) {
}
}
// IsLogin 是否已登录认证
func (s *sCronClient) IsLogin() bool {
if s.client == nil {
return false
}
return s.client.IsLogin
}
// onLoginEvent 登录认证成功事件
func (s *sCronClient) onLoginEvent() {
// ...
g.Log().Debug(gctx.New(), "CronClient login succeed.")
}
// onCloseEvent 连接关闭回调事件
func (s *sCronClient) onCloseEvent() {
// ...
g.Log().Debug(gctx.New(), "CronClient closed.")
}