sinatra-cometio 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.3.0 2013-03-16
2
+
3
+ * support modular style
4
+
1
5
  === 0.2.0 2013-03-12
2
6
 
3
7
  * push multiple queued data at once
data/README.md CHANGED
@@ -34,8 +34,8 @@ set :cometio, :timeout => 120
34
34
  run Sinatra::Application
35
35
  ```
36
36
  ```ruby
37
- CometIO.push :temperature, 35 # to all clients
38
- CometIO.push :light, {:value => 150}, {:to => session_id} # to specific client
37
+ cometio.push :temperature, 35 # to all clients
38
+ cometio.push :light, {:value => 150}, {:to => session_id} # to specific client
39
39
  ```
40
40
 
41
41
  Client Side
@@ -66,7 +66,7 @@ io.push("chat", {name: "shokai", message: "hello"}); // client -> server
66
66
  Server Side
67
67
 
68
68
  ```ruby
69
- CometIO.on :chat do |data, session|
69
+ cometio.on :chat do |data, session|
70
70
  puts "#{data['name']} : #{data['message']} <#{session}>"
71
71
  end
72
72
  ## => "shokai : hello <12abcde345f6g7h8ijk>"
@@ -85,11 +85,11 @@ io.on("connect", function(session){
85
85
  Server Side
86
86
 
87
87
  ```ruby
88
- CometIO.on :connect do |session|
88
+ cometio.on :connect do |session|
89
89
  puts "new client <#{session}>"
90
90
  end
91
91
 
92
- CometIO.on :disconnect do |session|
92
+ cometio.on :disconnect do |session|
93
93
  puts "client disconnected <#{session}>"
94
94
  end
95
95
  ```
@@ -109,16 +109,16 @@ io.on("error", function(err){
109
109
  Server Side
110
110
 
111
111
  ```ruby
112
- event_id = CometIO.on :chat do |data, from|
112
+ event_id = cometio.on :chat do |data, from|
113
113
  puts "#{data} - from#{from}"
114
114
  end
115
- CometIO.removeListener event_id
115
+ cometio.removeListener event_id
116
116
  ```
117
117
 
118
118
  or
119
119
 
120
120
  ```ruby
121
- CometIO.removeListener :chat # remove all "chat" listener
121
+ cometio.removeListener :chat # remove all "chat" listener
122
122
  ```
123
123
 
124
124
 
@@ -1,82 +1,79 @@
1
- module Sinatra::CometIO
1
+ module Sinatra
2
+ module CometIO
2
3
 
3
- def cometio=(options)
4
- CometIO.options = options
5
- end
6
-
7
- def cometio
8
- CometIO.options
9
- end
10
-
11
- helpers do
12
- def cometio_js
13
- "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/cometio/cometio.js"
4
+ def cometio=(options)
5
+ CometIO.options = options
14
6
  end
15
7
 
16
- def cometio_url
17
- "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/cometio/io"
8
+ def cometio
9
+ Sinatra::CometIO
18
10
  end
19
- end
20
11
 
21
- get '/cometio/cometio.js' do
22
- content_type 'application/javascript'
23
- @js ||= (
24
- js = ''
25
- Dir.glob(File.expand_path '../js/*.js', File.dirname(__FILE__)).each do |i|
26
- File.open(i) do |f|
27
- js += f.read
28
- end
29
- end
30
- ERB.new(js).result(binding)
31
- )
32
- end
12
+ def self.registered(app)
13
+ app.helpers Sinatra::Streaming
14
+ app.helpers Sinatra::CometIO::Helpers
15
+
16
+ app.get '/cometio/cometio.js' do
17
+ content_type 'application/javascript'
18
+ @js ||= (
19
+ js = ''
20
+ Dir.glob(File.expand_path '../js/*.js', File.dirname(__FILE__)).sort.each do |i|
21
+ File.open(i) do |f|
22
+ js += f.read
23
+ end
24
+ end
25
+ ERB.new(js).result(binding)
26
+ )
27
+ end
33
28
 
34
- get '/cometio/io' do
35
- stream :keep_open do |s|
36
- session = params[:session].to_s.empty? ? CometIO.create_session(request.ip) : params[:session]
37
- CometIO.sessions[session][:stream] = s
38
- CometIO.sessions[session][:last] = Time.now
39
- CometIO.emit :connect, session if params[:session].to_s.empty?
29
+ app.get '/cometio/io' do
30
+ stream :keep_open do |s|
31
+ session = params[:session].to_s.empty? ? CometIO.create_session(request.ip) : params[:session]
32
+ CometIO.sessions[session][:stream] = s
33
+ CometIO.sessions[session][:last] = Time.now
34
+ CometIO.emit :connect, session if params[:session].to_s.empty?
40
35
 
41
- unless CometIO.sessions[session][:queue].empty?
42
- begin
43
- s.write CometIO.sessions[session][:queue].to_json
44
- s.flush
45
- s.close
46
- rescue
47
- s.close
36
+ unless CometIO.sessions[session][:queue].empty?
37
+ begin
38
+ s.write CometIO.sessions[session][:queue].to_json
39
+ s.flush
40
+ s.close
41
+ rescue
42
+ s.close
43
+ end
44
+ CometIO.sessions[session][:queue] = []
45
+ end
46
+
47
+ EM::add_timer CometIO.options[:timeout] do
48
+ begin
49
+ s.write([{:type => :__heartbeat, :data => {:time => Time.now.to_i}}].to_json)
50
+ s.flush
51
+ s.close
52
+ rescue
53
+ s.close
54
+ end
55
+ end
48
56
  end
49
- CometIO.sessions[session][:queue] = []
50
57
  end
51
58
 
52
- EM::add_timer CometIO.options[:timeout] do
53
- begin
54
- s.write([{:type => :__heartbeat, :data => {:time => Time.now.to_i}}].to_json)
55
- s.flush
56
- s.close
57
- rescue
58
- s.close
59
+ app.post '/cometio/io' do
60
+ type = params[:type]
61
+ data = params[:data]
62
+ from = params[:session]
63
+ EM::defer do
64
+ CometIO.emit type, data, from unless type.to_s.empty?
65
+ end
66
+ stream :keep_open do |s|
67
+ begin
68
+ s.write({:session => from, :type => type, :data => data}.to_json)
69
+ s.flush
70
+ s.close
71
+ rescue
72
+ s.close
73
+ end
59
74
  end
60
75
  end
61
- end
62
- end
63
76
 
64
- post '/cometio/io' do
65
- type = params[:type]
66
- data = params[:data]
67
- from = params[:session]
68
- EM::defer do
69
- CometIO.emit type, data, from unless type.to_s.empty?
70
- end
71
- stream :keep_open do |s|
72
- begin
73
- s.write({:session => from, :type => type, :data => data}.to_json)
74
- s.flush
75
- s.close
76
- rescue
77
- s.close
78
- end
79
77
  end
80
78
  end
81
-
82
79
  end
@@ -1,51 +1,54 @@
1
- class CometIO
1
+ module Sinatra
2
+ module CometIO
2
3
 
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
4
+ def self.sessions
5
+ @@sessions ||= Hash.new{|h,session_id|
6
+ h[session_id] = {
7
+ :queue => [{:type => :__session_id, :data => session_id}],
8
+ :stream => nil,
9
+ :last => nil
10
+ }
9
11
  }
10
- }
11
- end
12
+ end
12
13
 
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
14
+ def self.gc
15
+ self.sessions.each do |id, s|
16
+ next unless s[:last] and s[:last] < Time.now-CometIO.options[:timeout]*2-10
17
+ self.sessions.delete id rescue next
18
+ self.emit :disconnect, id
19
+ end
18
20
  end
19
- end
20
21
 
21
- EM::defer do
22
- loop do
23
- self.gc
24
- sleep CometIO.options[:timeout]+5
22
+ EM::defer do
23
+ loop do
24
+ self.gc
25
+ sleep CometIO.options[:timeout]+5
26
+ end
25
27
  end
26
- end
27
28
 
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
29
+ def self.push(type, data, opt={})
30
+ session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]]
31
+ session_ids.each do |id|
32
+ s = self.sessions[id]
33
+ if s[:queue].empty? and s[:stream] != nil
34
+ begin
35
+ s[:stream].write([{:type => type, :data => data}].to_json)
36
+ s[:stream].flush
37
+ s[:stream].close
38
+ rescue
39
+ s[:stream].close
40
+ s[:queue].push :type => type, :data => data
41
+ end
42
+ else
39
43
  s[:queue].push :type => type, :data => data
40
44
  end
41
- else
42
- s[:queue].push :type => type, :data => data
43
45
  end
44
46
  end
45
- end
46
47
 
47
- def self.create_session(ip_addr)
48
- Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
48
+ def self.create_session(ip_addr)
49
+ Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
50
+ end
51
+
49
52
  end
50
53
  end
51
- EventEmitter.apply CometIO
54
+ EventEmitter.apply Sinatra::CometIO
@@ -0,0 +1,15 @@
1
+ module Sinatra
2
+ module CometIO
3
+ module Helpers
4
+
5
+ def cometio_js
6
+ "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/cometio/cometio.js"
7
+ end
8
+
9
+ def cometio_url
10
+ "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/cometio/io"
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -1,35 +1,37 @@
1
- class CometIO
1
+ module Sinatra
2
+ module CometIO
2
3
 
3
- def self.default_options
4
- {
5
- :timeout => [120, lambda{|v| v.kind_of? Fixnum and v >= 20 }]
6
- }
7
- end
4
+ def self.default_options
5
+ {
6
+ :timeout => [120, lambda{|v| v.kind_of? Fixnum and v >= 20 }]
7
+ }
8
+ end
8
9
 
9
- def self.options
10
- @@options ||= (
11
- opts = {}
12
- default_options.each do |k,v|
13
- opts[k] = v[0]
14
- end
15
- opts
16
- )
17
- end
10
+ def self.options
11
+ @@options ||= (
12
+ opts = {}
13
+ default_options.each do |k,v|
14
+ opts[k] = v[0]
15
+ end
16
+ opts
17
+ )
18
+ end
18
19
 
19
- def self.options=(opts)
20
- @@options = {}
21
- opts.each do |k,v|
22
- k = k.to_sym
23
- if default_options.include? k
24
- @@options[k] = default_options[k][1].call(v) ? v : default_options[k][0]
25
- else
26
- @@options[k] = v
20
+ def self.options=(opts)
21
+ @@options = {}
22
+ opts.each do |k,v|
23
+ k = k.to_sym
24
+ if default_options.include? k
25
+ @@options[k] = default_options[k][1].call(v) ? v : default_options[k][0]
26
+ else
27
+ @@options[k] = v
28
+ end
27
29
  end
30
+ default_options.each do |k, v|
31
+ @@options[k] = v unless @@options.include? k
32
+ end
33
+ @@options
28
34
  end
29
- default_options.each do |k, v|
30
- @@options[k] = v unless @@options.include? k
31
- end
32
- @@options
33
- end
34
35
 
36
+ end
35
37
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module CometIO
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -4,8 +4,13 @@ require 'digest/md5'
4
4
  require 'event_emitter'
5
5
  require 'sinatra/streaming'
6
6
  require File.expand_path '../sinatra-cometio/version', File.dirname(__FILE__)
7
+ require File.expand_path '../sinatra-cometio/helpers', File.dirname(__FILE__)
7
8
  require File.expand_path '../sinatra-cometio/options', File.dirname(__FILE__)
8
9
  require File.expand_path '../sinatra-cometio/cometio', File.dirname(__FILE__)
9
10
  require File.expand_path '../sinatra-cometio/application', File.dirname(__FILE__)
10
11
 
11
- Sinatra.register Sinatra::CometIO
12
+ module Sinatra
13
+ module CometIO
14
+ end
15
+ register CometIO
16
+ end
@@ -3,7 +3,7 @@ require 'event_emitter'
3
3
  require 'httparty'
4
4
  require 'json'
5
5
 
6
- class CometIO
6
+ module CometIO
7
7
  class Client
8
8
  class Error < StandardError
9
9
  end
data/sample/Gemfile CHANGED
@@ -3,7 +3,7 @@ source :rubygems
3
3
  gem 'foreman'
4
4
  gem 'rack'
5
5
  gem 'sinatra'
6
- gem 'backports', '< 3.1.0'
6
+ gem 'backports'
7
7
  gem 'thin'
8
8
  gem 'sinatra-contrib'
9
9
  gem 'event_emitter'
data/sample/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- backports (3.0.3)
4
+ backports (3.1.1)
5
5
  daemons (1.1.9)
6
6
  event_emitter (0.2.3)
7
- eventmachine (1.0.1)
8
- foreman (0.61.0)
7
+ eventmachine (1.0.3)
8
+ foreman (0.62.0)
9
9
  thor (>= 0.13.6)
10
10
  haml (4.0.0)
11
11
  tilt
@@ -20,7 +20,7 @@ GEM
20
20
  rack
21
21
  rack-test (0.6.2)
22
22
  rack (>= 1.0)
23
- sass (3.2.6)
23
+ sass (3.2.7)
24
24
  sinatra (1.3.5)
25
25
  rack (~> 1.4)
26
26
  rack-protection (~> 1.3)
@@ -37,13 +37,13 @@ GEM
37
37
  eventmachine (>= 0.12.6)
38
38
  rack (>= 1.0.0)
39
39
  thor (0.17.0)
40
- tilt (1.3.4)
40
+ tilt (1.3.5)
41
41
 
42
42
  PLATFORMS
43
43
  ruby
44
44
 
45
45
  DEPENDENCIES
46
- backports (< 3.1.0)
46
+ backports
47
47
  event_emitter
48
48
  foreman
49
49
  haml
data/sample/config.ru CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  Bundler.require
4
+ require 'sinatra/base'
4
5
  if development?
5
6
  $stdout.sync = true
6
7
  require 'sinatra/reloader'
@@ -12,4 +13,4 @@ require File.dirname(__FILE__)+'/main'
12
13
  set :haml, :escape_html => true
13
14
  set :cometio, :timeout => 60
14
15
 
15
- run Sinatra::Application
16
+ run ChatApp
data/sample/main.rb CHANGED
@@ -1,30 +1,34 @@
1
- CometIO.on :chat do |data, from|
2
- puts "#{data['name']} : #{data['message']} (from:#{from})"
3
- self.push :chat, data
4
- end
1
+ class ChatApp < Sinatra::Base
2
+ register Sinatra::CometIO
5
3
 
6
- CometIO.on :connect do |session|
7
- puts "new client <#{session}>"
8
- CometIO.push :chat, {:name => "system", :message => "new client <#{session}>"}
9
- CometIO.push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
10
- end
4
+ cometio.on :chat do |data, from|
5
+ puts "#{data['name']} : #{data['message']} (from:#{from})"
6
+ push :chat, data
7
+ end
11
8
 
12
- CometIO.on :disconnect do |session|
13
- puts "disconnect client <#{session}>"
14
- CometIO.push :chat, {:name => "system", :message => "bye <#{session}>"}
15
- end
9
+ cometio.on :connect do |session|
10
+ puts "new client <#{session}>"
11
+ push :chat, {:name => "system", :message => "new client <#{session}>"}
12
+ push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
13
+ end
16
14
 
17
- EM::defer do
18
- loop do
19
- CometIO.push :chat, :name => 'clock', :message => Time.now.to_s
20
- sleep 60
15
+ cometio.on :disconnect do |session|
16
+ puts "disconnect client <#{session}>"
17
+ push :chat, {:name => "system", :message => "bye <#{session}>"}
21
18
  end
22
- end
23
19
 
24
- get '/' do
25
- haml :index
26
- end
20
+ EM::defer do
21
+ loop do
22
+ cometio.push :chat, :name => 'clock', :message => Time.now.to_s
23
+ sleep 60
24
+ end
25
+ end
27
26
 
28
- get '/:source.css' do
29
- scss params[:source].to_sym
27
+ get '/' do
28
+ haml :index
29
+ end
30
+
31
+ get '/:source.css' do
32
+ scss params[:source].to_sym
33
+ end
30
34
  end
data/test/app/config.ru CHANGED
@@ -1,9 +1,7 @@
1
1
  require 'rubygems'
2
- require 'bundler'
3
- Bundler.require
4
2
  $stdout.sync = true
5
3
  $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
6
- require 'sinatra'
4
+ require 'sinatra/base'
7
5
  require 'sinatra/cometio'
8
6
  require File.dirname(__FILE__)+'/main'
9
7
 
data/test/app/main.rb CHANGED
@@ -3,27 +3,28 @@ File.open(pid_file, "w+") do |f|
3
3
  f.write Process.pid.to_s
4
4
  end
5
5
 
6
- class TestApp < Sinatra::Application
6
+ class TestApp < Sinatra::Base
7
+ register Sinatra::CometIO
7
8
 
8
9
  get '/' do
9
10
  "sinatra-cometio v#{Sinatra::CometIO::VERSION}"
10
11
  end
11
12
 
12
- CometIO.on :connect do |session|
13
+ Sinatra::CometIO.on :connect do |session|
13
14
  puts "new client <#{session}>"
14
15
  end
15
16
 
16
- CometIO.on :disconnect do |session|
17
+ Sinatra::CometIO.on :disconnect do |session|
17
18
  puts "disconnect client <#{session}>"
18
19
  end
19
20
 
20
- CometIO.on :broadcast do |data, from|
21
+ Sinatra::CometIO.on :broadcast do |data, from|
21
22
  puts from
22
23
  puts "broadcast <#{from}> - #{data.to_json}"
23
24
  push :broadcast, data
24
25
  end
25
26
 
26
- CometIO.on :message do |data, from|
27
+ Sinatra::CometIO.on :message do |data, from|
27
28
  puts "message <#{from}> - #{data.to_json}"
28
29
  push :message, data, :to => data['to']
29
30
  end
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.2.0
4
+ version: 0.3.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-12 00:00:00.000000000 Z
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -141,6 +141,7 @@ files:
141
141
  - lib/sinatra-cometio.rb
142
142
  - lib/sinatra-cometio/application.rb
143
143
  - lib/sinatra-cometio/cometio.rb
144
+ - lib/sinatra-cometio/helpers.rb
144
145
  - lib/sinatra-cometio/options.rb
145
146
  - lib/sinatra-cometio/version.rb
146
147
  - lib/sinatra/cometio.rb