sinatra-cometio 0.1.9 → 0.2.0

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 CHANGED
@@ -1,48 +1,52 @@
1
- === 0.1.9 2013-3-7
1
+ === 0.2.0 2013-03-12
2
+
3
+ * push multiple queued data at once
4
+
5
+ === 0.1.9 2013-03-07
2
6
 
3
7
  * use stream on post
4
8
 
5
- === 0.1.8 2013-3-6
9
+ === 0.1.8 2013-03-06
6
10
 
7
11
  * update event_emitter.js
8
12
 
9
- === 0.1.7 2013-3-3
13
+ === 0.1.7 2013-03-03
10
14
 
11
15
  * bugfix CometIO Ruby client
12
16
  * change config key xhr_interval -> timeout
13
17
 
14
- === 0.1.6 2013-3-3
18
+ === 0.1.6 2013-03-03
15
19
 
16
20
  * add IO close function - CometIO.close()
17
21
  * set :cometio, :xhr_interval => 15
18
22
  * use Sinatra.register
19
23
 
20
- === 0.1.5 2013-3-3
24
+ === 0.1.5 2013-03-03
21
25
 
22
26
  * add tests with ruby client
23
27
 
24
- === 0.1.4 2013-3-2
28
+ === 0.1.4 2013-03-02
25
29
 
26
30
  * add CometIO::Client
27
31
 
28
- === 0.1.3 2013-2-19
32
+ === 0.1.3 2013-02-19
29
33
 
30
34
  * bugfix queing on CometIO.push
31
35
 
32
- === 0.1.2 2013-2-17
36
+ === 0.1.2 2013-02-17
33
37
 
34
38
  * emit event when client disconnect
35
39
  * create session_ID from IP Addr
36
40
 
37
- === 0.1.1 2013-2-7
41
+ === 0.1.1 2013-02-07
38
42
 
39
43
  * little fix
40
44
 
41
- === 0.1.0 2013-2-6
45
+ === 0.1.0 2013-02-06
42
46
 
43
47
  * use EventEmitter.js
44
48
 
45
- === 0.0.9 2013-1-4
49
+ === 0.0.9 2013-01-04
46
50
 
47
51
  * use bundler gem template
48
52
 
data/README.md CHANGED
@@ -29,6 +29,11 @@ Server Side
29
29
  ```ruby
30
30
  require 'sinatra'
31
31
  require 'sinatra/cometio'
32
+ set :cometio, :timeout => 120
33
+
34
+ run Sinatra::Application
35
+ ```
36
+ ```ruby
32
37
  CometIO.push :temperature, 35 # to all clients
33
38
  CometIO.push :light, {:value => 150}, {:to => session_id} # to specific client
34
39
  ```
@@ -132,19 +137,6 @@ or
132
137
  io.removeListener("error"); // remove all "error" listener
133
138
  ```
134
139
 
135
- Config
136
- ------
137
-
138
- config.ru
139
- ```ruby
140
- require 'sinatra'
141
- require 'sinatra/cometio'
142
- require File.dirname(__FILE__)+'/main'
143
-
144
- set :cometio, :timeout => 60
145
-
146
- run Sinatra::Application
147
- ```
148
140
 
149
141
  Sample App
150
142
  ----------
data/lib/js/cometio.js CHANGED
@@ -46,9 +46,10 @@ var CometIO = function(){
46
46
  {
47
47
  url : self.url,
48
48
  data : {session : self.session},
49
- success : function(data){
50
- if(data){
51
- self.emit(data.type, data.data);
49
+ success : function(data_arr){
50
+ for(var i = 0; i < data_arr.length; i++){
51
+ var data = data_arr[i];
52
+ if(data) self.emit(data.type, data.data);
52
53
  }
53
54
  self.get();
54
55
  },
@@ -54,8 +54,11 @@ class CometIO
54
54
  sleep 10
55
55
  next
56
56
  else
57
- data = JSON.parse res.body
58
- self.emit data['type'], data['data']
57
+ data_arr = JSON.parse res.body
58
+ data_arr = [data_arr] unless data_arr.kind_of? Array
59
+ data_arr.each do |data|
60
+ self.emit data['type'], data['data']
61
+ end
59
62
  next
60
63
  end
61
64
  rescue Timeout::Error, JSON::ParserError
@@ -5,56 +5,7 @@ require 'event_emitter'
5
5
  require 'sinatra/streaming'
6
6
  require File.expand_path '../sinatra-cometio/version', File.dirname(__FILE__)
7
7
  require File.expand_path '../sinatra-cometio/options', File.dirname(__FILE__)
8
+ require File.expand_path '../sinatra-cometio/cometio', File.dirname(__FILE__)
8
9
  require File.expand_path '../sinatra-cometio/application', File.dirname(__FILE__)
9
- Sinatra.register Sinatra::CometIO
10
-
11
- class CometIO
12
- def self.sessions
13
- @@sessions ||= Hash.new{|h,session_id|
14
- h[session_id] = {
15
- :queue => [{:type => :__session_id, :data => session_id}],
16
- :stream => nil,
17
- :last => nil
18
- }
19
- }
20
- end
21
-
22
- def self.gc
23
- self.sessions.each do |id, s|
24
- next unless s[:last] and s[:last] < Time.now-CometIO.options[:timeout]*2-10
25
- self.sessions.delete id rescue next
26
- self.emit :disconnect, id
27
- end
28
- end
29
10
 
30
- EM::defer do
31
- loop do
32
- self.gc
33
- sleep CometIO.options[:timeout]+5
34
- end
35
- end
36
-
37
- def self.push(type, data, opt={})
38
- session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]]
39
- session_ids.each do |id|
40
- s = self.sessions[id]
41
- if s[:queue].empty? and s[:stream] != nil
42
- begin
43
- s[:stream].write({:type => type, :data => data}.to_json)
44
- s[:stream].flush
45
- s[:stream].close
46
- rescue
47
- s[:stream].close
48
- s[:queue].push :type => type, :data => data
49
- end
50
- else
51
- s[:queue].push :type => type, :data => data
52
- end
53
- end
54
- end
55
-
56
- def self.create_session(ip_addr)
57
- Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
58
- end
59
- end
60
- EventEmitter.apply CometIO
11
+ Sinatra.register Sinatra::CometIO
@@ -40,17 +40,18 @@ module Sinatra::CometIO
40
40
 
41
41
  unless CometIO.sessions[session][:queue].empty?
42
42
  begin
43
- s.write CometIO.sessions[session][:queue].shift.to_json
43
+ s.write CometIO.sessions[session][:queue].to_json
44
44
  s.flush
45
45
  s.close
46
46
  rescue
47
47
  s.close
48
48
  end
49
+ CometIO.sessions[session][:queue] = []
49
50
  end
50
51
 
51
52
  EM::add_timer CometIO.options[:timeout] do
52
53
  begin
53
- s.write({:type => :__heartbeat, :data => {:time => Time.now.to_i}}.to_json)
54
+ s.write([{:type => :__heartbeat, :data => {:time => Time.now.to_i}}].to_json)
54
55
  s.flush
55
56
  s.close
56
57
  rescue
@@ -0,0 +1,51 @@
1
+ class CometIO
2
+
3
+ def self.sessions
4
+ @@sessions ||= Hash.new{|h,session_id|
5
+ h[session_id] = {
6
+ :queue => [{:type => :__session_id, :data => session_id}],
7
+ :stream => nil,
8
+ :last => nil
9
+ }
10
+ }
11
+ end
12
+
13
+ def self.gc
14
+ self.sessions.each do |id, s|
15
+ next unless s[:last] and s[:last] < Time.now-CometIO.options[:timeout]*2-10
16
+ self.sessions.delete id rescue next
17
+ self.emit :disconnect, id
18
+ end
19
+ end
20
+
21
+ EM::defer do
22
+ loop do
23
+ self.gc
24
+ sleep CometIO.options[:timeout]+5
25
+ end
26
+ end
27
+
28
+ def self.push(type, data, opt={})
29
+ session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]]
30
+ session_ids.each do |id|
31
+ s = self.sessions[id]
32
+ if s[:queue].empty? and s[:stream] != nil
33
+ begin
34
+ s[:stream].write([{:type => type, :data => data}].to_json)
35
+ s[:stream].flush
36
+ s[:stream].close
37
+ rescue
38
+ s[:stream].close
39
+ s[:queue].push :type => type, :data => data
40
+ end
41
+ else
42
+ s[:queue].push :type => type, :data => data
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.create_session(ip_addr)
48
+ Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
49
+ end
50
+ end
51
+ EventEmitter.apply CometIO
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module CometIO
3
- VERSION = '0.1.9'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -4,7 +4,7 @@
4
4
  %meta{'http-equiv' => 'Content-Type', :content => 'text/html', :charset => 'UTF-8'}
5
5
  %title comet chat
6
6
  %link{:rel => 'stylesheet', :href => "/main.css", :type => 'text/css'}
7
- %script{:type => 'text/javascript', :src => "//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"}
7
+ %script{:type => 'text/javascript', :src => "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"}
8
8
  - ## load cometio.js
9
9
  %script{:type => 'text/javascript', :src => cometio_js}
10
10
  %script{:type => 'text/javascript', :src => "/js/index.js"}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-cometio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
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-07 00:00:00.000000000 Z
12
+ date: 2013-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -140,6 +140,7 @@ files:
140
140
  - lib/js/event_emitter.js
141
141
  - lib/sinatra-cometio.rb
142
142
  - lib/sinatra-cometio/application.rb
143
+ - lib/sinatra-cometio/cometio.rb
143
144
  - lib/sinatra-cometio/options.rb
144
145
  - lib/sinatra-cometio/version.rb
145
146
  - lib/sinatra/cometio.rb