Skip to content

Commit

Permalink
switched to rack/websocket - finally :)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed Jun 5, 2012
1 parent d1fee2f commit 154f322
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 45 deletions.
6 changes: 3 additions & 3 deletions fnordmetric.gemspec
Expand Up @@ -18,15 +18,15 @@ Gem::Specification.new do |s|
s.add_dependency "sinatra", ">= 1.2.6"
s.add_dependency "redis", ">= 2.2.2"
s.add_dependency "eventmachine"
s.add_dependency "em-websocket"
s.add_dependency "em-hiredis"
s.add_dependency "websocket-rack", "0.4.0"
s.add_dependency "em-hiredis", "0.1.1"
s.add_dependency "json"
s.add_dependency "i18n"
s.add_dependency "haml"
s.add_dependency "rack"
s.add_dependency "rack-test"
s.add_dependency "yajl-ruby"
s.add_dependency "thin"
s.add_dependency "thin", "~> 1.3.0"
s.add_dependency "activesupport"

s.add_development_dependency "delorean"
Expand Down
3 changes: 3 additions & 0 deletions lib/fnordmetric.rb
Expand Up @@ -5,7 +5,10 @@
require 'yajl'
require 'sinatra/base'
require 'haml'
require 'json'
require "thin"
require 'rack/server'
require 'rack/websocket'

require "fnordmetric/ext"
require "fnordmetric/version"
Expand Down
11 changes: 11 additions & 0 deletions lib/fnordmetric/ext.rb
Expand Up @@ -61,4 +61,15 @@ def emtpy
self.size == 0
end

end

class Thin::Connection

alias :pre_process_orig :pre_process

def pre_process
@request.env['async.connection'] = self
pre_process_orig
end

end
14 changes: 10 additions & 4 deletions lib/fnordmetric/web/reactor.rb
Expand Up @@ -4,17 +4,23 @@ def initialize
@namespaces = FnordMetric.namespaces
end

def execute(socket, event, messages = [])
def execute(*args)
execute_unsafe(*args)
rescue Exception => e
FnordMetric.error("reactor crashed: " + e.to_s); []
end

private

def execute_unsafe(socket, event, messages = [])
return false unless event["namespace"]
return false unless ns = @namespaces[event["namespace"].to_sym]
messages << discover(ns) if event["type"] == "discover_request"
messages << widget(ns, event) if event["type"] == "widget_request"
messages << gauge(ns, event) if event["type"] == "render_request"
messages.flatten.each{ |m| socket.send(m.to_json) if m }
messages.flatten.compact
end

private

def widget(namespace, event)
"FnordMetric::#{event["klass"]}".constantize.execute(namespace, event) # FIXPAUL
end
Expand Down
19 changes: 12 additions & 7 deletions lib/fnordmetric/web/web.rb
Expand Up @@ -12,7 +12,17 @@ def initialize(opts)

def initialized
server = @opts[:server].downcase
app = FnordMetric::App.new(@opts)

websocket = FnordMetric::WebSocket.new
webapp = FnordMetric::App.new(@opts)

dispatch = Rack::Builder.app do
use Rack::CommonLogger
use Rack::ShowExceptions

map("/stream"){ run websocket }
map("/"){ run webapp }
end

unless ["thin", "hatetepe"].include? server
raise "Need an EventMachine webserver, but #{server} isn't"
Expand All @@ -22,16 +32,11 @@ def initialized
port = @opts[:port]

Rack::Server.start(
:app => app,
:app => dispatch,
:server => server,
:Host => host,
:Port => port
) && FnordMetric.log("listening on http://#{host}:#{port}")

FnordMetric::WebSocket.new(
:host => host,
:port => (port.to_i+1)
) && FnordMetric.log("listening on ws://#{host}:#{port.to_i+1}")
end

end
46 changes: 18 additions & 28 deletions lib/fnordmetric/web/websocket.rb
@@ -1,42 +1,32 @@
require "eventmachine"
require "em-websocket"
require 'rack/websocket'
require "em-hiredis"
require "json"

class FnordMetric::WebSocket
class FnordMetric::WebSocket < Rack::WebSocket::Application

def initialize(opts)
@opts = {
:host => "0.0.0.0",
:port => 8080
}.merge(opts)
def initialize
super

@reactor = FnordMetric::Reactor.new

@uuid = "websocket-#{get_uuid}"

start_websocket
end

private

def start_websocket
EventMachine::WebSocket.start(@opts) do |socket|
socket.onopen do

socket.onmessage do |message|
#puts "received: #{message}"
begin
message = JSON.parse(message)
rescue
puts "websocket: invalid json"
else
message["_eid"] ||= get_uuid
message["_sender"] = @uuid
msg = @reactor.execute(socket, message) # FIXPAUL
end
end
def on_open(env)
# socket openened :)
end

def on_message(env, message)
begin
message = JSON.parse(message)
rescue
puts "websocket: invalid json"
else
message["_eid"] ||= get_uuid
message["_sender"] = @uuid

@reactor.execute(self, message).each do |m|
send_data m.to_json
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions web/js/fnordmetric.js
Expand Up @@ -155,9 +155,7 @@ var FnordMetric = (function(){
};

function connect(){
var socket_url = document.location.hostname + ":" + (parseInt(document.location.port)+1);

socket = new WebSocket("ws://" + socket_url);
socket = new WebSocket("ws://" + document.location.host + '/stream');
socket.onmessage = socketMessage;
socket.onclose = socketClose;
socket.onopen = socketOpen;
Expand Down

0 comments on commit 154f322

Please sign in to comment.