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.2.2'
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.
-
#_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 ].
-
#subscribed ⇒ Object
(also: #subscribed?)
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.
Constructor Details
#initialize(uri, channel = '', queued_send = false) ⇒ ActionCableClient
Returns a new instance of ActionCableClient.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/action_cable_client.rb', line 38 def initialize(uri, channel = '', queued_send = false) @_channel_name = channel @_uri = uri @_queued_send = queued_send @message_queue = [] @subscribed = false @_message_factory = 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 @_message_factory 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 |
#_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 @message_queue end |
#subscribed ⇒ Object Also known as: subscribed?
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 |
Instance Method Details
#connected ⇒ Object
callback when the client connects to the server
91 92 93 94 95 96 |
# File 'lib/action_cable_client.rb', line 91 def connected _websocket_client.callback do subscribe yield end end |
#perform(action, data) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/action_cable_client.rb', line 56 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
76 77 78 79 80 81 82 |
# File 'lib/action_cable_client.rb', line 76 def received(skip_pings = true) _websocket_client.stream do || (, skip_pings) do |json| yield(json) end end end |