changed client to retry after disconnect
This commit is contained in:
parent
c80c87c667
commit
fbed26d94b
|
@ -5,6 +5,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
@ -37,11 +38,22 @@ func Run(ctx context.Context, config *Config) error {
|
||||||
|
|
||||||
handler := NewWsHandler(config.Services)
|
handler := NewWsHandler(config.Services)
|
||||||
|
|
||||||
conn, _, err := dialer.Dial(serverURL.String(), nil)
|
authenticated := false
|
||||||
if err != nil {
|
for {
|
||||||
|
if conn, _, err := dialer.Dial(serverURL.String(), nil); err == nil {
|
||||||
|
authenticated = true
|
||||||
|
handler.HandleConn(ctx, conn)
|
||||||
|
} else if !authenticated {
|
||||||
return fmt.Errorf("First connection to server failed - check auth: %v", err)
|
return fmt.Errorf("First connection to server failed - check auth: %v", err)
|
||||||
}
|
}
|
||||||
|
loginfo.Println("disconnected from remote server")
|
||||||
|
|
||||||
handler.HandleConn(ctx, conn)
|
// Sleep for a few seconds before trying again, but only if the context is still active
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
return nil
|
return nil
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
}
|
||||||
|
loginfo.Println("attempting reconnect to remote server")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue