Sha256: b88a9d6021c53ddde2620732571e40ecfb4604f599ab034d1f8a2022856d5766

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'sinatra/base'
require 'thin'
require 'em-websocket'
require "uuid"
require 'json'

class RedGlassApp < Sinatra::Base

  red_glass_port = ENV['red_glass_port'].nil? ? 4567 : ENV['red_glass_port']
  socket = nil
  is_socket_connected = false
  events = Array.new

  get '/' do
    erb :index
  end

  post '/?' do
    request.body.rewind
    event = JSON.parse(request.body.read)['event_json']
    begin
      uuid = UUID.new
      event['id'] = uuid.generate
    rescue
      event['id'] = event['time']
    end
    events << event
    if is_socket_connected
      socket.send events.to_json
      events.clear
    end
  end

  get '/events' do
    events.to_json
  end

  get '/status' do
    'ready'
  end

  get '/kill' do
    Process.kill('INT', 0)
  end

  if app_file == $0
    EventMachine.run do
      RedGlassApp.run!({:port => red_glass_port})
      EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 4568) do |ws|
        socket = ws
        ws.onopen { is_socket_connected = true }
        ws.onmessage { |msg|
          if msg == 'all'
            ws.send events.to_json
            events.clear
          end
        }
        ws.onclose   { }
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
red-glass-0.1.3 lib/red-glass/red-glass-app/red-glass-app.rb
red-glass-0.1.2 lib/red-glass/red-glass-app/red-glass-app.rb