Class: AsyncCable::Connection

Inherits:
Async::WebSocket::Connection
  • Object
show all
Defined in:
lib/async_cable/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_codeInteger

WS connection close code 1000 - clean close

Returns:

  • (Integer)


87
88
89
# File 'lib/async_cable/connection.rb', line 87

def close_code
  @close_code || Protocol::WebSocket::Error::NO_ERROR
end

#close_reasonObject

Returns the value of attribute close_reason.



37
38
39
# File 'lib/async_cable/connection.rb', line 37

def close_reason
  @close_reason
end

#requestObject (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.

Parameters:

  • data (Hash)


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_nameObject



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

.loggerObject



20
21
22
# File 'lib/async_cable/connection.rb', line 20

def logger
  AsyncCable.config.logger
end

Instance Method Details

#channel_nameObject



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.

Returns:

  • (Boolean)


93
94
95
# File 'lib/async_cable/connection.rb', line 93

def close_clean?
  close_code == Protocol::WebSocket::Error::NO_ERROR
end

#handle_closeObject



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

Parameters:

  • env (Hash)

Raises:



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

#loggerObject



119
120
121
# File 'lib/async_cable/connection.rb', line 119

def logger
  self.class.logger
end

#on_closeObject

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.

Parameters:

  • data (Hash)


51
52
# File 'lib/async_cable/connection.rb', line 51

def on_data(data)
end

#on_openObject

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

Raises:

  • (UnauthorizedError)


80
81
82
# File 'lib/async_cable/connection.rb', line 80

def reject_unauthorized(reason = nil)
  raise UnauthorizedError, reason
end

#stream_for(stream_name) ⇒ Object

Parameters:

  • stream_name (String)


71
72
73
# File 'lib/async_cable/connection.rb', line 71

def stream_for(stream_name)
  @stream_name = stream_name
end

#stream_nameString

Returns stream name.

Returns:

  • (String)

    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

Parameters:

  • data (Hash)


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