mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-01-24 19:50:19 +08:00
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package tcpserver
|
||
|
||
import (
|
||
"context"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"hotgo/internal/library/network/tcp"
|
||
"hotgo/internal/service"
|
||
"hotgo/utility/simple"
|
||
)
|
||
|
||
type sTCPServer struct {
|
||
serv *tcp.Server
|
||
}
|
||
|
||
func init() {
|
||
service.RegisterTCPServer(newTCPServer())
|
||
}
|
||
|
||
func newTCPServer() *sTCPServer {
|
||
return &sTCPServer{}
|
||
}
|
||
|
||
// Start 启动服务
|
||
func (s *sTCPServer) Start(ctx context.Context) {
|
||
simple.SafeGo(ctx, func(ctx context.Context) {
|
||
g.Log().Debug(ctx, "TCPServer start..")
|
||
|
||
server, err := tcp.NewServer(&tcp.ServerConfig{
|
||
Name: "hotgo",
|
||
Addr: g.Cfg().MustGet(ctx, "tcp.server.address").String(),
|
||
})
|
||
|
||
if err != nil {
|
||
g.Log().Warningf(ctx, "TCPServer start fail:%+v", err)
|
||
return
|
||
}
|
||
|
||
s.serv = server
|
||
|
||
// 消息队列路由
|
||
s.serv.RegisterQueueRouter(map[string]tcp.RouterHandler{
|
||
// ...
|
||
})
|
||
|
||
// 定时任务路由
|
||
s.serv.RegisterCronRouter(map[string]tcp.RouterHandler{
|
||
// ...
|
||
})
|
||
|
||
// 授权服务路由
|
||
s.serv.RegisterAuthRouter(map[string]tcp.RouterHandler{
|
||
"AuthSummary": s.OnAuthSummary, // 获取授权信息
|
||
})
|
||
|
||
// 服务监听
|
||
if err = s.serv.Listen(); err != nil {
|
||
if !s.serv.IsClose() {
|
||
g.Log().Warningf(ctx, "TCPServer Listen err:%v", err)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
// Stop 关闭服务
|
||
func (s *sTCPServer) Stop(ctx context.Context) {
|
||
if s.serv != nil && !s.serv.IsClose() {
|
||
s.serv.Close()
|
||
g.Log().Debug(ctx, "TCPServer stop..")
|
||
}
|
||
}
|