socketclusterclient 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.simplecov +1 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +54 -0
- data/LICENSE +21 -0
- data/README.md +307 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/examples/authentication.rb +22 -0
- data/examples/channel.rb +49 -0
- data/examples/emitter.rb +31 -0
- data/examples/receiver.rb +36 -0
- data/examples/reconnection.rb +25 -0
- data/examples/ssl_connect.rb +12 -0
- data/lib/sc_client.rb +365 -0
- data/lib/socketclusterclient.rb +11 -0
- data/lib/socketclusterclient/data_models.rb +120 -0
- data/lib/socketclusterclient/emitter.rb +91 -0
- data/lib/socketclusterclient/log.rb +47 -0
- data/lib/socketclusterclient/parser.rb +39 -0
- data/lib/socketclusterclient/reconnect.rb +47 -0
- data/lib/socketclusterclient/version.rb +3 -0
- data/socketclusterclient.gemspec +32 -0
- metadata +145 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# Module DataModels provides JSON format based serialized and deserialized
|
4
|
+
# objects that ensures tight coupling
|
5
|
+
# @author Piyush Wani <piyushwww13@gmail.com>
|
6
|
+
#
|
7
|
+
module DataModels
|
8
|
+
# Returns a data model for an acknowledgment event
|
9
|
+
#
|
10
|
+
# @param [String] error An acknowledgment event
|
11
|
+
# @param [String] data A data object
|
12
|
+
# @param [Integer] cid A remote counter id
|
13
|
+
#
|
14
|
+
# @return An acknowledgement object
|
15
|
+
#
|
16
|
+
def get_ack_object(error, data, cid)
|
17
|
+
OpenStruct.new(
|
18
|
+
cid: cid,
|
19
|
+
data: data,
|
20
|
+
error: error
|
21
|
+
).to_h
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns a data model for an emitter event
|
25
|
+
#
|
26
|
+
# @param [String] event An emit event
|
27
|
+
# @param [Hash] object A data object
|
28
|
+
#
|
29
|
+
# @return An emit object
|
30
|
+
#
|
31
|
+
def get_emit_object(event, object)
|
32
|
+
OpenStruct.new(
|
33
|
+
event: event,
|
34
|
+
data: object
|
35
|
+
).to_h
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns a data model for an emitter acknowledgment event
|
39
|
+
#
|
40
|
+
# @param [String] event An emitter acknowledgment event
|
41
|
+
# @param [Hash] object A data object
|
42
|
+
# @param [Integer] counter A counter for a particular event
|
43
|
+
#
|
44
|
+
# @return An emitter acknowledgment object
|
45
|
+
#
|
46
|
+
def get_emit_ack_object(event, object, counter)
|
47
|
+
OpenStruct.new(
|
48
|
+
event: event,
|
49
|
+
data: object,
|
50
|
+
c_id: counter
|
51
|
+
).to_h
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns a data model for a handshake event
|
55
|
+
#
|
56
|
+
# @param [Integer] counter A counter for a particular event
|
57
|
+
#
|
58
|
+
# @return A handshake object
|
59
|
+
#
|
60
|
+
def get_handshake_object(counter)
|
61
|
+
OpenStruct.new(
|
62
|
+
cid: counter,
|
63
|
+
data: OpenStruct.new(
|
64
|
+
authToken: @auth_token
|
65
|
+
).to_h,
|
66
|
+
event: '#handshake'
|
67
|
+
).to_h
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns a data model for publish and publish with acknowledgment event
|
71
|
+
#
|
72
|
+
# @param [String] channel A channel for publishing data
|
73
|
+
# @param [String] data A data object
|
74
|
+
# @param [Integer] counter A counter for a particular event
|
75
|
+
#
|
76
|
+
# @return A publish object
|
77
|
+
#
|
78
|
+
def get_publish_object(channel, data, counter)
|
79
|
+
OpenStruct.new(
|
80
|
+
cid: counter,
|
81
|
+
data: OpenStruct.new(
|
82
|
+
channel: channel,
|
83
|
+
data: data
|
84
|
+
).to_h,
|
85
|
+
event: '#publish'
|
86
|
+
).to_h
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns a data model for subscribe and subscribe with acknowledgment event
|
90
|
+
#
|
91
|
+
# @param [String] channel A channel to subscribe
|
92
|
+
# @param [Integer] counter A counter for a particular event
|
93
|
+
#
|
94
|
+
# @return A subscribe object
|
95
|
+
#
|
96
|
+
def get_subscribe_object(channel, counter)
|
97
|
+
OpenStruct.new(
|
98
|
+
event: '#subscribe',
|
99
|
+
data: OpenStruct.new(
|
100
|
+
channel: channel
|
101
|
+
).to_h,
|
102
|
+
cid: counter
|
103
|
+
).to_h
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns a data model for unsubscribe and unsubscribe with acknowledgment event
|
107
|
+
#
|
108
|
+
# @param [String] channel A channel to unsubscribe
|
109
|
+
# @param [Integer] counter A counter for a particular event
|
110
|
+
#
|
111
|
+
# @return An unsubscribe object
|
112
|
+
#
|
113
|
+
def get_unsubscribe_object(channel, counter)
|
114
|
+
OpenStruct.new(
|
115
|
+
event: '#unsubscribe',
|
116
|
+
data: channel,
|
117
|
+
cid: counter
|
118
|
+
).to_h
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
#
|
2
|
+
# Module Emitter provides interface to execute events and acknowledgments
|
3
|
+
#
|
4
|
+
# @author Maanav Shah <shahmaanav07@gmail.com>
|
5
|
+
#
|
6
|
+
module Emitter
|
7
|
+
#
|
8
|
+
# Initiarizes events and acks in emitter
|
9
|
+
#
|
10
|
+
#
|
11
|
+
#
|
12
|
+
#
|
13
|
+
def initialize_emitter
|
14
|
+
@events = {}
|
15
|
+
@events_ack = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Adds a handler for a particular event
|
20
|
+
#
|
21
|
+
# @param [String] key An index to insert handler in events
|
22
|
+
# @param [Lambda] function A block to execute on event
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
def on(key, function)
|
27
|
+
@events[key] = function
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Adds a handler for a particular channel event
|
32
|
+
#
|
33
|
+
# @param [String] key An index to insert handler in events
|
34
|
+
# @param [Lambda] function A block to execute on event
|
35
|
+
#
|
36
|
+
#
|
37
|
+
#
|
38
|
+
def onchannel(key, function)
|
39
|
+
@events[key] = function
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Adds an acknowledgment handler for a particular event
|
44
|
+
#
|
45
|
+
# @param [String] key An index to insert handler in acknowledgment
|
46
|
+
# @param [Lambda] function An acknowledgment block to execute on event
|
47
|
+
#
|
48
|
+
#
|
49
|
+
#
|
50
|
+
def onack(key, function)
|
51
|
+
@events_ack[key] = function
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Executes a handler for a particular event
|
56
|
+
#
|
57
|
+
# @param [String] key An index to insert handler in events
|
58
|
+
# @param [Hash] object Data received from ScServer
|
59
|
+
#
|
60
|
+
#
|
61
|
+
#
|
62
|
+
def execute(key, object)
|
63
|
+
function = @events[key] if @events.key?(key)
|
64
|
+
function.call(key, object) if function
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Checks acknowledgment for an event
|
69
|
+
#
|
70
|
+
# @param [String] key An index to retrieve handler from events
|
71
|
+
#
|
72
|
+
#
|
73
|
+
#
|
74
|
+
def haseventack(key)
|
75
|
+
@events_ack[key]
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Executes a handler and an acknowledgment for a particular event
|
80
|
+
#
|
81
|
+
# @param [String] key An index to retrieve handler from events
|
82
|
+
# @param [Hash] object Data received from ScServer
|
83
|
+
# @param [Lambda] ack A block to execute as acknowledgment
|
84
|
+
#
|
85
|
+
#
|
86
|
+
#
|
87
|
+
def executeack(key, object, ack)
|
88
|
+
function = @events_ack[key] if @events_ack.key?(key)
|
89
|
+
function.call(key, object, ack) if function
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# Module Logger provides an interface to log events
|
3
|
+
#
|
4
|
+
# @author Maanav Shah <shahmaanav07@gmail.com>
|
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,0 +1,39 @@
|
|
1
|
+
#
|
2
|
+
# Module Parser returns the event to be executed
|
3
|
+
#
|
4
|
+
# @author Maanav Shah <shahmaanav07@gmail.com>
|
5
|
+
#
|
6
|
+
module Parser
|
7
|
+
CHECK_AUTHENTICATION = 1
|
8
|
+
PUBLISH = 2
|
9
|
+
REMOVE_AUTHENTICATION = 3
|
10
|
+
SET_AUTHENTICATION = 4
|
11
|
+
EVENT = 5
|
12
|
+
ACKNOWLEDGEMENT = 6
|
13
|
+
|
14
|
+
#
|
15
|
+
# Provides a handler for a particular event
|
16
|
+
#
|
17
|
+
# @param [String] event An event to execute
|
18
|
+
# @param [Integer] rid An id received from ScServer
|
19
|
+
#
|
20
|
+
# @return [Enum] Result of parsing event and rid
|
21
|
+
#
|
22
|
+
def self.parse(event, rid)
|
23
|
+
if event.to_s != ''
|
24
|
+
if event == '#publish'
|
25
|
+
PUBLISH
|
26
|
+
elsif event == '#removeAuthToken'
|
27
|
+
REMOVE_AUTHENTICATION
|
28
|
+
elsif event == '#setAuthToken'
|
29
|
+
SET_AUTHENTICATION
|
30
|
+
else
|
31
|
+
EVENT
|
32
|
+
end
|
33
|
+
elsif rid == 1
|
34
|
+
CHECK_AUTHENTICATION
|
35
|
+
else
|
36
|
+
ACKNOWLEDGEMENT
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# Module Reconnect provides Reconnection Support
|
3
|
+
#
|
4
|
+
# @author Piyush Wani <piyushwww13@gmail.com>
|
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
|
+
@max_attempts = nil # unlimited reconnection attempt
|
17
|
+
@attempts_made = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Adds a handler for Reconnection
|
22
|
+
#
|
23
|
+
# @param [Integer] reconnect_interval A interval for reconnection attempt( in milliseconds )
|
24
|
+
# @param [Integer] max_reconnect_interval A max Limit for reconnection interval (in milliseconds)
|
25
|
+
# @param [Integer] max_attempts A max number of Reconnection Attempts allowed
|
26
|
+
#
|
27
|
+
#
|
28
|
+
#
|
29
|
+
def set_reconnection_listener(reconnect_interval, max_reconnect_interval, max_attempts = @max_attempts)
|
30
|
+
@reconnect_interval = reconnect_interval > max_reconnect_interval ? max_reconnect_interval : reconnect_interval
|
31
|
+
@max_reconnect_interval = max_reconnect_interval
|
32
|
+
@max_attempts = max_attempts
|
33
|
+
@attempts_made = 0
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
#
|
39
|
+
# Check for reconnection to server
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# @return [Boolean] Allow reconnection
|
43
|
+
#
|
44
|
+
def should_reconnect
|
45
|
+
@enable_reconnection && (@max_attempts.nil? || (@attempts_made < @max_attempts))
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'socketclusterclient/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'socketclusterclient'
|
7
|
+
spec.version = Socketclusterclient::VERSION
|
8
|
+
spec.authors = ['Maanav Shah', 'Piyush Wani', 'Sachin Shinde']
|
9
|
+
spec.email = %w[shahmaanav07@gmail.com piyushwww13@gmail.com sachinshinde7676@gmail.com]
|
10
|
+
|
11
|
+
spec.platform = Gem::Platform::RUBY
|
12
|
+
spec.licenses = ['MIT']
|
13
|
+
|
14
|
+
spec.summary = 'Ruby client for socketcluster'
|
15
|
+
spec.description = 'A socketcluster client designed in ruby'
|
16
|
+
spec.homepage = 'https://socketcluster.io/'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 2.2.0'
|
26
|
+
|
27
|
+
spec.add_development_dependency('bundler', '~> 1.16')
|
28
|
+
spec.add_development_dependency('rake', '~> 10.0')
|
29
|
+
spec.add_development_dependency('rspec', '~> 3.0')
|
30
|
+
spec.add_development_dependency('simplecov', '~> 0.16')
|
31
|
+
spec.add_dependency('websocket-eventmachine-client', '~> 1.2')
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: socketclusterclient
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maanav Shah
|
8
|
+
- Piyush Wani
|
9
|
+
- Sachin Shinde
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2018-07-29 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.16'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.16'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '10.0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '10.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '3.0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: simplecov
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0.16'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0.16'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: websocket-eventmachine-client
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.2'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '1.2'
|
85
|
+
description: A socketcluster client designed in ruby
|
86
|
+
email:
|
87
|
+
- shahmaanav07@gmail.com
|
88
|
+
- piyushwww13@gmail.com
|
89
|
+
- sachinshinde7676@gmail.com
|
90
|
+
executables: []
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- ".gitignore"
|
95
|
+
- ".rspec"
|
96
|
+
- ".simplecov"
|
97
|
+
- ".travis.yml"
|
98
|
+
- CHANGELOG.md
|
99
|
+
- Gemfile
|
100
|
+
- Gemfile.lock
|
101
|
+
- LICENSE
|
102
|
+
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- bin/console
|
105
|
+
- bin/setup
|
106
|
+
- examples/authentication.rb
|
107
|
+
- examples/channel.rb
|
108
|
+
- examples/emitter.rb
|
109
|
+
- examples/receiver.rb
|
110
|
+
- examples/reconnection.rb
|
111
|
+
- examples/ssl_connect.rb
|
112
|
+
- lib/sc_client.rb
|
113
|
+
- lib/socketclusterclient.rb
|
114
|
+
- lib/socketclusterclient/data_models.rb
|
115
|
+
- lib/socketclusterclient/emitter.rb
|
116
|
+
- lib/socketclusterclient/log.rb
|
117
|
+
- lib/socketclusterclient/parser.rb
|
118
|
+
- lib/socketclusterclient/reconnect.rb
|
119
|
+
- lib/socketclusterclient/version.rb
|
120
|
+
- socketclusterclient.gemspec
|
121
|
+
homepage: https://socketcluster.io/
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 2.2.0
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.6.14
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Ruby client for socketcluster
|
145
|
+
test_files: []
|