Sha256: 0172bd3f49f5ef41d44dc1a41f8a466eb5a29a69788d7fadc18a3d59ae482bed

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 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
    event = JSON.parse(params[: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

8 entries across 8 versions & 1 rubygems

Version Path
red-glass-0.1.1 lib/red-glass/red-glass-app/red-glass-app.rb
red-glass-0.1.0 lib/red-glass/red-glass-app/red-glass-app.rb
red-glass-0.0.6 lib/red-glass/red-glass-app.rb
red-glass-0.0.5 lib/red-glass/red-glass-app.rb
red-glass-0.0.4 lib/red-glass/red-glass-app.rb
red-glass-0.0.3 lib/red-glass/red-glass-app.rb
red-glass-0.0.2 lib/red-glass/red-glass-app.rb
red-glass-0.0.1 lib/red-glass/red-glass-app.rb