lib/ably/realtime/channel.rb in ably-0.1.6 vs lib/ably/realtime/channel.rb in ably-0.2.0
- old
+ new
@@ -23,10 +23,16 @@
# Channel::STATE.Detached
# Channel::STATE.Failed
#
# @!attribute [r] state
# @return {Ably::Realtime::Connection::STATE} channel state
+ # @!attribute [r] client
+ # @return {Ably::Realtime::Client} Ably client associated with this channel
+ # @!attribute [r] name
+ # @return {String} channel name
+ # @!attribute [r] options
+ # @return {Hash} channel options configured for this channel, see {#initialize} for channel_options
#
class Channel
include Ably::Modules::Conversions
include Ably::Modules::EventEmitter
include Ably::Modules::EventMachineHelpers
@@ -47,13 +53,16 @@
attr_reader :client, :name, :options
# Initialize a new Channel object
#
- # @param client [Ably::Rest::Client]
- # @param name [String] The name of the channel
- # @param channel_options [Hash] Channel options, currently reserved for future Encryption options
+ # @param client [Ably::Rest::Client]
+ # @param name [String] The name of the channel
+ # @param channel_options [Hash] Channel options, currently reserved for Encryption options
+ # @option channel_options [Boolean] :encrypted setting this to true for this channel will encrypt & decrypt all messages automatically
+ # @option channel_options [Hash] :cipher_params A hash of options to configure the encryption. *:key* is required, all other options are optional. See {Ably::Util::Crypto#initialize} for a list of `cipher_params` options
+ #
def initialize(client, name, channel_options = {})
@client = client
@name = name
@options = channel_options.clone.freeze
@subscriptions = Hash.new { |hash, key| hash[key] = [] }
@@ -118,10 +127,11 @@
# Attach to this channel, and call the block if provided when attached.
# Attaching to a channel is implicit in when a message is published or #subscribe is called, so it is uncommon
# to need to call attach explicitly.
#
# @yield [Ably::Realtime::Channel] Block is called as soon as this channel is in the Attached state
+ # @return [void]
#
def attach(&block)
if attached?
block.call self if block_given?
else
@@ -134,10 +144,11 @@
end
# Detach this channel, and call the block if provided when in a Detached or Failed state
#
# @yield [Ably::Realtime::Channel] Block is called as soon as this channel is in the Detached or Failed state
+ # @return [void]
#
def detach(&block)
if detached? || failed?
block.call self if block_given?
else
@@ -170,19 +181,21 @@
# @!attribute [r] __incoming_msgbus__
# @return [Ably::Util::PubSub] Client library internal channel incoming message bus
# @api private
def __incoming_msgbus__
@__incoming_msgbus__ ||= Ably::Util::PubSub.new(
- coerce_into: Proc.new { |event| Models::ProtocolMessage::ACTION(event) }
+ coerce_into: Proc.new { |event| Ably::Models::ProtocolMessage::ACTION(event) }
)
end
private
attr_reader :queue, :subscriptions
def setup_event_handlers
__incoming_msgbus__.subscribe(:message) do |message|
+ message.decode self
+
subscriptions[:all].each { |cb| cb.call(message) }
subscriptions[message.name].each { |cb| cb.call(message) }
end
on(STATE.Attached) do
@@ -222,38 +235,38 @@
end
end
def send_messages_within_protocol_message(messages)
client.connection.send_protocol_message(
- action: Models::ProtocolMessage::ACTION.Message.to_i,
+ action: Ably::Models::ProtocolMessage::ACTION.Message.to_i,
channel: name,
messages: messages
)
end
def send_attach_protocol_message
- send_state_change_protocol_message Models::ProtocolMessage::ACTION.Attach
+ send_state_change_protocol_message Ably::Models::ProtocolMessage::ACTION.Attach
end
def send_detach_protocol_message
- send_state_change_protocol_message Models::ProtocolMessage::ACTION.Detach
+ send_state_change_protocol_message Ably::Models::ProtocolMessage::ACTION.Detach
end
def send_state_change_protocol_message(state)
client.connection.send_protocol_message(
action: state.to_i,
channel: name
)
end
def create_message(name, data)
- model = {
- name: name,
- data: data
- }
- model.merge!(clientId: client.client_id) if client.client_id
+ message = { name: name }
+ message.merge!(data: data) unless data.nil?
+ message.merge!(clientId: client.client_id) if client.client_id
- Models::Message.new(model, nil)
+ Ably::Models::Message.new(message, nil).tap do |message|
+ message.encode self
+ end
end
def rest_channel
client.rest_client.channel(name)
end