simplepubsub 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/simplepubsub.rb +31 -8
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c6e71936113e7c6809d833f169db2c7c7650df5
|
4
|
+
data.tar.gz: d6f910581d9eec557b010b5e143663eae7104f6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ab5350b0ffd45933731bc231baa24569d7d8f25fbad9d6d18adaf70ee19bc10ecd6b8b53100ef5cfcc2685660086340ff60741b637a37e809eae7e1bcd4a231
|
7
|
+
data.tar.gz: eb9f27007292a36c67937939db5b268b88e8ef428d6b73713c1ac4520495df4b34cf4d28bd522c8167d1f0aa8d76353f26b4f4f086f18e733a0184fe3d41af77
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/simplepubsub.rb
CHANGED
@@ -30,12 +30,20 @@ module SimplePubSub
|
|
30
30
|
puts "Received message: #{msg}"
|
31
31
|
|
32
32
|
a = msg.split(/\s*:\s*/,2)
|
33
|
+
|
34
|
+
def ws.subscriber?()
|
35
|
+
false
|
36
|
+
end
|
33
37
|
|
34
38
|
if a.first == 'subscribe to topic' then
|
35
39
|
|
36
40
|
topic = a.last.rstrip
|
37
41
|
subscribers[topic] ||= []
|
38
|
-
subscribers[topic] << ws
|
42
|
+
subscribers[topic] << ws
|
43
|
+
|
44
|
+
def ws.subscriber?()
|
45
|
+
true
|
46
|
+
end
|
39
47
|
|
40
48
|
elsif a.length > 1
|
41
49
|
|
@@ -49,7 +57,7 @@ module SimplePubSub
|
|
49
57
|
connections.each {|c| c.send topic + ': ' + message }
|
50
58
|
end
|
51
59
|
|
52
|
-
|
60
|
+
ws.send msg, :type => type
|
53
61
|
|
54
62
|
end
|
55
63
|
|
@@ -57,6 +65,7 @@ module SimplePubSub
|
|
57
65
|
|
58
66
|
ws.onclose do
|
59
67
|
puts "Client disconnected"
|
68
|
+
subscribers[topic].delete ws if ws.subscriber?
|
60
69
|
end
|
61
70
|
end
|
62
71
|
|
@@ -88,11 +97,7 @@ module SimplePubSub
|
|
88
97
|
pubsub = PubSub.new
|
89
98
|
yield(pubsub)
|
90
99
|
|
91
|
-
|
92
|
-
|
93
|
-
address = hostname + ':' + port
|
94
|
-
|
95
|
-
ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://' + address)
|
100
|
+
blk = lambda do |ws, em_already_running|
|
96
101
|
|
97
102
|
ws.onopen do
|
98
103
|
puts "Connected"
|
@@ -103,7 +108,7 @@ module SimplePubSub
|
|
103
108
|
a = msg.split(/\s*:\s*/,2)
|
104
109
|
topic, message = a
|
105
110
|
r = pubsub.proc.call topic, message
|
106
|
-
(ws.close; EM.stop) if r == :stop
|
111
|
+
(ws.close; EM.stop) if r == :stop and em_already_running == false
|
107
112
|
end
|
108
113
|
|
109
114
|
ws.onclose do
|
@@ -114,8 +119,26 @@ module SimplePubSub
|
|
114
119
|
ws.send pubsub.topic + ': ' + pubsub.message
|
115
120
|
end
|
116
121
|
|
122
|
+
end
|
123
|
+
|
124
|
+
address = hostname + ':' + port
|
125
|
+
params = {uri: 'ws://' + address}
|
126
|
+
c = WebSocket::EventMachine::Client
|
127
|
+
|
128
|
+
begin
|
117
129
|
|
130
|
+
# attempt to run a websocket assuming the EventMachine is
|
131
|
+
# already running
|
132
|
+
ws = c.connect(params)
|
133
|
+
blk.call ws, em_already_running = true
|
134
|
+
rescue
|
135
|
+
|
136
|
+
EM.run do
|
137
|
+
ws = c.connect(params)
|
138
|
+
blk.call(ws, em_already_running = false)
|
139
|
+
end
|
118
140
|
end
|
141
|
+
|
119
142
|
end
|
120
143
|
end
|
121
144
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|