diff --git a/socket/net/client/connector.go b/socket/net/client/connector.go index b12dbd9..cff84b7 100644 --- a/socket/net/client/connector.go +++ b/socket/net/client/connector.go @@ -22,6 +22,8 @@ type Connectors struct { Address string `json:"address,omitempty"` LocalAddress net.Addr `json:"-"` + OnDisconnected func(connector client.Connector) `json:"-"` + stopChan chan struct{} stopWg sync.WaitGroup @@ -102,6 +104,12 @@ RC_LOOP: if 0 >= c.GetReconnectTryTime() { c.reconnectedChan <- nil + + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } + continue RC_LOOP } @@ -118,7 +126,12 @@ RC_LOOP: } time.Sleep(c.GetReconnectInterval()) } + logging.Logger().Debugf("%s reconnecting has been failed", c.logHeader()) + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } } } diff --git a/socket/web/client/connector.go b/socket/web/client/connector.go index 507c016..0345bae 100644 --- a/socket/web/client/connector.go +++ b/socket/web/client/connector.go @@ -52,6 +52,8 @@ type Connectors struct { // If Proxy is nil or returns a nil *URL, no proxy is used. Proxy func(*http.Request) (*url.URL, error) `json:"-"` + OnDisconnected func(connector client.Connector) `json:"-"` + serverURL *url.URL stopChan chan struct{} @@ -139,6 +141,12 @@ RC_LOOP: if 0 >= c.GetReconnectTryTime() { c.reconnectedChan <- nil + + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } + continue RC_LOOP } @@ -161,6 +169,11 @@ RC_LOOP: time.Sleep(c.GetReconnectInterval()) } logging.Logger().Debugf("%s reconnecting has been failed", c.logHeader()) + + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } } }