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 CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sinatra-cometio.gemspec
4
4
  gemspec
5
- gem 'minitest'
6
- gem 'thin'
@@ -1,3 +1,9 @@
1
+ === 0.4.0 2013-03-23
2
+
3
+ * rename CometIO::Client -> Sinatra::CometIO::Client
4
+ * use gem.add_development_dependency
5
+ * delete sample/Gemfile
6
+
1
7
  === 0.3.9 2013-03-21
2
8
 
3
9
  * emit "error" on push before connect
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.8.2/jquery.min.js"></script>
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.push("chat", {name: "shokai", message: "hello"}); // client -> server
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(session){
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|
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module CometIO
3
- VERSION = '0.3.9'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -3,74 +3,76 @@ require 'event_emitter'
3
3
  require 'httparty'
4
4
  require 'json'
5
5
 
6
- module CometIO
7
- class Client
8
- class Error < StandardError
9
- end
6
+ module Sinatra
7
+ module CometIO
8
+ class Client
9
+ class Error < StandardError
10
+ end
10
11
 
11
- include EventEmitter
12
- attr_reader :url, :session
12
+ include EventEmitter
13
+ attr_reader :url, :session
13
14
 
14
- def initialize(url)
15
- raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
16
- @url = url
17
- @session = nil
18
- @running = false
19
- end
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
- def push(type, data)
22
- begin
23
- res = HTTParty.post @url, :timeout => 10, :body => {:type => type, :data => data, :session => @session}
24
- emit :error, "CometIO push error" unless res.code == 200
25
- rescue StandardError, Timeout::Error => e
26
- emit :error, "CometIO push error"
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
- def connect
31
- return self if @running
32
- self.on :__session_id do |session|
33
- @session = session
34
- self.emit :connect, @session
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
- def close
42
- @running = false
43
- self.remove_listener :__session_id
44
- end
42
+ def close
43
+ @running = false
44
+ self.remove_listener :__session_id
45
+ end
45
46
 
46
- private
47
- def get
48
- Thread.new do
49
- while @running do
50
- begin
51
- res = HTTParty.get "#{@url}?session=#{@session}", :timeout => 120
52
- unless res.code == 200
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})"
@@ -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
@@ -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'
@@ -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
@@ -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.3.9
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-21 00:00:00.000000000 Z
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
@@ -1,11 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'foreman'
4
- gem 'sinatra-contrib'
5
- gem 'sinatra'
6
- gem 'thin'
7
- gem 'event_emitter'
8
- gem 'json'
9
- gem 'haml'
10
- gem 'sass'
11
- gem 'httparty'
@@ -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
@@ -1 +0,0 @@
1
- web: bundle exec rackup config.ru -p $PORT