Class: FnordMetric::WebSocket

Inherits:
Rack::WebSocket::Application
  • Object
show all
Defined in:
lib/fnordmetric/web/websocket.rb

Instance Method Summary collapse

Constructor Details

#initializeWebSocket

Returns a new instance of WebSocket.



8
9
10
11
12
13
# File 'lib/fnordmetric/web/websocket.rb', line 8

def initialize
  super

  @reactor = FnordMetric::Reactor.new
  @uuid = "websocket-#{get_uuid}"
end

Instance Method Details

#get_uuidObject



37
38
39
# File 'lib/fnordmetric/web/websocket.rb', line 37

def get_uuid
  rand(8**64).to_s(36)
end

#on_message(env, message) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fnordmetric/web/websocket.rb', line 19

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
rescue Exception => e
  FnordMetric.error("[WebSocket] #{e.to_s}")
  puts e.backtrace.join("\n")
end

#on_open(env) ⇒ Object



15
16
17
# File 'lib/fnordmetric/web/websocket.rb', line 15

def on_open(env)
  @reactor.ready!
end