magicbus 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/magicbus.rb +6 -2
- data/lib/magicbus/publisher.rb +18 -8
- data/lib/magicbus/subscriber.rb +24 -30
- data/lib/magicbus/version.rb +1 -1
- metadata +1 -1
data/lib/magicbus.rb
CHANGED
@@ -5,10 +5,14 @@ module MagicBus
|
|
5
5
|
require_relative 'magicbus/subscriber'
|
6
6
|
require_relative 'magicbus/publisher'
|
7
7
|
|
8
|
+
class << self
|
9
|
+
attr_accessor :connection
|
10
|
+
end
|
11
|
+
|
8
12
|
def self.connect(params = { host: '127.0.0.1' })
|
9
13
|
EM.next_tick do
|
10
|
-
|
14
|
+
self.connection = AMQP.connect(*params)
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
14
|
-
end
|
18
|
+
end
|
data/lib/magicbus/publisher.rb
CHANGED
@@ -1,23 +1,30 @@
|
|
1
1
|
class MagicBus::Publisher
|
2
2
|
|
3
3
|
class << self
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :connection
|
5
5
|
attr_accessor :channels
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
self.channels = {}
|
9
9
|
|
10
|
-
|
10
|
+
def self.scatter(group, action, model, &block)
|
11
|
+
|
12
|
+
users = group.users.map(&:id).map(&:to_s)
|
13
|
+
|
14
|
+
users.each do |user_id|
|
11
15
|
user = User.find(user_id)
|
12
|
-
self.send_to(
|
16
|
+
self.send_to(user.id.to_s, action, model, block.call(user))
|
13
17
|
end
|
14
18
|
|
15
19
|
end
|
16
20
|
|
17
21
|
def self.broadcast(group, action, model, data = nil)
|
18
22
|
|
19
|
-
|
20
|
-
|
23
|
+
users = group.users.map(&:id).map(&:to_s)
|
24
|
+
|
25
|
+
users.each do |user_id|
|
26
|
+
user = User.find(user_id)
|
27
|
+
self.send_to(user.id.to_s, action, model, data)
|
21
28
|
end
|
22
29
|
|
23
30
|
end
|
@@ -25,7 +32,6 @@ class MagicBus::Publisher
|
|
25
32
|
def self.send_to(user_id, action, model, data)
|
26
33
|
|
27
34
|
data = { action: action, model: model, data: data }
|
28
|
-
|
29
35
|
self.publish(user_id, data)
|
30
36
|
|
31
37
|
end
|
@@ -35,7 +41,11 @@ class MagicBus::Publisher
|
|
35
41
|
def self.publish(user_id, data)
|
36
42
|
|
37
43
|
user_id = user_id.to_s
|
38
|
-
|
44
|
+
|
45
|
+
unless channels[user_id]
|
46
|
+
channels[user_id] = AMQP::Channel.new(MagicBus.connection)
|
47
|
+
end
|
48
|
+
|
39
49
|
channel = channels[user_id]
|
40
50
|
exchange = channel.fanout('syme')
|
41
51
|
exchange.publish(data.to_json)
|
data/lib/magicbus/subscriber.rb
CHANGED
@@ -6,22 +6,10 @@ class MagicBus::Subscriber
|
|
6
6
|
attr_accessor :clients
|
7
7
|
end
|
8
8
|
|
9
|
+
require 'securerandom'
|
10
|
+
|
9
11
|
self.channels, self.clients = {}, {}
|
10
12
|
|
11
|
-
def self.connected_users(group)
|
12
|
-
|
13
|
-
connected_users = []
|
14
|
-
|
15
|
-
group.users.each do |user|
|
16
|
-
if self.channels[user.id.to_s]
|
17
|
-
connected_users << user.id.to_s
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
connected_users
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
13
|
def self.subscribe(user_id, out)
|
26
14
|
|
27
15
|
user_id = user_id.to_s
|
@@ -29,22 +17,28 @@ class MagicBus::Subscriber
|
|
29
17
|
if channels[user_id].nil? ||
|
30
18
|
channels[user_id].status == :closed
|
31
19
|
|
32
|
-
channels[user_id] = AMQP::Channel.new(connection)
|
33
|
-
|
34
|
-
exchange = channel.fanout('syme')
|
35
|
-
|
36
|
-
queue = channel.queue(user_id, auto_delete: true)
|
37
|
-
.bind(exchange).subscribe do |payload|
|
38
|
-
self.clients[user_id].each do |client|
|
39
|
-
client << "data: #{payload}\n\n"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
20
|
+
channels[user_id] = AMQP::Channel.new(MagicBus.connection)
|
21
|
+
|
43
22
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
23
|
+
|
24
|
+
channel = channels[user_id]
|
25
|
+
exchange = channel.fanout('syme')
|
26
|
+
|
27
|
+
self.clients[user_id] ||= {}
|
28
|
+
|
29
|
+
client_id = SecureRandom.uuid
|
30
|
+
|
31
|
+
self.clients[user_id][client_id] = out
|
32
|
+
|
33
|
+
queue = channel.queue(client_id)
|
34
|
+
.bind(exchange).subscribe do |payload|
|
35
|
+
|
36
|
+
self.clients[user_id][client_id] <<
|
37
|
+
"data: #{payload}\n\n"
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
client_id
|
48
42
|
|
49
43
|
end
|
50
44
|
|
@@ -53,7 +47,7 @@ class MagicBus::Subscriber
|
|
53
47
|
user_id = user_id.to_s
|
54
48
|
|
55
49
|
if self.clients[user_id][client_id]
|
56
|
-
self.clients[user_id]
|
50
|
+
self.clients[user_id][client_id] = nil
|
57
51
|
end
|
58
52
|
|
59
53
|
if self.clients[user_id].length == 0
|
data/lib/magicbus/version.rb
CHANGED