From bbc5af6a3cae3a8e509b0ffadfd3830d1399f24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=C3=B6ffel?= Date: Tue, 9 Oct 2018 11:08:15 +0200 Subject: [PATCH 1/4] added connect handler and GetURL method --- recws.go | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/recws.go b/recws.go index 63c91aa..35d08b3 100644 --- a/recws.go +++ b/recws.go @@ -35,6 +35,8 @@ type RecConn struct { HandshakeTimeout time.Duration // NonVerbose suppress connecting/reconnecting messages. NonVerbose bool + // ConnectHandler fires after the connection successfully establish. + ConnectHandler func(rc *RecConn) error mu sync.Mutex url string @@ -50,10 +52,7 @@ type RecConn struct { // CloseAndReconnect will try to reconnect. func (rc *RecConn) closeAndReconnect() { rc.Close() - go func() { - rc.connect() - }() - + go rc.connect() } // Close closes the underlying network connection without @@ -180,14 +179,17 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) { rc.dialer = websocket.DefaultDialer rc.dialer.HandshakeTimeout = rc.HandshakeTimeout - go func() { - rc.connect() - }() + go rc.connect() // wait on first attempt time.Sleep(rc.HandshakeTimeout) } +// GetURL returns current connection url +func (rc *RecConn) GetURL() string { + return rc.url +} + func (rc *RecConn) connect() { b := &backoff.Backoff{ Min: rc.RecIntvlMin, @@ -214,12 +216,23 @@ func (rc *RecConn) connect() { if !rc.NonVerbose { log.Printf("Dial: connection was successfully established with %s\n", rc.url) } - break - } else { - if !rc.NonVerbose { - log.Println(err) - log.Println("Dial: will try again in", nextItvl, "seconds.") + + if rc.ConnectHandler == nil { + break } + + if err := rc.ConnectHandler(rc); err != nil { + log.Fatalf("Dial: connect handler failed with %s", err.Error()) + } + + log.Printf("Dial: connect handler was successfully established with %s\n", rc.url) + + return + } + + if !rc.NonVerbose { + log.Println(err) + log.Println("Dial: will try again in", nextItvl, "seconds.") } time.Sleep(nextItvl) @@ -252,3 +265,10 @@ func (rc *RecConn) IsConnected() bool { return rc.isConnected } + +func (rc *RecConn) GetConnection() *RecConn { + rc.mu.Lock() + defer rc.mu.Unlock() + + return rc +} From 7ad86cf9a647bce347918b82a1fdbeb42f454900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=C3=B6ffel?= Date: Tue, 9 Oct 2018 11:10:42 +0200 Subject: [PATCH 2/4] break to return --- recws.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recws.go b/recws.go index 35d08b3..31a7fbc 100644 --- a/recws.go +++ b/recws.go @@ -218,7 +218,7 @@ func (rc *RecConn) connect() { } if rc.ConnectHandler == nil { - break + return } if err := rc.ConnectHandler(rc); err != nil { From 67334dd2414c42d8ddfad989b123abe06a557aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=C3=B6ffel?= Date: Tue, 9 Oct 2018 11:11:31 +0200 Subject: [PATCH 3/4] removed getconnection --- recws.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/recws.go b/recws.go index 31a7fbc..7724aa6 100644 --- a/recws.go +++ b/recws.go @@ -265,10 +265,3 @@ func (rc *RecConn) IsConnected() bool { return rc.isConnected } - -func (rc *RecConn) GetConnection() *RecConn { - rc.mu.Lock() - defer rc.mu.Unlock() - - return rc -} From 6cd130b00b75f4a2948369f742fa2c6d2ec8233b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=C3=B6ffel?= Date: Tue, 9 Oct 2018 11:14:43 +0200 Subject: [PATCH 4/4] renamed to subscribeHandler --- recws.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/recws.go b/recws.go index 7724aa6..a7d5522 100644 --- a/recws.go +++ b/recws.go @@ -35,8 +35,8 @@ type RecConn struct { HandshakeTimeout time.Duration // NonVerbose suppress connecting/reconnecting messages. NonVerbose bool - // ConnectHandler fires after the connection successfully establish. - ConnectHandler func(rc *RecConn) error + // SubscribeHandler fires after the connection successfully establish. + SubscribeHandler func(rc *RecConn) error mu sync.Mutex url string @@ -217,11 +217,11 @@ func (rc *RecConn) connect() { log.Printf("Dial: connection was successfully established with %s\n", rc.url) } - if rc.ConnectHandler == nil { + if rc.SubscribeHandler == nil { return } - if err := rc.ConnectHandler(rc); err != nil { + if err := rc.SubscribeHandler(rc); err != nil { log.Fatalf("Dial: connect handler failed with %s", err.Error()) }