Skip to content

Commit 8577b98

Browse files
committed
Unit Test cases RSpec and Licence Change and Logger Addition (#15)
* Change in Licence Change repository licence MIT to OpenSocket * Minor changes in library change in reconnection instance method * Add Specs Add Specs for reconnection instance methods and SocketCluster Client instance methods * Update README.md Add Reconnection documentation in README.md * Changes in ScClient Class and Reconnect module Remove reconnect_decay from Reconnection handler Fix Max attempts for reconnection when auto reconnection is enabled * Add Logger added logger methods and messages along with specs * Update initial release, reconnection and logger Updated the initial release version to 0.1.0, added ssl connection provision, added reconnection and logging
1 parent 9b7a61b commit 8577b98

File tree

15 files changed

+631
-60
lines changed

15 files changed

+631
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changelog
22

3-
## 1.0.0
3+
## 0.1.0
44

55
- initial release

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ gemspec
77

88
gem 'websocket-eventmachine-client'
99

10-
gem 'rspec', :require => false, :group => :test
11-
gem 'simplecov', :require => false, :group => :test
10+
gem 'rspec', require: false, group: :test
11+
gem 'simplecov', require: false, group: :test

Gemfile.lock

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
PATH
2+
remote: .
3+
specs:
4+
socketclusterclient (0.1.0)
5+
websocket-eventmachine-client (~> 1.2)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
diff-lcs (1.3)
11+
docile (1.3.1)
12+
eventmachine (1.2.7)
13+
json (2.1.0)
14+
rake (10.5.0)
15+
rspec (3.7.0)
16+
rspec-core (~> 3.7.0)
17+
rspec-expectations (~> 3.7.0)
18+
rspec-mocks (~> 3.7.0)
19+
rspec-core (3.7.1)
20+
rspec-support (~> 3.7.0)
21+
rspec-expectations (3.7.0)
22+
diff-lcs (>= 1.2.0, < 2.0)
23+
rspec-support (~> 3.7.0)
24+
rspec-mocks (3.7.0)
25+
diff-lcs (>= 1.2.0, < 2.0)
26+
rspec-support (~> 3.7.0)
27+
rspec-support (3.7.1)
28+
simplecov (0.16.1)
29+
docile (~> 1.1)
30+
json (>= 1.8, < 3)
31+
simplecov-html (~> 0.10.0)
32+
simplecov-html (0.10.2)
33+
websocket (1.2.8)
34+
websocket-eventmachine-base (1.2.0)
35+
eventmachine (~> 1.0)
36+
websocket (~> 1.0)
37+
websocket-native (~> 1.0)
38+
websocket-eventmachine-client (1.2.0)
39+
websocket-eventmachine-base (~> 1.0)
40+
websocket-native (1.0.0)
41+
42+
PLATFORMS
43+
ruby
44+
45+
DEPENDENCIES
46+
bundler (~> 1.16)
47+
rake (~> 10.0)
48+
rspec
49+
simplecov
50+
socketclusterclient!
51+
websocket-eventmachine-client
52+
53+
BUNDLED WITH
54+
1.16.1

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MIT License
1+
OpenSocket License
22

33
Copyright (c) 2018 Maanav Shah
44

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ Create instance of `Socket` class by passing url of socketcluster-server end-poi
8585
socket.connect
8686
```
8787

88-
- By default reconnection to server is enabled, so to configure delay for connection
88+
- By default reconnection to server is disabled, so to enable automatic reconnection
89+
90+
```ruby
91+
# This will set automatic-reconnection to socketcluster-server repeat it infinitely
92+
socket.set_reconnection(true)
93+
```
94+
95+
- To set delay of reconnecting to server
8996

9097
```ruby
9198
# This will set automatic-reconnection to socketcluster-server with delay of 2 seconds and repeat it infinitely
@@ -100,6 +107,37 @@ Create instance of `Socket` class by passing url of socketcluster-server end-poi
100107
socket.set_reconnection(false)
101108
```
102109

110+
- For reconnection to the server you need to set reconnection strategy
111+
112+
```ruby
113+
# This will set the reconnection strategy
114+
socket.set_reconnection_listener(reconnect_interval, max_reconnect_interval, max_attempts)
115+
116+
# default reconnection strategy
117+
socket.set_reconnection_listener(2000, 30_000, nil)
118+
```
119+
120+
For example,
121+
```ruby
122+
socket.set_reconnection_listener(3000, 30_000, 10) # (reconnect_inverval, max_reconnect_interval, max_attempts)
123+
socket.set_reconnection_listener(3000, 30_000) # (reconnect_inverval, max_reconnect_interval)
124+
```
125+
126+
- You can set reconnect_interval, max_reconnect_interval and max_attempts directly as well
127+
```ruby
128+
socket.reconnect_interval = 2000
129+
socket.max_reconnect_interval = 20_000
130+
socket.max_attempts = 10
131+
```
132+
133+
- To allow reconnection to server, you need to reconnect
134+
135+
```ruby
136+
# This will set automatic-reconnection to socketcluster-server with delay of 2 seconds and repeat it till the maximum attempts are finished
137+
socket.connect
138+
```
139+
140+
103141
Emitting and listening to events
104142
--------------------------------
105143

@@ -227,6 +265,30 @@ Implementing Pub-Sub via channels
227265
end
228266
```
229267

268+
Logger
269+
------
270+
271+
#### Disabling logger
272+
273+
- To get logger for logging of events
274+
275+
```ruby
276+
socket.logger.info("Some information")
277+
socket.logger.warn("Some warning")
278+
```
279+
280+
- To disable logging of events
281+
282+
```ruby
283+
socket.disable_logging
284+
```
285+
286+
- To enable logging of events
287+
288+
```ruby
289+
socket.enable_logging
290+
```
291+
230292
Development
231293
-----------
232294

examples/reconnection.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
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(false) # default reconnection is true
22-
socket.max_attempts = 5
23-
socket.reconnect
21+
socket.set_reconnection_listener(3000, 30_000) # (reconnect_inverval, max_reconnect_interval)
22+
socket.set_reconnection_listener(3000, 30_000, 10) # (reconnect_inverval, max_reconnect_interval, max_attempts)
23+
socket.set_reconnection(true) # to allow reconnection, default reconnection is disabled
24+
socket.max_attempts = 5 # set maximum attempts allowed
25+
socket.connect

examples/ssl_connect.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'socketclusterclient'
2+
3+
on_connect = -> { puts 'on connect got called' }
4+
5+
on_disconnect = -> { puts 'on disconnect got called' }
6+
7+
on_connect_error = -> { puts 'on connect error got called' }
8+
9+
# connection to ssl server at websocket.org
10+
socket = ScClient.new('wss://echo.websocket.org/')
11+
socket.set_basic_listener(on_connect, on_disconnect, on_connect_error)
12+
socket.connect

lib/sc_client.rb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative './socketclusterclient/parser'
77
require_relative './socketclusterclient/data_models'
88
require_relative './socketclusterclient/reconnect'
9+
require_relative './socketclusterclient/log'
910

1011
#
1112
# Class ScClient provides an interface to connect to the socketcluster server
@@ -16,8 +17,9 @@ class ScClient
1617
include Emitter
1718
include DataModels
1819
include Reconnect
20+
include Log
1921

20-
attr_accessor :reconnect_interval, :max_reconnect_interval, :reconnect_decay, :max_attempts, :attempts_made
22+
attr_accessor :reconnect_interval, :max_reconnect_interval, :max_attempts
2123

2224
#
2325
# Initializes instance variables in socketcluster client
@@ -30,12 +32,11 @@ def initialize(url)
3032
@url = url
3133
@acks = {}
3234
@channels = []
33-
@enable_reconnection = true
35+
@enable_reconnection = false
3436
@delay = 3
3537
initialize_emitter
3638
initialize_reconnect
37-
@logger = Logger.new(STDOUT)
38-
@logger.level = Logger::WARN
39+
initialize_logger
3940
end
4041

4142
#
@@ -112,7 +113,7 @@ def ack_block(cid)
112113
end
113114

114115
#
115-
# Connect to the ScServer
116+
# Connects to the ScServer
116117
#
117118
#
118119
#
@@ -121,8 +122,16 @@ def connect
121122
EM.epoll
122123

123124
EM.run do
124-
trap('TERM') { stop }
125-
trap('INT') { stop }
125+
trap('TERM') do
126+
@enable_reconnection = false
127+
disconnect
128+
end
129+
130+
trap('INT') do
131+
@enable_reconnection = false
132+
disconnect
133+
end
134+
126135
@ws = WebSocket::EventMachine::Client.connect(uri: @url)
127136

128137
@ws.onopen do
@@ -132,6 +141,7 @@ def connect
132141
end
133142

134143
@ws.onmessage do |message, _type|
144+
@logger.info("Message received : #{message}") if @logger
135145
if message == '#1'
136146
@ws.send('#2')
137147
else
@@ -176,9 +186,12 @@ def connect
176186
end
177187

178188
@ws.onclose do
179-
@on_disconnected.call if @on_disconnected
180-
if @enable_reconnection
181-
reconnect
189+
if should_reconnect
190+
@reconnect_interval = @max_reconnect_interval if @reconnect_interval > @max_reconnect_interval
191+
sleep(@reconnect_interval / 1000)
192+
@attempts_made += 1
193+
@logger.info("Attempting to reconnect : #{@attempts_made}") if @logger
194+
connect
182195
else
183196
stop
184197
end
@@ -219,6 +232,7 @@ def set_reconnection(enable)
219232
#
220233
#
221234
def disconnect
235+
@on_disconnected.call if @on_disconnected
222236
@enable_reconnection = false
223237
@ws.close
224238
end
@@ -314,13 +328,13 @@ def publish(channel, data)
314328
end
315329

316330
#
317-
# Publish data to the specified channel name
331+
# Publish data to the specified channel name
318332
#
319333
# @param [String] channel A channel name
320334
# @param [String] data Data to be published on the channel
321335
# @param [Lambda] ack Block to execute on publish acknowledgment
322336
#
323-
# @return [<type>] <description>
337+
#
324338
#
325339
def publishack(channel, data, ack)
326340
@ws.send(get_publish_object(channel, data, increment_cnt).to_json)

lib/socketclusterclient/log.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Module Logger provides an interface to log events
3+
#
4+
# @author Maanav Shah <[email protected]>
5+
#
6+
module Log
7+
#
8+
# Initializes logger instance and sets logger level
9+
#
10+
#
11+
#
12+
#
13+
def initialize_logger
14+
@logger = Logger.new(STDOUT)
15+
end
16+
17+
#
18+
# Method to get the logger instance
19+
#
20+
#
21+
# @return [Logger] An instance of logger
22+
#
23+
def logger
24+
initialize_logger unless @logger
25+
@logger
26+
end
27+
28+
#
29+
# Method to disable logging
30+
#
31+
#
32+
#
33+
#
34+
def disable_logging
35+
@logger = nil
36+
end
37+
38+
#
39+
# Method to enable logging
40+
#
41+
#
42+
#
43+
#
44+
def enable_logging
45+
initialize_logger
46+
end
47+
end

0 commit comments

Comments
 (0)