Skip to content

Commit 701e419

Browse files
committed
Merge branch 'develop' into test
2 parents 2161c4e + a60ab50 commit 701e419

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Or install it yourself as:
3232

3333
$ gem install socketclusterclient
3434

35-
Description
35+
Usage
3636
-----------
3737
Create instance of `Socket` class by passing url of socketcluster-server end-point
3838

@@ -91,7 +91,7 @@ Create instance of `Socket` class by passing url of socketcluster-server end-poi
9191
socket.connect
9292
```
9393

94-
- For disabling reconnection to server
94+
- To disable automatic reconnection to server
9595

9696
```ruby
9797
# This will disable reconnection to socketcluster-server
@@ -225,11 +225,6 @@ Implementing Pub-Sub via channels
225225
end
226226
```
227227

228-
Usage
229-
-----
230-
231-
TODO: Write usage instructions here
232-
233228
Development
234229
-----------
235230

examples/reconnection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
socket = ScClient.new('ws://localhost:8000/socketcluster/')
1919
socket.set_basic_listener(on_connect, on_disconnect, on_connect_error)
2020
socket.set_authentication_listener(on_set_authentication, on_authentication)
21-
socket.set_reconnection(true) # default reconnection is true
22-
socket.set_delay(5) # reconnect in 5 seconds
23-
socket.connect
21+
socket.set_reconnection(false) # default reconnection is true
22+
socket.max_attempts = 5
23+
socket.reconnect

lib/sc_client.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require 'websocket-eventmachine-client'
22
require 'json'
3+
require 'logger'
34

45
require_relative './socketclusterclient/emitter'
56
require_relative './socketclusterclient/parser'
67
require_relative './socketclusterclient/data_models'
8+
require_relative './socketclusterclient/reconnect'
79

810
#
911
# Class ScClient provides an interface to connect to the socketcluster server
@@ -13,6 +15,9 @@
1315
class ScClient
1416
include Emitter
1517
include DataModels
18+
include Reconnect
19+
20+
attr_accessor :reconnect_interval, :max_reconnect_interval, :reconnect_decay, :max_attempts, :attempts_made
1621

1722
#
1823
# Initializes instance variables in socketcluster client
@@ -28,6 +33,9 @@ def initialize(url)
2833
@enable_reconnection = true
2934
@delay = 3
3035
initialize_emitter
36+
initialize_reconnect
37+
@logger = Logger.new(STDOUT)
38+
@logger.level = Logger::WARN
3139
end
3240

3341
#
@@ -340,15 +348,4 @@ def reset_value
340348
def increment_cnt
341349
@cnt += 1
342350
end
343-
344-
#
345-
# Reconnects to ScServer after delay
346-
#
347-
#
348-
#
349-
#
350-
def reconnect
351-
sleep @delay
352-
connect
353-
end
354351
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# Module Reconnect provides Reconnection Support
3+
#
4+
# @author Piyush Wani <[email protected]>
5+
#
6+
module Reconnect
7+
#
8+
# Initializes Reconnection related entities
9+
#
10+
#
11+
#
12+
#
13+
def initialize_reconnect
14+
@reconnect_interval = 2000
15+
@max_reconnect_interval = 30_000
16+
@reconnect_decay = 1
17+
@max_attempts = nil
18+
@attempts_made = 0
19+
end
20+
21+
#
22+
# Adds handler for Reconnection
23+
#
24+
# @param [Integer] reconnect_interval A interval for reconnection attempt( in MiliSeconds )
25+
# @param [Integer] max_reconnect_interval The Max Limit for reconnection interval (in MiliSeconds)
26+
# @param [Integer] reconnect_decay A
27+
# @param [Integer] max_attempts The Maximum number of Reconnection Attempts Allowed
28+
#
29+
#
30+
#
31+
def set_reconnection_listener(reconnect_interval, max_reconnect_interval, reconnect_decay, max_attempts)
32+
@reconnect_interval = (reconnect_interval > max_reconnect_interval) ? max_reconnect_interval : reconnect_interval
33+
@max_reconnect_interval = max_reconnect_interval
34+
@reconnect_decay = reconnect_decay
35+
@max_attempts = max_attempts
36+
@attempts_made = 0
37+
end
38+
39+
#
40+
# Reconnects to ScServer after delay
41+
#
42+
#
43+
#
44+
def reconnect
45+
if @reconnect_interval < @max_reconnect_interval
46+
@reconnect_interval *= @reconnect_decay
47+
@reconnect_interval = @max_reconnect_interval if @reconnect_interval > @max_reconnect_interval
48+
end
49+
50+
until reconnection_attempts_finished
51+
@attempts_made += 1
52+
@logger.warn("Attempt number : #{@attempts_made} ")
53+
connect
54+
sleep(@reconnect_interval / 1000)
55+
end
56+
57+
@attempts_made = 0
58+
@logger.warn('Unable to reconnect: max reconnection attempts reached')
59+
end
60+
61+
private
62+
63+
#
64+
# Checks whether all attempts are finished or not
65+
#
66+
#
67+
# @return [Boolean] Attempts finished
68+
#
69+
def reconnection_attempts_finished
70+
@attempts_made == @max_attempts
71+
end
72+
end

0 commit comments

Comments
 (0)