em-cometio-client 0.0.2 → 0.0.3
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.
- data/History.txt +5 -0
- data/README.md +2 -4
- data/lib/em-cometio-client/client.rb +6 -3
- data/lib/em-cometio-client/version.rb +1 -1
- metadata +2 -2
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -35,10 +35,8 @@ EM::run do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
## push "chat" event to Server
|
38
|
-
EM::
|
39
|
-
|
40
|
-
client.push :chat, {:message => Time.now.to_s, :name => 'clock'}
|
41
|
-
end
|
38
|
+
EM::add_periodic_timer 10 do
|
39
|
+
client.push :chat, {:message => Time.now.to_s, :name => 'clock'}
|
42
40
|
end
|
43
41
|
end
|
44
42
|
```
|
@@ -3,17 +3,19 @@ module EventMachine
|
|
3
3
|
class Client
|
4
4
|
include EventEmitter
|
5
5
|
attr_reader :url, :session
|
6
|
+
attr_accessor :timeout
|
6
7
|
|
7
8
|
def initialize(url)
|
8
9
|
raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
|
9
10
|
@url = url
|
10
11
|
@session = nil
|
11
12
|
@running = false
|
13
|
+
@timeout = 120
|
12
14
|
end
|
13
15
|
|
14
16
|
def push(type, data)
|
15
17
|
that = self
|
16
|
-
http = EM::HttpRequest.new(@url, :connect_timeout =>
|
18
|
+
http = EM::HttpRequest.new(@url, :connect_timeout => 10).
|
17
19
|
post(:body => {:type => type, :data => data, :session => @session})
|
18
20
|
http.callback do |res|
|
19
21
|
end
|
@@ -40,12 +42,13 @@ module EventMachine
|
|
40
42
|
|
41
43
|
private
|
42
44
|
def get
|
43
|
-
http = EM::HttpRequest.new("#{@url}?session=#{@session}", :connect_timeout =>
|
45
|
+
http = EM::HttpRequest.new("#{@url}?session=#{@session}", :connect_timeout => @timeout).get
|
44
46
|
http.callback do |res|
|
45
47
|
begin
|
46
48
|
data = JSON.parse res.response
|
47
49
|
self.emit data['type'], data['data']
|
48
|
-
rescue
|
50
|
+
rescue JSON::ParserError
|
51
|
+
rescue StandardError
|
49
52
|
self.emit :error, "CometIO get error"
|
50
53
|
sleep 10
|
51
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-cometio-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|