2
0

Merge pull request #28 from p4u/master

add a close channel in order to finish the dial loop
This commit is contained in:
Lucas Löffel
2020-10-19 18:07:08 +02:00
committed by GitHub
+9 -1
View File
@@ -54,6 +54,8 @@ type RecConn struct {
httpResp *http.Response httpResp *http.Response
dialErr error dialErr error
dialer *websocket.Dialer dialer *websocket.Dialer
// if set to true, close stops dial reconnection
close chan (bool)
*websocket.Conn *websocket.Conn
} }
@@ -87,7 +89,7 @@ func (rc *RecConn) Close() {
rc.Conn.Close() rc.Conn.Close()
rc.mu.Unlock() rc.mu.Unlock()
} }
rc.close <- true
rc.setIsConnected(false) rc.setIsConnected(false)
} }
@@ -290,6 +292,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
log.Fatalf("Dial: %v", err) log.Fatalf("Dial: %v", err)
} }
rc.close = make(chan bool, 1)
// Config // Config
rc.setURL(urlStr) rc.setURL(urlStr)
rc.setReqHeader(reqHeader) rc.setReqHeader(reqHeader)
@@ -394,6 +397,10 @@ func (rc *RecConn) connect() {
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
for { for {
select {
case <-rc.close:
return
default:
nextItvl := b.Duration() nextItvl := b.Duration()
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader) wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)
@@ -433,6 +440,7 @@ func (rc *RecConn) connect() {
time.Sleep(nextItvl) time.Sleep(nextItvl)
} }
} }
}
// GetHTTPResponse returns the http response from the handshake. // GetHTTPResponse returns the http response from the handshake.
// Useful when WebSocket handshake fails, // Useful when WebSocket handshake fails,