Class: AsyncCable::Connection
- Inherits:
-
Async::WebSocket::Connection
- Object
- Async::WebSocket::Connection
- AsyncCable::Connection
- Defined in:
- lib/async_cable/connection.rb
Instance Attribute Summary collapse
-
#close_code ⇒ Integer
WS connection close code 1000 - clean close.
-
#close_reason ⇒ Object
Returns the value of attribute close_reason.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
-
.broadcast(stream_name, data) ⇒ Object
Transmit data to all WS connections in current channel and provided stream.
- .channel_name ⇒ Object
- .identified_as(channel) ⇒ Object
- .inherited(subclass) ⇒ Object
- .logger ⇒ Object
Instance Method Summary collapse
- #channel_name ⇒ Object
-
#close_clean? ⇒ Boolean
Was WS connection closed clean or dirty.
- #handle_close ⇒ Object
- #handle_open(env) ⇒ Object
-
#initialize(*args, &block) ⇒ Connection
constructor
A new instance of Connection.
- #logger ⇒ Object
-
#on_close ⇒ Object
Will be executed when WS connection closed.
-
#on_data(data) ⇒ Object
Will be executed when data received from WS client.
-
#on_open ⇒ Object
Will be executed when WS connection opened.
- #reject_unauthorized(reason = nil) ⇒ Object
- #stream_for(stream_name) ⇒ Object
-
#stream_name ⇒ String
Stream name.
-
#transmit(data) ⇒ Object
call this method to transmit data to current WS client.
Constructor Details
#initialize(*args, &block) ⇒ Connection
Returns a new instance of Connection.
39 40 41 42 |
# File 'lib/async_cable/connection.rb', line 39 def initialize(*args, &block) super @mutex = Mutex.new end |
Instance Attribute Details
#close_code ⇒ Integer
WS connection close code 1000 - clean close
87 88 89 |
# File 'lib/async_cable/connection.rb', line 87 def close_code @close_code || Protocol::WebSocket::Error::NO_ERROR end |
#close_reason ⇒ Object
Returns the value of attribute close_reason.
37 38 39 |
# File 'lib/async_cable/connection.rb', line 37 def close_reason @close_reason end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
35 36 37 |
# File 'lib/async_cable/connection.rb', line 35 def request @request end |
Class Method Details
.broadcast(stream_name, data) ⇒ Object
Transmit data to all WS connections in current channel and provided stream.
26 27 28 29 30 31 32 |
# File 'lib/async_cable/connection.rb', line 26 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 |
.channel_name ⇒ Object
16 17 18 |
# File 'lib/async_cable/connection.rb', line 16 def channel_name @channel_name end |
.identified_as(channel) ⇒ Object
12 13 14 |
# File 'lib/async_cable/connection.rb', line 12 def identified_as(channel) @channel_name = channel.to_s end |
.inherited(subclass) ⇒ Object
8 9 10 |
# File 'lib/async_cable/connection.rb', line 8 def inherited(subclass) subclass.identified_as subclass.name.demodulize.underscore end |
.logger ⇒ Object
20 21 22 |
# File 'lib/async_cable/connection.rb', line 20 def logger AsyncCable.config.logger end |
Instance Method Details
#channel_name ⇒ Object
115 116 117 |
# File 'lib/async_cable/connection.rb', line 115 def channel_name self.class.channel_name end |
#close_clean? ⇒ Boolean
Was WS connection closed clean or dirty.
93 94 95 |
# File 'lib/async_cable/connection.rb', line 93 def close_clean? close_code == Protocol::WebSocket::Error::NO_ERROR end |
#handle_close ⇒ Object
108 109 110 111 112 113 |
# File 'lib/async_cable/connection.rb', line 108 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 |
#handle_open(env) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/async_cable/connection.rb', line 100 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 |
#logger ⇒ Object
119 120 121 |
# File 'lib/async_cable/connection.rb', line 119 def logger self.class.logger end |
#on_close ⇒ Object
Will be executed when WS connection closed. see #close_code, #close_reason for details.
56 57 |
# File 'lib/async_cable/connection.rb', line 56 def on_close end |
#on_data(data) ⇒ Object
Will be executed when data received from WS client.
51 52 |
# File 'lib/async_cable/connection.rb', line 51 def on_data(data) end |
#on_open ⇒ Object
Will be executed when WS connection opened. #stream_for must be called here with stream name
46 47 |
# File 'lib/async_cable/connection.rb', line 46 def on_open end |
#reject_unauthorized(reason = nil) ⇒ Object
80 81 82 |
# File 'lib/async_cable/connection.rb', line 80 def (reason = nil) raise UnauthorizedError, reason end |
#stream_for(stream_name) ⇒ Object
71 72 73 |
# File 'lib/async_cable/connection.rb', line 71 def stream_for(stream_name) @stream_name = stream_name end |
#stream_name ⇒ String
Returns stream name.
76 77 78 |
# File 'lib/async_cable/connection.rb', line 76 def stream_name @stream_name end |
#transmit(data) ⇒ Object
call this method to transmit data to current WS client
61 62 63 64 65 66 67 68 |
# File 'lib/async_cable/connection.rb', line 61 def transmit(data) logger.debug { "#{self.class}#transmit identifier=#{identifier} data=#{data.inspect}" } @mutex.synchronize do write(data) flush end end |