simplepubsub 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a2ebdb4745d554e759c762bec5a725dc9133bd5
4
- data.tar.gz: 5345a4d9741104cc47a2f17c8ca94e19d8e76057
3
+ metadata.gz: 6c6e71936113e7c6809d833f169db2c7c7650df5
4
+ data.tar.gz: d6f910581d9eec557b010b5e143663eae7104f6e
5
5
  SHA512:
6
- metadata.gz: 39e3993b0ea25c2ef5122216963b8c867771f9dee266933733c6ca37cd9a1bec645ef6a6e9143084f4398c90aefd2f2d88e07c82a91c32fe622bd7393c5b1cb6
7
- data.tar.gz: 74b44f7215feea756bede94891f4f1727f01e189a0f34b04a91aa3bb5eacdd1210d5d45c1448859487e70d53d7042d373e5b150fbd0be6c315b8b17ef83d07a1
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
- ws.send msg, :type => type
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
- EM.run do
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
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplepubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file