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.2'
Instance Attribute Summary collapse
-
#_connected_callback ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_message_factory ⇒ Object
readonly
Returns the value of attribute _message_factory.
-
#_pinged_callback ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_subscribed ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_subscribed_callback ⇒ 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
- #pinged(&block) ⇒ Object
-
#received ⇒ 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.
37 38 39 40 41 42 43 44 45 |
# File 'lib/action_cable_client.rb', line 37 def initialize(uri, params = '', connect_on_start = true, headers = {}) @_uri = uri @message_queue = [] @_subscribed = false @_message_factory = MessageFactory.new(params) connect!(headers) if connect_on_start end |
Instance Attribute Details
#_connected_callback ⇒ Object
The queue should store entries in the format:
- action, data
25 26 27 |
# File 'lib/action_cable_client.rb', line 25 def _connected_callback @_connected_callback end |
#_message_factory ⇒ Object (readonly)
Returns the value of attribute _message_factory.
22 23 24 |
# File 'lib/action_cable_client.rb', line 22 def @_message_factory end |
#_pinged_callback ⇒ Object
The queue should store entries in the format:
- action, data
25 26 27 |
# File 'lib/action_cable_client.rb', line 25 def _pinged_callback @_pinged_callback end |
#_subscribed ⇒ Object
The queue should store entries in the format:
- action, data
25 26 27 |
# File 'lib/action_cable_client.rb', line 25 def _subscribed @_subscribed end |
#_subscribed_callback ⇒ Object
The queue should store entries in the format:
- action, data
25 26 27 |
# File 'lib/action_cable_client.rb', line 25 def _subscribed_callback @_subscribed_callback end |
#_uri ⇒ Object (readonly)
Returns the value of attribute _uri.
21 22 23 |
# File 'lib/action_cable_client.rb', line 21 def _uri @_uri end |
#_websocket_client ⇒ Object (readonly)
Returns the value of attribute _websocket_client.
21 22 23 |
# File 'lib/action_cable_client.rb', line 21 def _websocket_client @_websocket_client end |
#message_queue ⇒ Object
The queue should store entries in the format:
- action, data
25 26 27 |
# File 'lib/action_cable_client.rb', line 25 def @message_queue end |
Instance Method Details
#connect!(headers = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/action_cable_client.rb', line 47 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
93 94 95 96 97 98 |
# File 'lib/action_cable_client.rb', line 93 def connected self._connected_callback = Proc.new do subscribe yield end end |
#disconnected ⇒ Object
callback when the server disconnects from the client.
130 131 132 133 134 135 |
# File 'lib/action_cable_client.rb', line 130 def disconnected _websocket_client.onclose do self._subscribed = false yield end end |
#perform(action, data) ⇒ Object
65 66 67 |
# File 'lib/action_cable_client.rb', line 65 def perform(action, data) (action, data) end |
#pinged(&block) ⇒ Object
137 138 139 |
# File 'lib/action_cable_client.rb', line 137 def pinged(&block) self._pinged_callback = block end |
#received ⇒ Object
callback for received messages as well as what triggers depleting the message queue
78 79 80 81 82 83 84 |
# File 'lib/action_cable_client.rb', line 78 def received _websocket_client. do |, _type| () 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
113 114 115 |
# File 'lib/action_cable_client.rb', line 113 def subscribed(&block) self._subscribed_callback = block end |
#subscribed? ⇒ Boolean
Returns is the client subscribed to the channel?.
118 119 120 |
# File 'lib/action_cable_client.rb', line 118 def subscribed? _subscribed end |