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 ⇒ String
-
.identified_as(channel) ⇒ Object
sets #channel_name for connection class.
- .inherited(subclass) ⇒ Object
- .logger ⇒ Logger
Instance Method Summary collapse
- #channel_name ⇒ String
-
#close_clean? ⇒ Boolean
Was WS connection closed clean or dirty.
-
#handle_close ⇒ Object
Called when connection being closed.
-
#handle_open(env) ⇒ Object
Called when connection being opened.
-
#initialize(*args, &block) ⇒ Connection
constructor
A new instance of Connection.
- #logger ⇒ Logger
-
#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.
42 43 44 45 |
# File 'lib/async_cable/connection.rb', line 42 def initialize(*args, &block) super @mutex = Mutex.new end |
Instance Attribute Details
#close_code ⇒ Integer
WS connection close code 1000 - clean close
92 93 94 |
# File 'lib/async_cable/connection.rb', line 92 def close_code @close_code || Protocol::WebSocket::Error::NO_ERROR end |
#close_reason ⇒ Object
Returns the value of attribute close_reason.
40 41 42 |
# File 'lib/async_cable/connection.rb', line 40 def close_reason @close_reason end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
38 39 40 |
# File 'lib/async_cable/connection.rb', line 38 def request @request end |
Class Method Details
.broadcast(stream_name, data) ⇒ Object
Transmit data to all WS connections in current channel and provided stream.
29 30 31 32 33 34 35 |
# 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 |
.channel_name ⇒ String
18 19 20 |
# File 'lib/async_cable/connection.rb', line 18 def channel_name @channel_name end |
.identified_as(channel) ⇒ Object
sets #channel_name for connection class
13 14 15 |
# File 'lib/async_cable/connection.rb', line 13 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 ⇒ Logger
23 24 25 |
# File 'lib/async_cable/connection.rb', line 23 def logger AsyncCable.config.logger end |
Instance Method Details
#channel_name ⇒ String
124 125 126 |
# File 'lib/async_cable/connection.rb', line 124 def channel_name self.class.channel_name end |
#close_clean? ⇒ Boolean
Was WS connection closed clean or dirty.
98 99 100 |
# File 'lib/async_cable/connection.rb', line 98 def close_clean? close_code == Protocol::WebSocket::Error::NO_ERROR end |
#handle_close ⇒ Object
Called when connection being closed.
116 117 118 119 120 121 |
# 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 |
#handle_open(env) ⇒ Object
Called when connection being opened.
106 107 108 109 110 111 112 |
# 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 |
#logger ⇒ Logger
129 130 131 |
# File 'lib/async_cable/connection.rb', line 129 def logger self.class.logger end |
#on_close ⇒ Object
Will be executed when WS connection closed. see #close_code, #close_reason for details.
59 60 |
# File 'lib/async_cable/connection.rb', line 59 def on_close end |
#on_data(data) ⇒ Object
Will be executed when data received from WS client.
54 55 |
# File 'lib/async_cable/connection.rb', line 54 def on_data(data) end |
#on_open ⇒ Object
Will be executed when WS connection opened. #stream_for must be called here with stream name
49 50 |
# File 'lib/async_cable/connection.rb', line 49 def on_open end |
#reject_unauthorized(reason = nil) ⇒ Object
85 86 87 |
# File 'lib/async_cable/connection.rb', line 85 def (reason = nil) raise UnauthorizedError, reason end |
#stream_for(stream_name) ⇒ Object
74 75 76 |
# File 'lib/async_cable/connection.rb', line 74 def stream_for(stream_name) @stream_name = stream_name end |
#stream_name ⇒ String
Returns stream name.
79 80 81 |
# File 'lib/async_cable/connection.rb', line 79 def stream_name @stream_name end |
#transmit(data) ⇒ Object
call this method to transmit data to current WS client
64 65 66 67 68 69 70 71 |
# 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 |