Module: Alondra::Server

Extended by:
Server
Included in:
Server
Defined in:
lib/alondra/server.rb

Instance Method Summary collapse

Instance Method Details

#runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alondra/server.rb', line 7

def run
  Log.info "Server starting on port #{Alondra.config.port}"

  EM::WebSocket.start(:host => '0.0.0.0', :port => Alondra.config.port) do |websocket|

    websocket.onopen do
      session = SessionParser.parse(websocket)

      Log.info "client connected."
      Connection.new(websocket, session)
    end

    websocket.onclose do
      Log.info "Connection closed"
      Connections[websocket].destroy! if Connections[websocket].present?
    end

    websocket.onerror do |ex|
      puts "Error: #{ex.message}"
      Log.error "Error: #{ex.message}"
      Log.error ex.backtrace.join("\n")
      Connections[websocket].destroy! if Connections[websocket]
    end

    websocket.onmessage do |msg|
      Log.info "received: #{msg}"
      CommandDispatcher.dispatch(msg, Connections[websocket])
    end
  end

  EM.error_handler do |error|
    puts "Error raised during event loop: #{error.message}"
    Log.error "Error raised during event loop: #{error.message}"
    Log.error error.backtrace.join("\n")
  end
end