@@ -54,6 +54,8 @@ type RecConn struct {
54
54
httpResp * http.Response
55
55
dialErr error
56
56
dialer * websocket.Dialer
57
+ // if set to true, close stops dial reconnection
58
+ close chan (bool )
57
59
58
60
* websocket.Conn
59
61
}
@@ -87,7 +89,7 @@ func (rc *RecConn) Close() {
87
89
rc .Conn .Close ()
88
90
rc .mu .Unlock ()
89
91
}
90
-
92
+ rc . close <- true
91
93
rc .setIsConnected (false )
92
94
}
93
95
@@ -290,6 +292,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
290
292
log .Fatalf ("Dial: %v" , err )
291
293
}
292
294
295
+ rc .close = make (chan bool , 1 )
293
296
// Config
294
297
rc .setURL (urlStr )
295
298
rc .setReqHeader (reqHeader )
@@ -394,43 +397,48 @@ func (rc *RecConn) connect() {
394
397
rand .Seed (time .Now ().UTC ().UnixNano ())
395
398
396
399
for {
397
- nextItvl := b .Duration ()
398
- wsConn , httpResp , err := rc .dialer .Dial (rc .url , rc .reqHeader )
399
-
400
- rc .mu .Lock ()
401
- rc .Conn = wsConn
402
- rc .dialErr = err
403
- rc .isConnected = err == nil
404
- rc .httpResp = httpResp
405
- rc .mu .Unlock ()
406
-
407
- if err == nil {
408
- if ! rc .getNonVerbose () {
409
- log .Printf ("Dial: connection was successfully established with %s\n " , rc .url )
410
- }
400
+ select {
401
+ case <- rc .close :
402
+ return
403
+ default :
404
+ nextItvl := b .Duration ()
405
+ wsConn , httpResp , err := rc .dialer .Dial (rc .url , rc .reqHeader )
406
+
407
+ rc .mu .Lock ()
408
+ rc .Conn = wsConn
409
+ rc .dialErr = err
410
+ rc .isConnected = err == nil
411
+ rc .httpResp = httpResp
412
+ rc .mu .Unlock ()
413
+
414
+ if err == nil {
415
+ if ! rc .getNonVerbose () {
416
+ log .Printf ("Dial: connection was successfully established with %s\n " , rc .url )
417
+ }
411
418
412
- if rc .hasSubscribeHandler () {
413
- if err := rc .SubscribeHandler (); err != nil {
414
- log .Fatalf ("Dial: connect handler failed with %s" , err .Error ())
419
+ if rc .hasSubscribeHandler () {
420
+ if err := rc .SubscribeHandler (); err != nil {
421
+ log .Fatalf ("Dial: connect handler failed with %s" , err .Error ())
422
+ }
423
+ if ! rc .getNonVerbose () {
424
+ log .Printf ("Dial: connect handler was successfully established with %s\n " , rc .url )
425
+ }
415
426
}
416
- if ! rc .getNonVerbose () {
417
- log .Printf ("Dial: connect handler was successfully established with %s\n " , rc .url )
427
+
428
+ if rc .getKeepAliveTimeout () != 0 {
429
+ rc .keepAlive ()
418
430
}
431
+
432
+ return
419
433
}
420
434
421
- if rc .getKeepAliveTimeout () != 0 {
422
- rc .keepAlive ()
435
+ if ! rc .getNonVerbose () {
436
+ log .Println (err )
437
+ log .Println ("Dial: will try again in" , nextItvl , "seconds." )
423
438
}
424
-
425
- return
426
- }
427
439
428
- if ! rc .getNonVerbose () {
429
- log .Println (err )
430
- log .Println ("Dial: will try again in" , nextItvl , "seconds." )
440
+ time .Sleep (nextItvl )
431
441
}
432
-
433
- time .Sleep (nextItvl )
434
442
}
435
443
}
436
444
0 commit comments