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 =
'3.1.0'
Instance Attribute Summary collapse
-
#_connected_callback ⇒ Object
Returns the value of attribute _connected_callback.
-
#_disconnected_callback ⇒ Object
Returns the value of attribute _disconnected_callback.
-
#_message_factory ⇒ Object
readonly
Returns the value of attribute _message_factory.
-
#_pinged_callback ⇒ Object
Returns the value of attribute _pinged_callback.
-
#_subscribed ⇒ Object
The queue should store entries in the format: [ action, data ].
-
#_subscribed_callback ⇒ Object
Returns the value of attribute _subscribed_callback.
-
#_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 = {}, tls = {}) ⇒ 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 = {}, tls = {}) ⇒ 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.
- #reconnect! ⇒ Object
-
#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 = {}, tls = {}) ⇒ ActionCableClient
Returns a new instance of ActionCableClient.
39 40 41 42 43 44 45 46 47 |
# File 'lib/action_cable_client.rb', line 39 def initialize(uri, params = '', connect_on_start = true, headers = {}, tls = {}) @_uri = uri @message_queue = [] @_subscribed = false @_message_factory = MessageFactory.new(params) connect!(headers, tls) if connect_on_start end |
Instance Attribute Details
#_connected_callback ⇒ Object
Returns the value of attribute _connected_callback.
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 def _connected_callback @_connected_callback end |
#_disconnected_callback ⇒ Object
Returns the value of attribute _disconnected_callback.
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 def _disconnected_callback @_disconnected_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
Returns the value of attribute _pinged_callback.
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 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
Returns the value of attribute _subscribed_callback.
26 27 28 |
# File 'lib/action_cable_client.rb', line 26 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 = {}, tls = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/action_cable_client.rb', line 49 def connect!(headers = {}, tls = {}) # 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, tls: tls) @_websocket_client.onclose do self._subscribed = false _disconnected_callback&.call end end |
#connected ⇒ Object
callback when the client connects to the server
106 107 108 109 110 |
# File 'lib/action_cable_client.rb', line 106 def connected self._connected_callback = proc do |json| yield(json) end end |
#disconnected ⇒ Object
callback when the server disconnects from the client.
142 143 144 145 146 |
# File 'lib/action_cable_client.rb', line 142 def disconnected self._disconnected_callback = proc do yield end end |
#perform(action, data) ⇒ Object
78 79 80 |
# File 'lib/action_cable_client.rb', line 78 def perform(action, data) (action, data) end |
#pinged(&block) ⇒ Object
148 149 150 |
# File 'lib/action_cable_client.rb', line 148 def pinged(&block) self._pinged_callback = block end |
#received ⇒ Object
callback for received messages as well as what triggers depleting the message queue
91 92 93 94 95 96 97 |
# File 'lib/action_cable_client.rb', line 91 def received _websocket_client. do |, _type| () do |json| yield(json) end end end |
#reconnect! ⇒ Object
70 71 72 73 74 |
# File 'lib/action_cable_client.rb', line 70 def reconnect! uri = URI(@_uri) EventMachine.reconnect uri.host, uri.port, @_websocket_client @_websocket_client.post_init 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
125 126 127 |
# File 'lib/action_cable_client.rb', line 125 def subscribed(&block) self._subscribed_callback = block end |
#subscribed? ⇒ Boolean
Returns is the client subscribed to the channel?.
130 131 132 |
# File 'lib/action_cable_client.rb', line 130 def subscribed? _subscribed end |