module ActsAsRealTime

Attributes

channel[RW]
host[RW]
html[RW]
insert_method[RW]
mod_app[RW]
port[RW]
selector[RW]
ws[RW]

Public Class Methods

included(base) click to toggle source
# File lib/acts_as_realtime.rb, line 46
def self.included(base)
  base.send :extend, ClassMethods
end
startup_web_socket_server() click to toggle source
# File lib/acts_as_realtime.rb, line 8
def self.startup_web_socket_server
  Thread.new {
    begin
      EventMachine.run {
        @channel = EM::Channel.new
        EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8092, :debug => true) do |ws|
          @ws = ws
          #RoRRT::Application.save_communication_variables @channel, ws
          ws.onopen {
            sid = @channel.subscribe { |msg| ws.send msg }
            #@channel.push "#{sid} connected!"

            ws.onmessage { |msg|
              #@channel.push "<#{sid}>: #{msg}"
              puts msg
            }

            ws.onclose {
              @channel.unsubscribe(sid)
              #@channel.push "<#{sid}>: Closed"
            }
          }

        end
        puts "Web socket server started"
      }
    rescue => e
      puts e.message
    end
  }

  #Este while es para esperar a que el hilo asigne las variables del socket y del canal
  while (@channel.nil? and @ws.nil?); end
  [@ws, @channel]
end