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.



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_codeInteger

WS connection close code 1000 - clean close

Returns:

  • (Integer)


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

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

#close_reasonObject

Returns the value of attribute close_reason.



40
41
42
# File 'lib/async_cable/connection.rb', line 40

def close_reason
  @close_reason
end

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

Parameters:

  • data (Hash)


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_nameString

Returns:

  • (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

.loggerLogger

Returns:

  • (Logger)


23
24
25
# File 'lib/async_cable/connection.rb', line 23

def logger
  AsyncCable.config.logger
end

Instance Method Details

#channel_nameString

Returns:

  • (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.

Returns:

  • (Boolean)


98
99
100
# File 'lib/async_cable/connection.rb', line 98

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

#handle_closeObject

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.

Parameters:

  • env (Hash)

Raises:



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

#loggerLogger

Returns:

  • (Logger)


129
130
131
# File 'lib/async_cable/connection.rb', line 129

def logger
  self.class.logger
end

#on_closeObject

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.

Parameters:

  • data (Hash)


54
55
# File 'lib/async_cable/connection.rb', line 54

def on_data(data)
end

#on_openObject

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

Parameters:

  • reason (String, NilClass) (defaults to: nil)

    exception message.

Raises:

  • (AsyncCable::UnauthorizedError)


85
86
87
# File 'lib/async_cable/connection.rb', line 85

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

#stream_for(stream_name) ⇒ Object

Parameters:

  • stream_name (String)


74
75
76
# File 'lib/async_cable/connection.rb', line 74

def stream_for(stream_name)
  @stream_name = stream_name
end

#stream_nameString

Returns stream name.

Returns:

  • (String)

    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

Parameters:

  • data (Hash)


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