feat: adding client close function
This commit is contained in:
@@ -288,12 +288,13 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
for {
|
||||
log.Println("Attempting reconnect in %v...", backoff)
|
||||
isInnerLoop := true
|
||||
for isInnerLoop {
|
||||
log.Printf("Attempting reconnect in %v...", backoff)
|
||||
select {
|
||||
case <-time.After(backoff):
|
||||
if err := wsClient.connect(); err != nil {
|
||||
log.Println("Reconnect failed: %v", err)
|
||||
log.Printf("Reconnect failed: %v", err)
|
||||
if backoff < maxBackoff {
|
||||
backoff *= 2
|
||||
}
|
||||
@@ -301,7 +302,10 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
}
|
||||
log.Println("Reconnected successfully")
|
||||
backoff = 1 * time.Second
|
||||
break
|
||||
isInnerLoop = false
|
||||
continue
|
||||
case <-wsClient.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
case <-wsClient.ctx.Done():
|
||||
@@ -314,3 +318,19 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
||||
return wsClient.dataChannel
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) Close() error {
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
if wsClient.cancel != nil {
|
||||
wsClient.cancel()
|
||||
}
|
||||
if wsClient.conn != nil {
|
||||
wsClient.conn.Close()
|
||||
}
|
||||
wsClient.isConnected = false
|
||||
return nil
|
||||
})
|
||||
close(wsClient.dataChannel)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user