diff --git a/io_client_manage.go b/io_client_manage.go index bda9e78..9598e85 100644 --- a/io_client_manage.go +++ b/io_client_manage.go @@ -389,9 +389,7 @@ func (this *ClientManage) CloseClient(key string) error { // CloseClientAll 关闭所有客户端 func (this *ClientManage) CloseClientAll() { - this.mu.Lock() - defer this.mu.Unlock() - for _, v := range this.m { + for _, v := range this.CopyClientMap() { v.CloseAll() } this.m = make(map[string]*Client) diff --git a/io_server.go b/io_server.go index fc0c51b..661a790 100644 --- a/io_server.go +++ b/io_server.go @@ -16,6 +16,7 @@ func RunServer(newListen ListenFunc, options ...OptionServer) error { return s.Run() } +// NewServer 定义Listen,不用忘记运行Run,不要回出现能连接,服务无反应的情况 func NewServer(newListen ListenFunc, options ...OptionServer) (*Server, error) { return NewServerWithContext(context.Background(), newListen, options...) } @@ -36,10 +37,9 @@ func NewServerWithContext(ctx context.Context, newListen func() (Listener, error tag: maps.NewSafe(), listener: listener, } - s.ICloser.Logger = s.Logger - s.ClientManage.Logger = s.Logger + s.SetLogger(s.Logger) //开启基础信息打印 - s.Logger.Debug() + s.Debug() //设置关闭函数 s.ICloser.SetCloseFunc(func(ctx context.Context, msg Message) { //关闭listener @@ -70,9 +70,18 @@ type Server struct { func (this *Server) Debug(b ...bool) { this.Logger.Debug(b...) + this.ICloser.Logger.Debug(b...) this.ClientManage.Logger.Debug(b...) } +func (this *Server) SetLogger(logger Logger) *Server { + l := newLogger(logger) + this.Logger = l + this.ICloser.Logger = l + this.ClientManage.Logger = l + return this +} + func (this *Server) Tag() *maps.Safe { return this.tag } diff --git a/testdata/testserver/main.go b/testdata/testserver/main.go new file mode 100644 index 0000000..88dd1af --- /dev/null +++ b/testdata/testserver/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/injoyai/io" + "github.com/injoyai/io/listen" + "github.com/injoyai/logs" + "time" +) + +func main() { + + listen.RunTCPServer(10086, func(s *io.Server) { + go func() { + for { + <-time.After(time.Second * 5) + logs.Debug(s.GetClientLen()) + } + }() + }) + +}