Skip to content
This repository
Browse code

switched to rack/websocket - finally :)

  • Loading branch information...
commit 154f32219a7eb6c50b41bba6923a901313ca7c45 1 parent d1fee2f
Paul Asmuth authored
6 fnordmetric.gemspec
@@ -18,15 +18,15 @@ Gem::Specification.new do |s|
18 18 s.add_dependency "sinatra", ">= 1.2.6"
19 19 s.add_dependency "redis", ">= 2.2.2"
20 20 s.add_dependency "eventmachine"
21   - s.add_dependency "em-websocket"
22   - s.add_dependency "em-hiredis"
  21 + s.add_dependency "websocket-rack", "0.4.0"
  22 + s.add_dependency "em-hiredis", "0.1.1"
23 23 s.add_dependency "json"
24 24 s.add_dependency "i18n"
25 25 s.add_dependency "haml"
26 26 s.add_dependency "rack"
27 27 s.add_dependency "rack-test"
28 28 s.add_dependency "yajl-ruby"
29   - s.add_dependency "thin"
  29 + s.add_dependency "thin", "~> 1.3.0"
30 30 s.add_dependency "activesupport"
31 31
32 32 s.add_development_dependency "delorean"
3  lib/fnordmetric.rb
@@ -5,7 +5,10 @@
5 5 require 'yajl'
6 6 require 'sinatra/base'
7 7 require 'haml'
  8 +require 'json'
  9 +require "thin"
8 10 require 'rack/server'
  11 +require 'rack/websocket'
9 12
10 13 require "fnordmetric/ext"
11 14 require "fnordmetric/version"
11 lib/fnordmetric/ext.rb
@@ -61,4 +61,15 @@ def emtpy
61 61 self.size == 0
62 62 end
63 63
  64 +end
  65 +
  66 +class Thin::Connection
  67 +
  68 + alias :pre_process_orig :pre_process
  69 +
  70 + def pre_process
  71 + @request.env['async.connection'] = self
  72 + pre_process_orig
  73 + end
  74 +
64 75 end
14 lib/fnordmetric/web/reactor.rb
@@ -4,17 +4,23 @@ def initialize
4 4 @namespaces = FnordMetric.namespaces
5 5 end
6 6
7   - def execute(socket, event, messages = [])
  7 + def execute(*args)
  8 + execute_unsafe(*args)
  9 + rescue Exception => e
  10 + FnordMetric.error("reactor crashed: " + e.to_s); []
  11 + end
  12 +
  13 +private
  14 +
  15 + def execute_unsafe(socket, event, messages = [])
8 16 return false unless event["namespace"]
9 17 return false unless ns = @namespaces[event["namespace"].to_sym]
10 18 messages << discover(ns) if event["type"] == "discover_request"
11 19 messages << widget(ns, event) if event["type"] == "widget_request"
12 20 messages << gauge(ns, event) if event["type"] == "render_request"
13   - messages.flatten.each{ |m| socket.send(m.to_json) if m }
  21 + messages.flatten.compact
14 22 end
15 23
16   -private
17   -
18 24 def widget(namespace, event)
19 25 "FnordMetric::#{event["klass"]}".constantize.execute(namespace, event) # FIXPAUL
20 26 end
19 lib/fnordmetric/web/web.rb
@@ -12,7 +12,17 @@ def initialize(opts)
12 12
13 13 def initialized
14 14 server = @opts[:server].downcase
15   - app = FnordMetric::App.new(@opts)
  15 +
  16 + websocket = FnordMetric::WebSocket.new
  17 + webapp = FnordMetric::App.new(@opts)
  18 +
  19 + dispatch = Rack::Builder.app do
  20 + use Rack::CommonLogger
  21 + use Rack::ShowExceptions
  22 +
  23 + map("/stream"){ run websocket }
  24 + map("/"){ run webapp }
  25 + end
16 26
17 27 unless ["thin", "hatetepe"].include? server
18 28 raise "Need an EventMachine webserver, but #{server} isn't"
@@ -22,16 +32,11 @@ def initialized
22 32 port = @opts[:port]
23 33
24 34 Rack::Server.start(
25   - :app => app,
  35 + :app => dispatch,
26 36 :server => server,
27 37 :Host => host,
28 38 :Port => port
29 39 ) && FnordMetric.log("listening on http://#{host}:#{port}")
30   -
31   - FnordMetric::WebSocket.new(
32   - :host => host,
33   - :port => (port.to_i+1)
34   - ) && FnordMetric.log("listening on ws://#{host}:#{port.to_i+1}")
35 40 end
36 41
37 42 end
46 lib/fnordmetric/web/websocket.rb
... ... @@ -1,42 +1,32 @@
1 1 require "eventmachine"
2   -require "em-websocket"
  2 +require 'rack/websocket'
3 3 require "em-hiredis"
4 4 require "json"
5 5
6   -class FnordMetric::WebSocket
  6 +class FnordMetric::WebSocket < Rack::WebSocket::Application
7 7
8   - def initialize(opts)
9   - @opts = {
10   - :host => "0.0.0.0",
11   - :port => 8080
12   - }.merge(opts)
  8 + def initialize
  9 + super
13 10
14 11 @reactor = FnordMetric::Reactor.new
15   -
16 12 @uuid = "websocket-#{get_uuid}"
17   -
18   - start_websocket
19 13 end
20 14
21   -private
22   -
23   - def start_websocket
24   - EventMachine::WebSocket.start(@opts) do |socket|
25   - socket.onopen do
26   -
27   - socket.onmessage do |message|
28   - #puts "received: #{message}"
29   - begin
30   - message = JSON.parse(message)
31   - rescue
32   - puts "websocket: invalid json"
33   - else
34   - message["_eid"] ||= get_uuid
35   - message["_sender"] = @uuid
36   - msg = @reactor.execute(socket, message) # FIXPAUL
37   - end
38   - end
  15 + def on_open(env)
  16 + # socket openened :)
  17 + end
39 18
  19 + def on_message(env, message)
  20 + begin
  21 + message = JSON.parse(message)
  22 + rescue
  23 + puts "websocket: invalid json"
  24 + else
  25 + message["_eid"] ||= get_uuid
  26 + message["_sender"] = @uuid
  27 +
  28 + @reactor.execute(self, message).each do |m|
  29 + send_data m.to_json
40 30 end
41 31 end
42 32 end
4 web/js/fnordmetric.js
@@ -155,9 +155,7 @@ var FnordMetric = (function(){
155 155 };
156 156
157 157 function connect(){
158   - var socket_url = document.location.hostname + ":" + (parseInt(document.location.port)+1);
159   -
160   - socket = new WebSocket("ws://" + socket_url);
  158 + socket = new WebSocket("ws://" + document.location.host + '/stream');
161 159 socket.onmessage = socketMessage;
162 160 socket.onclose = socketClose;
163 161 socket.onopen = socketOpen;

0 comments on commit 154f322

Please sign in to comment.
Something went wrong with that request. Please try again.