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.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.
-
#_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 = '') ⇒ 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 = '') ⇒ ActionCableClient
Returns a new instance of ActionCableClient.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/action_cable_client.rb', line 36 def initialize(uri, channel = '') @_channel_name = channel @_uri = uri @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 |
#_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
53 54 55 |
# File 'lib/action_cable_client.rb', line 53 def perform(action, data) .push([action, data]) end |
#received(skip_pings = true) ⇒ Object
callback for received messages as well as what triggers depleting the message queue
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/action_cable_client.rb', line 69 def received(skip_pings = true) _websocket_client.stream do | | string = .data json = JSON.parse(string) if is_ping?(json) check_for_subscribe_confirmation(json) unless subscribed? yield(json) unless skip_pings else yield(json) end deplete_queue if subscribed? end end |