sinatra-cometio 0.3.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -2
- data/History.txt +6 -0
- data/README.md +8 -4
- data/lib/sinatra-cometio/version.rb +1 -1
- data/lib/sinatra/cometio/client.rb +55 -53
- data/sample/bin/cui_chat_client.rb +1 -1
- data/sample/config.ru +3 -0
- data/sinatra-cometio.gemspec +5 -0
- data/test/test_cometio.rb +5 -5
- data/test/test_helper.rb +1 -2
- metadata +66 -5
- data/sample/Gemfile +0 -11
- data/sample/Gemfile.lock +0 -54
- data/sample/Procfile +0 -1
data/Gemfile
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -44,11 +44,12 @@ io.push :light, {:value => 150}, {:to => session_id} # to specific client
|
|
44
44
|
Client Side
|
45
45
|
|
46
46
|
```html
|
47
|
-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.
|
47
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
48
48
|
<script src="<%= cometio_js %>"></script>
|
49
49
|
```
|
50
50
|
```javascript
|
51
51
|
var io = new CometIO().connect();
|
52
|
+
|
52
53
|
io.on("temperature", function(value){
|
53
54
|
console.log("server temperature : " + value);
|
54
55
|
}); // => "server temperature : 35"
|
@@ -63,7 +64,9 @@ io.on("light", function(data){
|
|
63
64
|
Client Side
|
64
65
|
|
65
66
|
```javascript
|
66
|
-
io.
|
67
|
+
io.on("connect", function(){
|
68
|
+
io.push("chat", {name: "shokai", message: "hello"}); // client -> server
|
69
|
+
});
|
67
70
|
```
|
68
71
|
|
69
72
|
Server Side
|
@@ -80,8 +83,8 @@ end
|
|
80
83
|
Client Side
|
81
84
|
|
82
85
|
```javascript
|
83
|
-
io.on("connect", function(
|
84
|
-
alert("connect!!");
|
86
|
+
io.on("connect", function(session_id){
|
87
|
+
alert("connect!! "+session_id);
|
85
88
|
});
|
86
89
|
```
|
87
90
|
|
@@ -90,6 +93,7 @@ Server Side
|
|
90
93
|
```ruby
|
91
94
|
io.on :connect do |session|
|
92
95
|
puts "new client <#{session}>"
|
96
|
+
io.push :hello, "hello new client!!"
|
93
97
|
end
|
94
98
|
|
95
99
|
io.on :disconnect do |session|
|
@@ -3,74 +3,76 @@ require 'event_emitter'
|
|
3
3
|
require 'httparty'
|
4
4
|
require 'json'
|
5
5
|
|
6
|
-
module
|
7
|
-
|
8
|
-
class
|
9
|
-
|
6
|
+
module Sinatra
|
7
|
+
module CometIO
|
8
|
+
class Client
|
9
|
+
class Error < StandardError
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
include EventEmitter
|
13
|
+
attr_reader :url, :session
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def initialize(url)
|
16
|
+
raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
|
17
|
+
@url = url
|
18
|
+
@session = nil
|
19
|
+
@running = false
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
def push(type, data)
|
23
|
+
begin
|
24
|
+
res = HTTParty.post @url, :timeout => 10, :body => {:type => type, :data => data, :session => @session}
|
25
|
+
emit :error, "CometIO push error" unless res.code == 200
|
26
|
+
rescue StandardError, Timeout::Error => e
|
27
|
+
emit :error, "CometIO push error"
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
def connect
|
32
|
+
return self if @running
|
33
|
+
self.on :__session_id do |session|
|
34
|
+
@session = session
|
35
|
+
self.emit :connect, @session
|
36
|
+
end
|
37
|
+
@running = true
|
38
|
+
get
|
39
|
+
return self
|
35
40
|
end
|
36
|
-
@running = true
|
37
|
-
get
|
38
|
-
return self
|
39
|
-
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
def close
|
43
|
+
@running = false
|
44
|
+
self.remove_listener :__session_id
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
private
|
48
|
+
def get
|
49
|
+
Thread.new do
|
50
|
+
while @running do
|
51
|
+
begin
|
52
|
+
res = HTTParty.get "#{@url}?session=#{@session}", :timeout => 120
|
53
|
+
unless res.code == 200
|
54
|
+
self.emit :error, "CometIO get error"
|
55
|
+
sleep 10
|
56
|
+
next
|
57
|
+
else
|
58
|
+
data_arr = JSON.parse res.body
|
59
|
+
data_arr = [data_arr] unless data_arr.kind_of? Array
|
60
|
+
data_arr.each do |data|
|
61
|
+
self.emit data['type'], data['data']
|
62
|
+
end
|
63
|
+
next
|
64
|
+
end
|
65
|
+
rescue Timeout::Error, JSON::ParserError
|
66
|
+
next
|
67
|
+
rescue StandardError
|
53
68
|
self.emit :error, "CometIO get error"
|
54
69
|
sleep 10
|
55
70
|
next
|
56
|
-
else
|
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
|
62
|
-
next
|
63
71
|
end
|
64
|
-
rescue Timeout::Error, JSON::ParserError
|
65
|
-
next
|
66
|
-
rescue StandardError
|
67
|
-
self.emit :error, "CometIO get error"
|
68
|
-
sleep 10
|
69
|
-
next
|
70
72
|
end
|
71
73
|
end
|
72
74
|
end
|
73
|
-
end
|
74
75
|
|
76
|
+
end
|
75
77
|
end
|
76
78
|
end
|
@@ -5,7 +5,7 @@ require 'sinatra/cometio/client'
|
|
5
5
|
|
6
6
|
name = `whoami`.strip || 'shokai'
|
7
7
|
|
8
|
-
client = CometIO::Client.new('http://localhost:5000/cometio/io').connect
|
8
|
+
client = Sinatra::CometIO::Client.new('http://localhost:5000/cometio/io').connect
|
9
9
|
|
10
10
|
client.on :connect do |session|
|
11
11
|
puts "connect!! (session_id:#{session})"
|
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'
|
4
5
|
require 'sinatra/base'
|
5
6
|
if development?
|
6
7
|
$stdout.sync = true
|
@@ -8,6 +9,8 @@ if development?
|
|
8
9
|
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
9
10
|
end
|
10
11
|
require 'sinatra/cometio'
|
12
|
+
require 'haml'
|
13
|
+
require 'sass'
|
11
14
|
require File.dirname(__FILE__)+'/main'
|
12
15
|
|
13
16
|
set :haml, :escape_html => true
|
data/sinatra-cometio.gemspec
CHANGED
@@ -16,6 +16,11 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
17
|
gem.require_paths = ["lib"]
|
18
18
|
|
19
|
+
gem.add_development_dependency 'minitest'
|
20
|
+
gem.add_development_dependency 'thin'
|
21
|
+
gem.add_development_dependency 'haml'
|
22
|
+
gem.add_development_dependency 'sass'
|
23
|
+
|
19
24
|
gem.add_dependency 'sinatra-contrib', '>= 1.3.2'
|
20
25
|
gem.add_dependency 'sinatra', '>= 1.3.0'
|
21
26
|
gem.add_dependency 'eventmachine', '>= 1.0.0'
|
data/test/test_cometio.rb
CHANGED
@@ -11,7 +11,7 @@ class TestCometio < MiniTest::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_simple
|
14
|
-
client = CometIO::Client.new(App.cometio_url).connect
|
14
|
+
client = Sinatra::CometIO::Client.new(App.cometio_url).connect
|
15
15
|
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
16
16
|
res = nil
|
17
17
|
client.on :broadcast do |data|
|
@@ -35,7 +35,7 @@ class TestCometio < MiniTest::Unit::TestCase
|
|
35
35
|
def test_client_to_client2
|
36
36
|
## client --> server --> client2
|
37
37
|
|
38
|
-
client = CometIO::Client.new(App.cometio_url).connect
|
38
|
+
client = Sinatra::CometIO::Client.new(App.cometio_url).connect
|
39
39
|
post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
|
40
40
|
res = nil
|
41
41
|
client.on :message do |data|
|
@@ -44,7 +44,7 @@ class TestCometio < MiniTest::Unit::TestCase
|
|
44
44
|
|
45
45
|
res2 = nil
|
46
46
|
client.on :connect do |session|
|
47
|
-
client2 = CometIO::Client.new(App.cometio_url).connect
|
47
|
+
client2 = Sinatra::CometIO::Client.new(App.cometio_url).connect
|
48
48
|
client2.on :connect do |session2|
|
49
49
|
post_data['to'] = session2
|
50
50
|
client.push :message, post_data
|
@@ -69,7 +69,7 @@ class TestCometio < MiniTest::Unit::TestCase
|
|
69
69
|
|
70
70
|
def test_broadcast
|
71
71
|
## client --> server --> client&client2
|
72
|
-
client = CometIO::Client.new(App.cometio_url).connect
|
72
|
+
client = Sinatra::CometIO::Client.new(App.cometio_url).connect
|
73
73
|
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
74
74
|
res = nil
|
75
75
|
client.on :broadcast do |data|
|
@@ -79,7 +79,7 @@ class TestCometio < MiniTest::Unit::TestCase
|
|
79
79
|
|
80
80
|
res2 = nil
|
81
81
|
client.on :connect do |session|
|
82
|
-
client2 = CometIO::Client.new(App.cometio_url).connect
|
82
|
+
client2 = Sinatra::CometIO::Client.new(App.cometio_url).connect
|
83
83
|
client2.on :connect do |session2|
|
84
84
|
client.push :broadcast, post_data
|
85
85
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
1
2
|
require 'rubygems'
|
2
3
|
require 'bundler/setup'
|
3
4
|
require 'minitest/autorun'
|
4
5
|
require 'sinatra/cometio/client'
|
5
6
|
require File.expand_path 'app', File.dirname(__FILE__)
|
6
7
|
|
7
|
-
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
8
|
-
|
9
8
|
['SIGHUP', 'SIGINT', 'SIGKILL', 'SIGTERM'].each do |sig|
|
10
9
|
Kernel.trap sig do
|
11
10
|
App.stop
|
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.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,72 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: thin
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: haml
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sass
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
14
78
|
- !ruby/object:Gem::Dependency
|
15
79
|
name: sinatra-contrib
|
16
80
|
requirement: !ruby/object:Gem::Requirement
|
@@ -132,9 +196,6 @@ files:
|
|
132
196
|
- lib/sinatra/cometio.rb
|
133
197
|
- lib/sinatra/cometio/client.rb
|
134
198
|
- sample/.gitignore
|
135
|
-
- sample/Gemfile
|
136
|
-
- sample/Gemfile.lock
|
137
|
-
- sample/Procfile
|
138
199
|
- sample/README.md
|
139
200
|
- sample/bin/cui_chat_client.rb
|
140
201
|
- sample/config.ru
|
data/sample/Gemfile
DELETED
data/sample/Gemfile.lock
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: https://rubygems.org/
|
3
|
-
specs:
|
4
|
-
backports (3.1.1)
|
5
|
-
daemons (1.1.9)
|
6
|
-
event_emitter (0.2.4)
|
7
|
-
eventmachine (1.0.3)
|
8
|
-
foreman (0.62.0)
|
9
|
-
thor (>= 0.13.6)
|
10
|
-
haml (4.0.1)
|
11
|
-
tilt
|
12
|
-
httparty (0.10.2)
|
13
|
-
multi_json (~> 1.0)
|
14
|
-
multi_xml (>= 0.5.2)
|
15
|
-
json (1.7.7)
|
16
|
-
multi_json (1.7.1)
|
17
|
-
multi_xml (0.5.3)
|
18
|
-
rack (1.5.2)
|
19
|
-
rack-protection (1.5.0)
|
20
|
-
rack
|
21
|
-
rack-test (0.6.2)
|
22
|
-
rack (>= 1.0)
|
23
|
-
sass (3.2.7)
|
24
|
-
sinatra (1.3.6)
|
25
|
-
rack (~> 1.4)
|
26
|
-
rack-protection (~> 1.3)
|
27
|
-
tilt (~> 1.3, >= 1.3.3)
|
28
|
-
sinatra-contrib (1.3.2)
|
29
|
-
backports (>= 2.0)
|
30
|
-
eventmachine
|
31
|
-
rack-protection
|
32
|
-
rack-test
|
33
|
-
sinatra (~> 1.3.0)
|
34
|
-
tilt (~> 1.3)
|
35
|
-
thin (1.5.1)
|
36
|
-
daemons (>= 1.0.9)
|
37
|
-
eventmachine (>= 0.12.6)
|
38
|
-
rack (>= 1.0.0)
|
39
|
-
thor (0.17.0)
|
40
|
-
tilt (1.3.6)
|
41
|
-
|
42
|
-
PLATFORMS
|
43
|
-
ruby
|
44
|
-
|
45
|
-
DEPENDENCIES
|
46
|
-
event_emitter
|
47
|
-
foreman
|
48
|
-
haml
|
49
|
-
httparty
|
50
|
-
json
|
51
|
-
sass
|
52
|
-
sinatra
|
53
|
-
sinatra-contrib
|
54
|
-
thin
|
data/sample/Procfile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
web: bundle exec rackup config.ru -p $PORT
|