diff --git a/socket/net/client/connector.go b/socket/net/client/connector.go index 055ddb9..7d0fbfd 100644 --- a/socket/net/client/connector.go +++ b/socket/net/client/connector.go @@ -87,6 +87,18 @@ func (c *Connectors) logHeader() string { return fmt.Sprintf("Connector[%s]: ", c.Name) } +func (c *Connectors) onDisconnected() { + close(c.readChan) + close(c.writeChan) + + c.reconnectedChan <- nil + + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } +} + func (c *Connectors) handleReconnect() { defer func() { c.stopWg.Done() @@ -101,14 +113,8 @@ RC_LOOP: } if 0 >= c.GetReconnectTryTime() { - c.reconnectedChan <- nil - - onDisconnected := c.OnDisconnected - if nil != onDisconnected { - onDisconnected(c) - } - - continue RC_LOOP + c.onDisconnected() + return } logging.Logger().Debugf("%s connection lost", c.logHeader()) @@ -126,10 +132,8 @@ RC_LOOP: } logging.Logger().Debugf("%s reconnecting has been failed", c.logHeader()) - onDisconnected := c.OnDisconnected - if nil != onDisconnected { - onDisconnected(c) - } + c.onDisconnected() + return } } diff --git a/socket/web/client/connector.go b/socket/web/client/connector.go index 8854174..edadcf1 100644 --- a/socket/web/client/connector.go +++ b/socket/web/client/connector.go @@ -124,6 +124,18 @@ func (c *Connectors) logHeader() string { return fmt.Sprintf("Connector[%s]:", c.Name) } +func (c *Connectors) onDisconnected() { + close(c.readChan) + close(c.writeChan) + + c.reconnectedChan <- nil + + onDisconnected := c.OnDisconnected + if nil != onDisconnected { + onDisconnected(c) + } +} + func (c *Connectors) handleReconnect() { defer func() { c.stopWg.Done() @@ -138,14 +150,8 @@ RC_LOOP: } if 0 >= c.GetReconnectTryTime() { - c.reconnectedChan <- nil - - onDisconnected := c.OnDisconnected - if nil != onDisconnected { - onDisconnected(c) - } - - continue RC_LOOP + c.onDisconnected() + return } logging.Logger().Debugf("%s connection lost", c.logHeader()) @@ -168,10 +174,8 @@ RC_LOOP: } logging.Logger().Debugf("%s reconnecting has been failed", c.logHeader()) - onDisconnected := c.OnDisconnected - if nil != onDisconnected { - onDisconnected(c) - } + c.onDisconnected() + return } }