class AsyncCable::Server

Attributes

connection_class[R]

Rack application should be used inside Async::Reactor loop.

Public Class Methods

new(connection_class:, &block) click to toggle source

@param connection_class [Class] subclass of AsyncCable::Connection. @param block [Proc<Hash>] yields when not valid WS request. @yieldreturn [Array] `[status,headers,body]`

# File lib/async_cable/server.rb, line 13
def initialize(connection_class:, &block)
  @connection_class = connection_class
  @block = block
end

Public Instance Methods

call(env) click to toggle source

@param env [Hash] @return [Array] `[status,headers,body]`

# File lib/async_cable/server.rb, line 20
def call(env)
  response = Async::WebSocket::Adapters::Rack.open(env, handler: connection_class) do |connection|
    connection.handle_open(env)

    while (data = connection.read)
      connection.on_data(data)
    end
  rescue Protocol::WebSocket::ProtocolError => error
    logger.debug { "#{self.class}#call rescue #{error.class} message=#{error.message} code=#{error.code}" }
    connection.close_code = error.code
    connection.close_reason = error.message
  rescue AsyncCable::Errors::Error => error
    connection.close_code = error.code
    connection.close_reason = error.message
  ensure
    logger.debug { "#{self.class}#call connection closed" }
    connection.handle_close
  end
  # response[1] ca be Protocol::HTTP::Headers::Merged here.
  # We transform it to hash because we don't want to break other middleware logic.
  response[1] = response[1].to_a.to_h if !response.nil? && !response[1].is_a?(Hash)
  response || [400, { 'Content-Type' => 'text/plain' }, ['Not valid ws']]
end
logger() click to toggle source
# File lib/async_cable/server.rb, line 44
def logger
  AsyncCable.config.logger
end