From b445324bccbe87368be8ea05e6514c9ace2b7a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Mon, 1 Jun 2020 23:37:24 +0800 Subject: [PATCH] add ClientLifecycleCallbacks --- clientconn.go | 9 +++++++++ conf.go | 27 +++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/clientconn.go b/clientconn.go index 5be50dd..acf58ee 100644 --- a/clientconn.go +++ b/clientconn.go @@ -226,8 +226,17 @@ func (conn *Connection) loop() { conn.writeFrames() }) + if conn.conf.LifecycleCallbacks.OnConnect != nil { + conn.conf.LifecycleCallbacks.OnConnect(conn) + } + <-(*conn.loopCtx).Done() conn.closeRWC() + + if conn.conf.LifecycleCallbacks.OnDisconnect != nil { + conn.conf.LifecycleCallbacks.OnDisconnect(conn) + } + conn.loopWG.Wait() conn.endLoop() diff --git a/conf.go b/conf.go index b6262e9..192ed8a 100644 --- a/conf.go +++ b/conf.go @@ -52,18 +52,25 @@ type ServerBinding struct { // SubFunc for subscribe callback type SubFunc func(*Connection, *Frame) +// ClientLifecycleCallbacks for Connection +type ClientLifecycleCallbacks struct { + OnConnect func(*Connection) + OnDisconnect func(*Connection) +} + // ConnectionConfig is conf for Connection type ConnectionConfig struct { - WriteTimeout int - ReadTimeout int - DialTimeout time.Duration - WriteFrameChSize int - WBufSize int // best effort only, check log for error - RBufSize int // best effort only, check log for error - Handler Handler - OverlayNetwork func(address string, dialConfig DialConfig) (net.Conn, error) - Codec CompressorCodec - TLSConf *tls.Config + WriteTimeout int + ReadTimeout int + DialTimeout time.Duration + WriteFrameChSize int + WBufSize int // best effort only, check log for error + RBufSize int // best effort only, check log for error + Handler Handler + OverlayNetwork func(address string, dialConfig DialConfig) (net.Conn, error) + Codec CompressorCodec + TLSConf *tls.Config + LifecycleCallbacks ClientLifecycleCallbacks } // DialConfig for dial