Module: Juggernaut

Defined in:
lib/css_push.rb

Constant Summary collapse

CONFIG =
YAML::load(ERB.new(IO.read("#{ROOT}/config/juggernaut_hosts.yml")).result).freeze
CR =
"\0"

Class Method Summary collapse

Class Method Details

.send_data(hash, response = false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/css_push.rb', line 58

def send_data(hash, response = false)
  hash[:channels]   = Array(hash[:channels])   if hash[:channels]
  hash[:client_ids] = Array(hash[:client_ids]) if hash[:client_ids]

  res = []
  hosts.each do |address|
    begin
      hash[:secret_key] = address[:secret_key] if address[:secret_key]

      @socket = TCPSocket.new(address[:host], address[:port])
      # the \0 is to mirror flash
      @socket.print(hash.to_json + CR)
      @socket.flush
      res << @socket.readline(CR) if response
    ensure
      @socket.close if @socket and !@socket.closed?
    end
  end
  res.collect {|r| ActiveSupport::JSON.decode(r.chomp!(CR)) } if response
end

.send_to_all(data) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/css_push.rb', line 48

def send_to_all(data)
  fc = {
    :command   => :broadcast,
    :body      => data, 
    :type      => :to_channels,
    :channels  => []
  }
  send_data(fc)
end