Module: Juggernaut::Rails::ConvenienceMethods::ClassMethods

Defined in:
lib/juggernaut/rails/convenience_methods.rb

Instance Method Summary collapse

Instance Method Details

#remove_all_channels(channels) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/juggernaut/rails/convenience_methods.rb', line 72

def remove_all_channels(channels)
  fc = {
    :command    => :query,
    :type       => :remove_all_channels,
    :channels   => channels
  }
  send_data(fc)
end

#remove_channels_from_clients(client_ids, channels) ⇒ Object Also known as: remove_channel_from_client, remove_channels_from_client



60
61
62
63
64
65
66
67
68
# File 'lib/juggernaut/rails/convenience_methods.rb', line 60

def remove_channels_from_clients(client_ids, channels)
  fc = {
    :command    => :query,
    :type       => :remove_channels_from_client,
    :client_ids => client_ids,
    :channels   => channels
  }
  send_data(fc)
end

#send_data(hash, response = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/juggernaut/rails/convenience_methods.rb', line 108

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 + Juggernaut::Server::CR)
      @socket.flush
      res << @socket.readline(Juggernaut::Server::CR) if response
    ensure
      @socket.close if @socket and !@socket.closed?
    end
  end
  res.collect {|r| ActiveSupport::JSON.decode(r.chomp!(Juggernaut::Server::CR)) } if response
end

#send_to_all(data) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/juggernaut/rails/convenience_methods.rb', line 15

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

#send_to_channels(data, channels) ⇒ Object Also known as: send_to_channel



25
26
27
28
29
30
31
32
33
# File 'lib/juggernaut/rails/convenience_methods.rb', line 25

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

#send_to_clients(data, client_ids) ⇒ Object Also known as: send_to_client



36
37
38
39
40
41
42
43
44
# File 'lib/juggernaut/rails/convenience_methods.rb', line 36

def send_to_clients(data, client_ids)
  fc = {
    :command    => :broadcast,
    :body       => data, 
    :type       => :to_clients,
    :client_ids => client_ids
  }
  send_data(fc)
end

#send_to_clients_on_channels(data, client_ids, channels) ⇒ Object Also known as: send_to_clients_on_channel, send_to_client_on_channels



47
48
49
50
51
52
53
54
55
56
# File 'lib/juggernaut/rails/convenience_methods.rb', line 47

def send_to_clients_on_channels(data, client_ids, channels)
  fc = {
    :command    => :broadcast,
    :body       => data, 
    :type       => :to_clients,
    :client_ids => client_ids,
    :channels   => channels
  }
  send_data(fc)
end

#show_client(client_id) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/juggernaut/rails/convenience_methods.rb', line 89

def show_client(client_id)
  fc = {
    :command    => :query,
    :type       => :show_client,
    :client_id  => client_id
  }
  send_data(fc, true).flatten[0]
end

#show_clientsObject



81
82
83
84
85
86
87
# File 'lib/juggernaut/rails/convenience_methods.rb', line 81

def show_clients
  fc = {
    :command  => :query,
    :type     => :show_clients
  }
  send_data(fc, true).flatten
end

#show_clients_for_channels(channels) ⇒ Object Also known as: show_clients_for_channel



98
99
100
101
102
103
104
105
# File 'lib/juggernaut/rails/convenience_methods.rb', line 98

def show_clients_for_channels(channels)
  fc = {
    :command    => :query,
    :type       => :show_clients_for_channels,
    :channels   => channels
  }
  send_data(fc, true).flatten
end