action_cable_client 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +18 -14
- data/lib/action_cable_client.rb +9 -11
- data/lib/action_cable_client/message.rb +12 -1
- data/lib/action_cable_client/version.rb +1 -1
- metadata +4 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4085894ce47fa6d116953ecdaf16770f7849cb27
|
4
|
+
data.tar.gz: 654c166304ca757a36fd0c4d3755ee132fdbe497
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 368ca003b9182f912b7b81b3b4d5715d33c33cb047f16d3b6ae29a38a722e13494f60837e1aa05c7608a8628311aa585c2769407664557a735e14af4513430d8
|
7
|
+
data.tar.gz: c99816b5ede4d23cf3442b00bf9eb7feadf26a0a3c811c63c65ca8e48ecd1997858a8cedfda6376c6de17882e986003f08b791610a9a48cd0b77b83ace70d077
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -5,10 +5,7 @@
|
|
5
5
|
[![Test Coverage](https://codeclimate.com/github/NullVoxPopuli/action_cable_client/badges/coverage.svg)](https://codeclimate.com/github/NullVoxPopuli/action_cable_client/coverage)
|
6
6
|
[![Dependency Status](https://gemnasium.com/badges/github.com/NullVoxPopuli/action_cable_client.svg)](https://gemnasium.com/github.com/NullVoxPopuli/action_cable_client)
|
7
7
|
|
8
|
-
|
9
|
-
There are quite a few WebSocket Servers out there. The popular ones seem to be: [faye-websocket-ruby](https://github.com/faye/faye-websocket-ruby), [em-websockets](https://github.com/igrigorik/em-websocket). The client-only websocket gems are kinda hard to find, and are only raw websocket support. Rails has a thin layer on top of web sockets to help manage subscribing to channels and send/receive on channels.
|
10
|
-
|
11
|
-
This gem is a wrapper around [em-websocket-client](https://github.com/mwylde/em-websocket-client/), and supports the Rails Action Cable protocol.
|
8
|
+
This gem is a wrapper around [websocket-eventmachine-client](https://github.com/imanel/websocket-eventmachine-client), and supports the Rails Action Cable protocol.
|
12
9
|
|
13
10
|
## Usage
|
14
11
|
|
@@ -29,8 +26,7 @@ EventMachine.run do
|
|
29
26
|
puts message
|
30
27
|
end
|
31
28
|
|
32
|
-
#
|
33
|
-
# a ping from the server
|
29
|
+
# Sends a message to the sever, with the 'action', 'speak'
|
34
30
|
client.perform('speak', { message: 'hello from amc' })
|
35
31
|
end
|
36
32
|
```
|
@@ -45,23 +41,23 @@ The available hooks to tie in to are:
|
|
45
41
|
- `subscribed {}`
|
46
42
|
- `errored { |msg| }`
|
47
43
|
- `received { |msg }`
|
44
|
+
- `pinged { |msg| }`
|
48
45
|
|
49
46
|
|
50
47
|
#### Connecting on initialization is also configurable.
|
51
48
|
|
52
49
|
```ruby
|
53
|
-
client = ActionCableClient.new(uri, 'RoomChannel',
|
54
|
-
client.connect!
|
50
|
+
client = ActionCableClient.new(uri, 'RoomChannel', false)
|
51
|
+
client.connect!(headers = {})
|
55
52
|
```
|
56
53
|
|
57
|
-
this way if you also enable ping receiving via
|
58
54
|
```ruby
|
59
|
-
client.
|
60
|
-
#
|
55
|
+
client.pinged do |_data|
|
56
|
+
# you could track the time since you last received a ping, if you haven't
|
57
|
+
# received one in a while, it could be that your client is disconnected.
|
61
58
|
end
|
62
59
|
```
|
63
60
|
|
64
|
-
you could track the time since you last received a ping, if you haven't received one in a while, it could be that your client is disconnected.
|
65
61
|
|
66
62
|
To reconnect,
|
67
63
|
|
@@ -84,6 +80,16 @@ then on the server end, in your Channel, `params` will give you:
|
|
84
80
|
}
|
85
81
|
```
|
86
82
|
|
83
|
+
#### Using Headers
|
84
|
+
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
params = { channel: 'RoomChannel', favorite_color: 'blue' }
|
88
|
+
client = ActionCableClient.new(uri, params, true, {
|
89
|
+
'Authorization' => 'Bearer token'
|
90
|
+
})
|
91
|
+
```
|
92
|
+
|
87
93
|
|
88
94
|
## Demo
|
89
95
|
|
@@ -111,8 +117,6 @@ There really isn't that much to this gem. :-)
|
|
111
117
|
|
112
118
|
4. Notes:
|
113
119
|
- Every message sent to the server has a `command` and `identifier` key.
|
114
|
-
- Ping messages from the action cable server look like:
|
115
|
-
- `{ "type" => "ping", "message" => 1461845503 }`
|
116
120
|
- The channel value must match the `name` of the channel class on the ActionCable server.
|
117
121
|
- `identifier` and `data` are redundantly jsonified. So, for example (in ruby):
|
118
122
|
```ruby
|
data/lib/action_cable_client.rb
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
# required gems
|
4
4
|
require 'websocket-eventmachine-client'
|
5
5
|
require 'forwardable'
|
6
|
-
require 'active_support/core_ext/string'
|
7
6
|
require 'json'
|
8
7
|
|
9
8
|
# local files
|
@@ -23,7 +22,7 @@ class ActionCableClient
|
|
23
22
|
attr_reader :_message_factory
|
24
23
|
# The queue should store entries in the format:
|
25
24
|
# [ action, data ]
|
26
|
-
attr_accessor :message_queue, :_subscribed, :_subscribed_callaback
|
25
|
+
attr_accessor :message_queue, :_subscribed, :_subscribed_callaback, :_pinged_callback
|
27
26
|
|
28
27
|
def_delegator :_websocket_client, :onerror, :errored
|
29
28
|
def_delegator :_websocket_client, :send, :send_msg
|
@@ -70,18 +69,15 @@ class ActionCableClient
|
|
70
69
|
# callback for received messages as well as
|
71
70
|
# what triggers depleting the message queue
|
72
71
|
#
|
73
|
-
# @param [Boolean] skip_pings - by default, messages
|
74
|
-
# with the identifier '_ping' are skipped
|
75
|
-
#
|
76
72
|
# @example
|
77
73
|
# client = ActionCableClient.new(uri, 'RoomChannel')
|
78
74
|
# client.received do |message|
|
79
75
|
# # the received message will be JSON
|
80
76
|
# puts message
|
81
77
|
# end
|
82
|
-
def received
|
78
|
+
def received
|
83
79
|
_websocket_client.onmessage do |message, _type|
|
84
|
-
handle_received_message(message
|
80
|
+
handle_received_message(message) do |json|
|
85
81
|
yield(json)
|
86
82
|
end
|
87
83
|
end
|
@@ -138,17 +134,19 @@ class ActionCableClient
|
|
138
134
|
end
|
139
135
|
end
|
140
136
|
|
137
|
+
def pinged(&block)
|
138
|
+
self._pinged_callback = block
|
139
|
+
end
|
140
|
+
|
141
141
|
private
|
142
142
|
|
143
143
|
# @param [String] message - the websockt message object
|
144
|
-
|
145
|
-
# with the identifier '_ping' are skipped
|
146
|
-
def handle_received_message(message, skip_pings = true)
|
144
|
+
def handle_received_message(message)
|
147
145
|
return if message.empty?
|
148
146
|
json = JSON.parse(message)
|
149
147
|
|
150
148
|
if is_ping?(json)
|
151
|
-
|
149
|
+
_pinged_callback&.call(json)
|
152
150
|
elsif !subscribed?
|
153
151
|
check_for_subscribe_confirmation(json)
|
154
152
|
else
|
@@ -26,9 +26,20 @@ class ActionCableClient
|
|
26
26
|
identifier: _identifier.to_json
|
27
27
|
}
|
28
28
|
|
29
|
-
hash[:data] = _data.to_json if
|
29
|
+
hash[:data] = _data.to_json if present?(_data)
|
30
30
|
|
31
31
|
hash.to_json
|
32
32
|
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def present?(data)
|
37
|
+
case data
|
38
|
+
when String
|
39
|
+
!(data.empty? || /\A[[:space:]]*\z/.match(data))
|
40
|
+
else
|
41
|
+
data.respond_to?(:empty?) ? !data.empty? : !!data
|
42
|
+
end
|
43
|
+
end
|
33
44
|
end
|
34
45
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_cable_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L. Preston Sego III
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 5.0.0.beta4
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 5.0.0.beta4
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: websocket-eventmachine-client
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,9 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
114
|
version: '0'
|
129
115
|
requirements: []
|
130
116
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
117
|
+
rubygems_version: 2.6.11
|
132
118
|
signing_key:
|
133
119
|
specification_version: 4
|
134
|
-
summary: ActionCableClient-2.0.
|
120
|
+
summary: ActionCableClient-2.0.1
|
135
121
|
test_files: []
|
136
|
-
has_rdoc:
|