Class: ActionCableClient
- Inherits:
-
Object
- Object
- ActionCableClient
- Extended by:
- Forwardable
- Defined in:
- lib/action_cable_client.rb,
lib/action_cable_client/message.rb,
lib/action_cable_client/version.rb,
lib/action_cable_client/message_factory.rb
Defined Under Namespace
Classes: Commands, Message, MessageFactory
Constant Summary collapse
- VERSION =
'1.3.0'
Instance Attribute Summary collapse
-
#_channel_name ⇒ Object
readonly
Returns the value of attribute _channel_name.
-
#_message_factory ⇒ Object
readonly
Returns the value of attribute _message_factory.
-
#_queued_send ⇒ Object
readonly
Returns the value of attribute _queued_send.
-
#_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
-
#connected ⇒ Object
callback when the client connects to the server.
-
#initialize(uri, channel = '', queued_send = false) ⇒ 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, channel = '', queued_send = false) ⇒ ActionCableClient
Returns a new instance of ActionCableClient.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/action_cable_client.rb', line 35 def initialize(uri, channel = '', queued_send = false) @_channel_name = channel @_uri = uri @_queued_send = queued_send = [] @_subscribed = false = MessageFactory.new(channel) # NOTE: # EventMachine::WebSocketClient # https://github.com/mwylde/em-websocket-client/blob/master/lib/em-websocket-client.rb # is a subclass of # https://github.com/eventmachine/eventmachine/blob/master/lib/em/connection.rb @_websocket_client = EventMachine::WebSocketClient.connect(_uri) end |
Instance Attribute Details
#_channel_name ⇒ Object (readonly)
Returns the value of attribute _channel_name.
20 21 22 |
# File 'lib/action_cable_client.rb', line 20 def _channel_name @_channel_name end |
#_message_factory ⇒ Object (readonly)
Returns the value of attribute _message_factory.
21 22 23 |
# File 'lib/action_cable_client.rb', line 21 def end |
#_queued_send ⇒ Object (readonly)
Returns the value of attribute _queued_send.
20 21 22 |
# File 'lib/action_cable_client.rb', line 20 def _queued_send @_queued_send end |
#_subscribed ⇒ Object
The queue should store entries in the format:
- action, data
24 25 26 |
# File 'lib/action_cable_client.rb', line 24 def _subscribed @_subscribed end |
#_subscribed_callaback ⇒ Object
The queue should store entries in the format:
- action, data
24 25 26 |
# File 'lib/action_cable_client.rb', line 24 def _subscribed_callaback @_subscribed_callaback end |
#_uri ⇒ Object (readonly)
Returns the value of attribute _uri.
20 21 22 |
# File 'lib/action_cable_client.rb', line 20 def _uri @_uri end |
#_websocket_client ⇒ Object (readonly)
Returns the value of attribute _websocket_client.
20 21 22 |
# File 'lib/action_cable_client.rb', line 20 def _websocket_client @_websocket_client end |
#message_queue ⇒ Object
The queue should store entries in the format:
- action, data
24 25 26 |
# File 'lib/action_cable_client.rb', line 24 def end |
Instance Method Details
#connected ⇒ Object
callback when the client connects to the server
88 89 90 91 92 93 |
# File 'lib/action_cable_client.rb', line 88 def connected _websocket_client.callback do subscribe yield end end |
#perform(action, data) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/action_cable_client.rb', line 53 def perform(action, data) if _queued_send .push([action, data]) else (action, data) end end |
#received(skip_pings = true) ⇒ Object
callback for received messages as well as what triggers depleting the message queue
73 74 75 76 77 78 79 |
# File 'lib/action_cable_client.rb', line 73 def received(skip_pings = true) _websocket_client.stream do || (, 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
108 109 110 |
# File 'lib/action_cable_client.rb', line 108 def subscribed(&block) self._subscribed_callaback = block end |
#subscribed? ⇒ Boolean
Returns is the client subscribed to the channel?.
113 114 115 |
# File 'lib/action_cable_client.rb', line 113 def subscribed? _subscribed end |