Class: ActionCableClient
- Inherits:
-
Object
- Object
- ActionCableClient
- Extended by:
- Forwardable
- Defined in:
- lib/action_cable_client.rb,
lib/action_cable_client/errors.rb,
lib/action_cable_client/message.rb,
lib/action_cable_client/version.rb,
lib/action_cable_client/message_factory.rb
Defined Under Namespace
Modules: Errors Classes: Commands, Message, MessageFactory
Constant Summary collapse
- VERSION =
'2.0.0'
Instance Attribute Summary collapse
-
#_message_factory ⇒ Object
readonly
Returns the value of attribute _message_factory.
-
#_subscribed ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_subscribed_callaback ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_uri ⇒ Object
readonly
Returns the value of attribute _uri.
-
#_websocket_client ⇒ Object
readonly
Returns the value of attribute _websocket_client.
-
#message_queue ⇒ Object
The queue should store entries in the format: [ action, data ].
Instance Method Summary collapse
- #connect!(headers = {}) ⇒ Object
-
#connected ⇒ Object
callback when the client connects to the server.
-
#disconnected ⇒ Object
callback when the server disconnects from the client.
-
#initialize(uri, params = '', connect_on_start = true, headers = {}) ⇒ ActionCableClient
constructor
A new instance of ActionCableClient.
- #perform(action, data) ⇒ Object
-
#received(skip_pings = true) ⇒ Object
callback for received messages as well as what triggers depleting the message queue.
-
#subscribed(&block) ⇒ Object
callback when the client receives a confirm_subscription message from the action_cable server.
-
#subscribed? ⇒ Boolean
Is the client subscribed to the channel?.
Constructor Details
#initialize(uri, params = '', connect_on_start = true, headers = {}) ⇒ ActionCableClient
Returns a new instance of ActionCableClient.
38 39 40 41 42 43 44 45 46 |
# File 'lib/action_cable_client.rb', line 38 def initialize(uri, params = '', connect_on_start = true, headers = {}) @_uri = uri = [] @_subscribed = false = MessageFactory.new(params) connect!(headers) if connect_on_start end |
Instance Attribute Details
#_message_factory ⇒ Object (readonly)
Returns the value of attribute _message_factory.
23 24 25 |
# File 'lib/action_cable_client.rb', line 23 def end |
#_subscribed ⇒ Object
The queue should store entries in the format:
- action, data
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 def _subscribed @_subscribed end |
#_subscribed_callaback ⇒ Object
The queue should store entries in the format:
- action, data
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 def _subscribed_callaback @_subscribed_callaback end |
#_uri ⇒ Object (readonly)
Returns the value of attribute _uri.
22 23 24 |
# File 'lib/action_cable_client.rb', line 22 def _uri @_uri end |
#_websocket_client ⇒ Object (readonly)
Returns the value of attribute _websocket_client.
22 23 24 |
# File 'lib/action_cable_client.rb', line 22 def _websocket_client @_websocket_client end |
#message_queue ⇒ Object
The queue should store entries in the format:
- action, data
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 def end |
Instance Method Details
#connect!(headers = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/action_cable_client.rb', line 48 def connect!(headers = {}) # Quick Reference for WebSocket::EM::Client's api # - onopen - called after successfully connecting # - onclose - called after closing connection # - onmessage - called when client recives a message. on `message do |msg, type (text or binary)|`` # - also called when a ping is received # - onerror - called when client encounters an error # - onping - called when client receives a ping from the server # - onpong - called when client receives a pong from the server # - send - sends a message to the server (and also disables any metaprogramming shenanigans :-/) # - close - closes the connection and optionally sends close frame to server. `close(code, data)` # - ping - sends a ping # - pong - sends a pong @_websocket_client = WebSocket::EventMachine::Client.connect(uri: @_uri, headers: headers) end |
#connected ⇒ Object
callback when the client connects to the server
97 98 99 100 101 102 |
# File 'lib/action_cable_client.rb', line 97 def connected _websocket_client.onopen do subscribe yield end end |
#disconnected ⇒ Object
callback when the server disconnects from the client.
134 135 136 137 138 139 |
# File 'lib/action_cable_client.rb', line 134 def disconnected _websocket_client.onclose do self._subscribed = false yield end end |
#perform(action, data) ⇒ Object
66 67 68 |
# File 'lib/action_cable_client.rb', line 66 def perform(action, data) (action, data) end |
#received(skip_pings = true) ⇒ Object
callback for received messages as well as what triggers depleting the message queue
82 83 84 85 86 87 88 |
# File 'lib/action_cable_client.rb', line 82 def received(skip_pings = true) _websocket_client. do |, _type| (, skip_pings) do |json| yield(json) end end end |
#subscribed(&block) ⇒ Object
callback when the client receives a confirm_subscription message from the action_cable server. This is only called once, and signifies that you can now send messages on the channel
117 118 119 |
# File 'lib/action_cable_client.rb', line 117 def subscribed(&block) self._subscribed_callaback = block end |
#subscribed? ⇒ Boolean
Returns is the client subscribed to the channel?.
122 123 124 |
# File 'lib/action_cable_client.rb', line 122 def subscribed? _subscribed end |