dima-exe-juggernaut 0.5.9.25 → 0.5.9.26
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/juggernaut/client.rb +17 -11
- data/lib/juggernaut/message.rb +10 -5
- data/lib/juggernaut/server.rb +3 -2
- metadata +1 -1
data/lib/juggernaut/client.rb
CHANGED
@@ -39,20 +39,20 @@ module Juggernaut
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def find_by_signature(signature)
|
42
|
-
|
43
|
-
find do |client|
|
42
|
+
# signature should be unique
|
43
|
+
find do |client|
|
44
44
|
client.connections.select { |connection| connection.signature == signature }.any?
|
45
45
|
end.first
|
46
46
|
end
|
47
47
|
|
48
48
|
def find_by_channels(channels)
|
49
|
-
find do |client|
|
49
|
+
find do |client|
|
50
50
|
client.has_channels?(channels)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
def find_by_id_and_channels(id, channels)
|
55
|
-
find do |client|
|
55
|
+
find do |client|
|
56
56
|
client.has_channels?(channels) && client.id == id
|
57
57
|
end.first
|
58
58
|
end
|
@@ -101,7 +101,7 @@ module Juggernaut
|
|
101
101
|
|
102
102
|
def to_json
|
103
103
|
{
|
104
|
-
:client_id => @id,
|
104
|
+
:client_id => @id,
|
105
105
|
:num_connections => @connections.size,
|
106
106
|
:session_id => @session_id
|
107
107
|
}.to_json
|
@@ -135,7 +135,7 @@ module Juggernaut
|
|
135
135
|
return true unless options[:logout_url]
|
136
136
|
post_request(options[:logout_url], [ ], :timeout => options[:post_request_timeout] || 5)
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
def remove_connection(connection)
|
140
140
|
@connections.delete(connection)
|
141
141
|
self.reset_logout_timeout!
|
@@ -159,9 +159,14 @@ module Juggernaut
|
|
159
159
|
|
160
160
|
logger.debug("Sending #{@length} queued message(s) to client #{friendly_id}")
|
161
161
|
|
162
|
+
req = connection.instance_variable_get("@request")
|
162
163
|
@length.times do |id|
|
163
164
|
message = @messages[id]
|
164
|
-
|
165
|
+
n = (req &&
|
166
|
+
req[:timestamp].to_f > 0.0 &&
|
167
|
+
(req[:timestamp].to_f + 0.0001) >= message[:timestamp].to_f)
|
168
|
+
next if n
|
169
|
+
send_message_to_connection(connection, message[:message], message[:channels], message[:timestamp]) if connection.alive?
|
165
170
|
end
|
166
171
|
end
|
167
172
|
|
@@ -231,7 +236,7 @@ module Juggernaut
|
|
231
236
|
logger.error("#{url.to_s} timeout")
|
232
237
|
return false
|
233
238
|
end
|
234
|
-
end
|
239
|
+
end
|
235
240
|
|
236
241
|
def send_message_to_connections(msg, channels)
|
237
242
|
@connections.each do |connection|
|
@@ -239,8 +244,8 @@ module Juggernaut
|
|
239
244
|
end
|
240
245
|
end
|
241
246
|
|
242
|
-
def send_message_to_connection(connection, msg, channels)
|
243
|
-
connection.broadcast(msg) if !channels or channels.empty? or connection.has_channels?(channels)
|
247
|
+
def send_message_to_connection(connection, msg, channels, timestamp = nil)
|
248
|
+
connection.broadcast(msg, timestamp) if !channels or channels.empty? or connection.has_channels?(channels)
|
244
249
|
end
|
245
250
|
|
246
251
|
# Queued messages are stored until a timeout is reached which is the
|
@@ -249,7 +254,8 @@ module Juggernaut
|
|
249
254
|
# clicking off one page and loading the next one.
|
250
255
|
def store_message(msg, channels)
|
251
256
|
self.expire_queued_messages!
|
252
|
-
@messages << { :channels => channels, :message => msg, :timeout => Time.now + options[:timeout] }
|
257
|
+
@messages << { :channels => channels, :message => msg, :timeout => Time.now + options[:timeout], :timestamp => Time.now.to_f }
|
258
|
+
@messages
|
253
259
|
end
|
254
260
|
|
255
261
|
def expire_queued_messages!
|
data/lib/juggernaut/message.rb
CHANGED
@@ -4,16 +4,21 @@ module Juggernaut
|
|
4
4
|
attr_accessor :signature
|
5
5
|
attr_accessor :body
|
6
6
|
attr_reader :created_at
|
7
|
-
|
8
|
-
def initialize(id, body, signature)
|
7
|
+
|
8
|
+
def initialize(id, body, signature, utime = nil)
|
9
9
|
@id = id
|
10
10
|
@body = body
|
11
11
|
@signature = signature
|
12
12
|
@created_at = Time.now
|
13
|
+
@timestamp = utime
|
13
14
|
end
|
14
|
-
|
15
|
+
|
16
|
+
def timestamp
|
17
|
+
@timestamp || @created_at.to_f.to_s
|
18
|
+
end
|
19
|
+
|
15
20
|
def to_s
|
16
|
-
{ :id => @id.to_s, :body => @body, :signature => @signature }.to_json
|
21
|
+
{ :id => @id.to_s, :body => @body, :signature => @signature, :timestamp => timestamp.to_s }.to_json
|
17
22
|
end
|
18
23
|
end
|
19
|
-
end
|
24
|
+
end
|
data/lib/juggernaut/server.rb
CHANGED
@@ -54,6 +54,7 @@ module Juggernaut
|
|
54
54
|
attr_reader :channels
|
55
55
|
attr_reader :client
|
56
56
|
|
57
|
+
|
57
58
|
# EM methods
|
58
59
|
|
59
60
|
def post_init
|
@@ -186,8 +187,8 @@ module Juggernaut
|
|
186
187
|
|
187
188
|
# Connection methods
|
188
189
|
|
189
|
-
def broadcast(bdy)
|
190
|
-
msg = Juggernaut::Message.new(@current_msg_id += 1, bdy, self.signature)
|
190
|
+
def broadcast(bdy, timestamp = nil)
|
191
|
+
msg = Juggernaut::Message.new(@current_msg_id += 1, bdy, self.signature, timestamp)
|
191
192
|
publish(msg)
|
192
193
|
end
|
193
194
|
|