Sha256: 3c271ab10d6cace89c54112bd2c024a5a4ea22537080d6266995d9092858b20f

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module MagicMirror
  FAYE_PORT = 4555
  class Mirror
    def initialize

    end

    def init_servers!
      threads = []
      # start sinatra
      threads << start_sinatra
      threads << start_faye

      self
    end

    def start_sinatra
      Thread.new {
        SinatraSilver::App.run!
      }
    end

    def start_faye

      Thread.new {
        bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
        bayeux.listen(FAYE_PORT)
        run bayeux
      }

      #wait_until_faye_is_up
    end

    def get_faye_app

    end

    # sends messages through faye server to web interface
    def speak_into(msg)
      require 'net/http'
      channel = "/0001"

      message = {:channel => channel, :data => msg}
      uri = URI.parse("http://localhost:#{FAYE_PORT}/faye")
      begin
        Net::HTTP.post_form(uri, :message => message.to_json)
      rescue
        puts "failed to send message to faye server and thus webclient"
      end

    end

    def wait_until_faye_is_up
      while(true) do
        begin
          uri = URI.parse("http://localhost:#{FAYE_PORT}/faye")
          # Shortcut
          response = Net::HTTP.get_response(uri)
        rescue

        end
        sleep 1
        break if response and response.code == 400 and response.body == "Bad request"
      end
    end


  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
magic_mirror-0.1.0 lib/magic_mirror/mirror.rb
magic_mirror-0.0.2 lib/magic_mirror/mirror.rb