class AsyncCable::Connection
Attributes
Public Class Methods
Transmit data to all WS connections in current channel and provided stream. @param data [Hash]
# File lib/async_cable/connection.rb, line 29 def broadcast(stream_name, data) logger.debug { "#{name}.broadcast data=#{data.inspect}" } Registry.each(channel_name, stream_name) do |conn| conn.transmit(data) unless conn.closed? end end
@return [String]
# File lib/async_cable/connection.rb, line 18 def channel_name @channel_name end
sets channel_name
for connection class
# File lib/async_cable/connection.rb, line 13 def identified_as(channel) @channel_name = channel.to_s end
# File lib/async_cable/connection.rb, line 8 def inherited(subclass) subclass.identified_as subclass.name.demodulize.underscore end
@return [Logger]
# File lib/async_cable/connection.rb, line 23 def logger AsyncCable.config.logger end
# File lib/async_cable/connection.rb, line 42 def initialize(*args, &block) super @mutex = Mutex.new end
Public Instance Methods
@return [String]
# File lib/async_cable/connection.rb, line 124 def channel_name self.class.channel_name end
Was WS connection closed clean or dirty. @return [Boolean]
# File lib/async_cable/connection.rb, line 98 def close_clean? close_code == Protocol::WebSocket::Error::NO_ERROR end
WS connection close code 1000 - clean close @return [Integer]
# File lib/async_cable/connection.rb, line 92 def close_code @close_code || Protocol::WebSocket::Error::NO_ERROR end
Called when connection being closed. @see close_code
close_reason
for close details.
# File lib/async_cable/connection.rb, line 116 def handle_close logger.debug { "#{self.class}#handle_close clean=#{close_clean?} code=#{close_code} reason=#{close_reason}" } close Registry.remove(channel_name, stream_name, self) on_close end
Called when connection being opened. @param env [Hash] @raise [AsyncCable::Errors::StreamNameNotSet] if stream_for
was not called @raise [AsyncCable::Errors::UnauthorizedError] if reject_unauthorized
was called
# File lib/async_cable/connection.rb, line 106 def handle_open(env) logger.debug { "#{self.class}#handle_open" } @request = Rack::Request.new(env) on_open raise Errors::StreamNameNotSet, self.class.name unless defined?(@stream_name) Registry.add(channel_name, stream_name, self) end
@return [Logger]
# File lib/async_cable/connection.rb, line 129 def logger self.class.logger end
Will be executed when WS connection closed. see close_code
, close_reason
for details.
# File lib/async_cable/connection.rb, line 59 def on_close end
Will be executed when data received from WS client. @param data [Hash]
# File lib/async_cable/connection.rb, line 54 def on_data(data) end
Will be executed when WS connection opened. stream_for
must be called here with stream name
# File lib/async_cable/connection.rb, line 49 def on_open end
@param stream_name
[String]
# File lib/async_cable/connection.rb, line 74 def stream_for(stream_name) @stream_name = stream_name end
@return [String] stream name
# File lib/async_cable/connection.rb, line 79 def stream_name @stream_name end
call this method to transmit data to current WS client @param data [Hash]
# File lib/async_cable/connection.rb, line 64 def transmit(data) logger.debug { "#{self.class}#transmit stream_name=#{stream_name} data=#{data.inspect}" } @mutex.synchronize do write(data) flush end end