Skip to content

Commit

Permalink
add ClientLifecycleCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiangxu committed Jun 1, 2020
1 parent 9544cc5 commit b445324
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
9 changes: 9 additions & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
27 changes: 17 additions & 10 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b445324

Please sign in to comment.