lib/hara/server.rb in hara-0.1.0 vs lib/hara/server.rb in hara-0.2.0
- old
+ new
@@ -1,42 +1,34 @@
-require 'reel'
+require 'em-websocket'
+require 'eventmachine'
require 'hara/base'
module Hara
- class Server < Reel::Server
- include Celluloid::Logger
+ 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
- def initialize host, port
- info "Server starting on #{host}:#{port}"
- super(host, port, &method(:on_connection))
- end
+ ws.onopen { |handshake|
+ actor = Hara::Application.new handshake, ws
+ }
- def on_connection(connection)
- while request = connection.request
- case request
- when Reel::WebSocket
- info "Received a WebSocket connection"
- handle_websocket request
- when Reel::Request
- Hara.request_handler.call connection, request
- else
- warn "Connection not support"
- request.close
- end
- end
- end
+ ws.onclose {
+ begin
+ actor.terminate! if actor.alive?
+ rescue Celluloid::DeadActorError => e
+ end
+ }
- Hara.request_handler do |connection, request|
- info "#{request.remote_ip} request #{request.url}, not support"
- request.close
- end
-
- def handle_websocket socket
- Hara::Application.new socket
- end
-
- def self.start host, port
- self.supervise_as :hara, host, port
- sleep
+ ws.onmessage { |msg|
+ actor.async.process_msg msg
+ }
+ end
+ }
+ end
end
end
end