require 'em-websocket' require 'eventmachine' require 'hara/base' module Hara class Server class << self def start host, port puts "Server starting on #{host}:#{port}" EM.epoll EM.run { EM::WebSocket.run(host: host, port: port) do |ws| actor = nil ws.onopen { |handshake| actor = Hara::Application.new handshake, ws } ws.onclose { begin actor.terminate! if actor.alive? rescue Celluloid::DeadActorError => e end } ws.onmessage { |msg| actor.async.process_msg msg } end } end end end end