htmon-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/htmon-client.gemspec +26 -0
- data/lib/htmon/client.rb +137 -0
- data/lib/htmon/client/version.rb +5 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d9563ea02f37218a9bfe83d1a35afc325ba6aaaf
|
4
|
+
data.tar.gz: 81c0b262b988fbbc77e6549f5b5b861f834384e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2bff3bc01d0c2e07971cc1c4b5558e8c04b202ba055190305832c8c55b25406858091de94d2cc96be9fde3120c6591ba80e0436857476baf2ed86f26881fac9
|
7
|
+
data.tar.gz: 6a47b8845797983e42d36686aeb41ebf16f22ea156a8699160982ccf3cdc39a08bf5da71dc8f899894e91e5eaf104a8bf69763251704c661402b1b9856533257
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Tim Foerster
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Htmon::Client
|
2
|
+
|
3
|
+
A Faye-Websocket based Websocket or ActionCable client.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'htmon-client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install htmon-client
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Initialize client, which automatically connects and reconnects to server, by default.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
client = Htmon::Client.new(server: 'ws://example.org/cable', channels: 'SystemChannel')
|
27
|
+
```
|
28
|
+
|
29
|
+
The autoreconnect could be disables by passing autoconnect: false to constructor.
|
30
|
+
|
31
|
+
You are able to react on open and receive events.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# Globally
|
35
|
+
Htmon::Client.on_receive do |mesage|
|
36
|
+
# content will be dumped and replied
|
37
|
+
{ command: :message ... }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Instance based
|
41
|
+
client.on_receive do |mesage|
|
42
|
+
# content will be dumped and replied
|
43
|
+
{ command: :message ... }
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
If a callbacks will return *nil*, then the reply will be skipped.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/timmyArch/htmon-client.
|
52
|
+
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
57
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "htmon/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'htmon/client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "htmon-client"
|
8
|
+
spec.version = Htmon::Client::VERSION
|
9
|
+
spec.authors = ["Tim Foerster"]
|
10
|
+
spec.email = ["github@mailserver.1n3t.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Websocket client}
|
13
|
+
spec.homepage = "https://github.com/timmyArch/htmon-client"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_dependency "activesupport"
|
25
|
+
spec.add_dependency "faye-websocket"
|
26
|
+
end
|
data/lib/htmon/client.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
require "htmon/client/version"
|
2
|
+
require 'faye/websocket'
|
3
|
+
require 'eventmachine'
|
4
|
+
require 'logger'
|
5
|
+
require 'json'
|
6
|
+
require 'active_support/core_ext/time'
|
7
|
+
|
8
|
+
Thread.abort_on_exception = true
|
9
|
+
|
10
|
+
module Htmon
|
11
|
+
class Client
|
12
|
+
|
13
|
+
attr_reader :server, :autoconnect, :channels
|
14
|
+
|
15
|
+
attr_accessor :reconnect, :timeout
|
16
|
+
|
17
|
+
def self.on_receive &block
|
18
|
+
callbacks[:receive] << block
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_receive &block
|
22
|
+
callbacks[:receive] << block
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.on_open &block
|
26
|
+
callbacks[:open] << block
|
27
|
+
end
|
28
|
+
|
29
|
+
def on_open &block
|
30
|
+
callbacks[:open] << block
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize server: nil, channels: [], autoconnect: true, timeout: 2
|
34
|
+
@autoconnect, @server = autoconnect, server
|
35
|
+
@timeout = timeout
|
36
|
+
@channels = [channels].flatten
|
37
|
+
connect! if autoconnect
|
38
|
+
autoconnect! if autoconnect
|
39
|
+
register_subscriber_callbacks
|
40
|
+
end
|
41
|
+
|
42
|
+
def connect!
|
43
|
+
@thread ||= Thread.new do
|
44
|
+
EM.run do
|
45
|
+
ws = Faye::WebSocket::Client.new(server)
|
46
|
+
|
47
|
+
ws.on :open do |event|
|
48
|
+
logger.info "Connection opened"
|
49
|
+
(self.class.callbacks[:open] + callbacks[:open]).each do |x|
|
50
|
+
Timeout::timeout(timeout) do
|
51
|
+
ret = x.call(event)
|
52
|
+
ws.send JSON.dump(ret) if ret
|
53
|
+
end rescue nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ws.on :message do |event|
|
58
|
+
logger.debug "Message received: #{event.data.inspect}"
|
59
|
+
self.reconnect = Time.now + 5
|
60
|
+
(self.class.callbacks[:receive] + callbacks[:receive]).each do |x|
|
61
|
+
Timeout::timeout(timeout) do
|
62
|
+
ret = x.call(event.date)
|
63
|
+
ws.send JSON.dump(ret) if ret
|
64
|
+
end rescue nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
ws.on :close do |event|
|
69
|
+
logger.info("Connection closed { code: #{event.code},"+
|
70
|
+
" reason: #{event.reason.inspect} }")
|
71
|
+
end
|
72
|
+
|
73
|
+
ws.on :error do |event|
|
74
|
+
logger.info("Connection error occured... #{event.inspect}")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def reconnect!
|
81
|
+
disconnect! and connect!
|
82
|
+
end
|
83
|
+
|
84
|
+
def disconnect!
|
85
|
+
@thread.kill rescue nil
|
86
|
+
ensure
|
87
|
+
@thread = nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.logger
|
91
|
+
@logger ||= Logger.new(STDOUT)
|
92
|
+
end
|
93
|
+
|
94
|
+
def logger
|
95
|
+
@logger ||= begin
|
96
|
+
l = self.class.logger.dup
|
97
|
+
l.instance_variable_set(:@progname,
|
98
|
+
"Websocket##{server}")
|
99
|
+
l
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.callbacks
|
104
|
+
@callbacks ||= { open: [],
|
105
|
+
receive: [] }
|
106
|
+
end
|
107
|
+
|
108
|
+
def callbacks
|
109
|
+
@callbacks ||= { open: [],
|
110
|
+
receive: [] }
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def autoconnect!
|
116
|
+
@autoconnect_poller ||= Thread.new do
|
117
|
+
loop do
|
118
|
+
if self.reconnect and self.reconnect.past?
|
119
|
+
logger.info "Connection will be restored ... "
|
120
|
+
reconnect!
|
121
|
+
end
|
122
|
+
sleep 1
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def register_subscriber_callbacks
|
128
|
+
channels.each do |x|
|
129
|
+
on_open do
|
130
|
+
{ command: :subscribe, identifier: JSON.dump(channel: x) }
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: htmon-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Foerster
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faye-websocket
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- github@mailserver.1n3t.de
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- htmon-client.gemspec
|
100
|
+
- lib/htmon/client.rb
|
101
|
+
- lib/htmon/client/version.rb
|
102
|
+
homepage: https://github.com/timmyArch/htmon-client
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.5.1
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Websocket client
|
126
|
+
test_files: []
|