class WebsocketRails::ConnectionManager

The ConnectionManager class implements the core Rack application that handles incoming WebSocket connections.

Constants

BadRequestResponse
ExceptionResponse
SuccessfulResponse

Attributes

connections[R]

Contains a Hash of currently open connections. @return [Hash]

dispatcher[R]

Contains the {Dispatcher} instance for the active server. @return [Dispatcher]

synchronization[R]

Contains the {Synchronization} instance for the active server. @return [Synchronization]

Public Class Methods

new() click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 30
def initialize
  @connections = {}
  @dispatcher  = Dispatcher.new(self)

  if WebsocketRails.synchronize?
    EM.next_tick do
      Fiber.new {
        Synchronization.synchronize!
        EM.add_shutdown_hook { Synchronization.shutdown! }
      }.resume
    end
  end
end

Public Instance Methods

call(env) click to toggle source

Primary entry point for the Rack application

# File lib/websocket_rails/connection_manager.rb, line 49
def call(env)
  request = ActionDispatch::Request.new(env)

  if request.post?
    response = parse_incoming_event(request.params)
  else
    response = open_connection(request)
  end

  response
rescue InvalidConnectionError
  BadRequestResponse
end
close_connection(connection) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 89
def close_connection(connection)
  WebsocketRails.channel_manager.unsubscribe connection
  destroy_user_connection connection

  connections.delete connection.id

  info "Connection closed: #{connection}"
  connection = nil
end
inspect() click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 44
def inspect
  "websocket_rails"
end

Private Instance Methods

assign_connection_id(connection) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 100
def assign_connection_id(connection)
  begin
    id = SecureRandom.hex(10)
  end while connections.has_key?(id)

  connection.id = id
end
destroy_user_connection(connection) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 113
def destroy_user_connection(connection)
  return unless connection.user_connection?
  WebsocketRails.users.delete(connection)
end
find_connection_by_id(id) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 71
def find_connection_by_id(id)
  connections[id] || raise(InvalidConnectionError)
end
open_connection(request) click to toggle source

Opens a persistent connection using the appropriate {ConnectionAdapter}. Stores active connections in the {connections} Hash.

# File lib/websocket_rails/connection_manager.rb, line 77
def open_connection(request)
  connection = ConnectionAdapters.establish_connection(request, dispatcher)

  assign_connection_id connection
  register_user_connection connection

  connections[connection.id] = connection

  info "Connection opened: #{connection}"
  connection.rack_response
end
parse_incoming_event(params) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 65
def parse_incoming_event(params)
  connection = find_connection_by_id(params["client_id"])
  connection.on_message params["data"]
  SuccessfulResponse
end
register_user_connection(connection) click to toggle source
# File lib/websocket_rails/connection_manager.rb, line 108
def register_user_connection(connection)
  return unless connection.user_connection?
  WebsocketRails.users[connection.user_identifier] = connection
end