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 {|close_info = {}| begin actor.async.set_close_info close_info actor.terminate! if actor.alive? rescue Celluloid::DeadActorError => e end } ws.onmessage { |msg| actor.async.process_msg msg } end } end end end end