Skip to content

Commit

Permalink
准备修改setPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
injoyai committed Jun 15, 2023
1 parent 3be35bc commit 9592d3e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func (this *Client) SetCloseFunc(fn func(ctx context.Context, msg *IMessage)) {
})
}

func (this *Client) setPrinter(p *printer) {
//this.printer = p
}

// SetPrintFunc 设置打印函数
func (this *Client) SetPrintFunc(fn PrintFunc) *Client {
this.IWriter.SetPrintFunc(fn)
Expand Down
16 changes: 15 additions & 1 deletion io_client_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func NewClientManage(ctx context.Context) *ClientManage {
})
//设置默认处理数据函数
e.SetDealFunc(func(msg *IMessage) { e.readChan <- msg })

//设置前置函数
e.SetBeforeFunc(func(c *Client) error {
//默认连接打印信息
if e.printer != nil {
e.printer.Print(NewMessage("新的客户端连接..."), TagInfo, c.GetKey())
}
return nil
})
//执行超时机制
go func() {
for {
Expand Down Expand Up @@ -73,6 +80,12 @@ type ClientManage struct {

timeout time.Duration //超时时间,小于0是不超时
timeoutInterval time.Duration //超时检测间隔
*printer
}

// setPrinter 设置打印实例
func (this *ClientManage) setPrinter(p *printer) {
this.printer = p
}

// SetReadFunc 设置数据读取
Expand Down Expand Up @@ -195,6 +208,7 @@ func (this *ClientManage) SetClient(c *Client) {
c.SetCloseFunc(this._closeFunc) //连接关闭方法
c.SetReadFunc(this.readFunc) //读取数据方法
c.SetWriteFunc(this.writeFunc) //设置发送函数
c.setPrinter(this.printer) //设置打印实例

// 协程执行,等待连接的后续数据,来决定后续操作
go func(c *Client) {
Expand Down
12 changes: 6 additions & 6 deletions io_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func NewServerWithContext(ctx context.Context, newListen func() (Listener, error
//关闭已连接的客户端,关闭listener后,客户端还能正常通讯
s.ClientManage.CloseClientAll()
})
//设置前置函数
s.ClientManage.SetBeforeFunc(func(c *Client) error {
//默认连接打印信息
s.Print(NewMessage("新的客户端连接..."), TagInfo, c.GetKey())
return nil
})
////设置前置函数
//s.ClientManage.SetBeforeFunc(func(c *Client) error {
// //默认连接打印信息
// s.Print(NewMessage("新的客户端连接..."), TagInfo, c.GetKey())
// return nil
//})
//预设服务处理
s.SetOptions(options...)
return s, nil
Expand Down

0 comments on commit 9592d3e

Please sign in to comment.