sinatra-cometio 0.2.0 → 0.3.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 +4 -0
- data/README.md +8 -8
- data/lib/sinatra-cometio/application.rb +63 -66
- data/lib/sinatra-cometio/cometio.rb +40 -37
- data/lib/sinatra-cometio/helpers.rb +15 -0
- data/lib/sinatra-cometio/options.rb +30 -28
- data/lib/sinatra-cometio/version.rb +1 -1
- data/lib/sinatra/cometio.rb +6 -1
- data/lib/sinatra/cometio/client.rb +1 -1
- data/sample/Gemfile +1 -1
- data/sample/Gemfile.lock +6 -6
- data/sample/config.ru +2 -1
- data/sample/main.rb +27 -23
- data/test/app/config.ru +1 -3
- data/test/app/main.rb +6 -5
- metadata +3 -2
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -34,8 +34,8 @@ set :cometio, :timeout => 120
|
|
34
34
|
run Sinatra::Application
|
35
35
|
```
|
36
36
|
```ruby
|
37
|
-
|
38
|
-
|
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
|
-
|
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
|
-
|
88
|
+
cometio.on :connect do |session|
|
89
89
|
puts "new client <#{session}>"
|
90
90
|
end
|
91
91
|
|
92
|
-
|
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 =
|
112
|
+
event_id = cometio.on :chat do |data, from|
|
113
113
|
puts "#{data} - from#{from}"
|
114
114
|
end
|
115
|
-
|
115
|
+
cometio.removeListener event_id
|
116
116
|
```
|
117
117
|
|
118
118
|
or
|
119
119
|
|
120
120
|
```ruby
|
121
|
-
|
121
|
+
cometio.removeListener :chat # remove all "chat" listener
|
122
122
|
```
|
123
123
|
|
124
124
|
|
@@ -1,82 +1,79 @@
|
|
1
|
-
module Sinatra
|
1
|
+
module Sinatra
|
2
|
+
module CometIO
|
2
3
|
|
3
|
-
|
4
|
-
|
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
|
17
|
-
|
8
|
+
def cometio
|
9
|
+
Sinatra::CometIO
|
18
10
|
end
|
19
|
-
end
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
1
|
+
module Sinatra
|
2
|
+
module CometIO
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
48
|
-
|
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
|
-
|
1
|
+
module Sinatra
|
2
|
+
module CometIO
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def self.default_options
|
5
|
+
{
|
6
|
+
:timeout => [120, lambda{|v| v.kind_of? Fixnum and v >= 20 }]
|
7
|
+
}
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
data/lib/sinatra/cometio.rb
CHANGED
@@ -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
|
-
|
12
|
+
module Sinatra
|
13
|
+
module CometIO
|
14
|
+
end
|
15
|
+
register CometIO
|
16
|
+
end
|
data/sample/Gemfile
CHANGED
data/sample/Gemfile.lock
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
backports (3.
|
4
|
+
backports (3.1.1)
|
5
5
|
daemons (1.1.9)
|
6
6
|
event_emitter (0.2.3)
|
7
|
-
eventmachine (1.0.
|
8
|
-
foreman (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.
|
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.
|
40
|
+
tilt (1.3.5)
|
41
41
|
|
42
42
|
PLATFORMS
|
43
43
|
ruby
|
44
44
|
|
45
45
|
DEPENDENCIES
|
46
|
-
backports
|
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
|
16
|
+
run ChatApp
|
data/sample/main.rb
CHANGED
@@ -1,30 +1,34 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
self.push :chat, data
|
4
|
-
end
|
1
|
+
class ChatApp < Sinatra::Base
|
2
|
+
register Sinatra::CometIO
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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 '
|
29
|
-
|
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
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::
|
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.
|
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
|
+
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
|