sinatra-rocketio 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sinatra-rocketio.gemspec
4
4
  gemspec
5
- gem 'minitest'
6
- gem 'thin'
7
- gem 'em-cometio-client'
@@ -1,3 +1,8 @@
1
+ === 0.1.0 2013-03-24
2
+
3
+ * add ruby client library - Sinatra::RocketIO::Client
4
+ * add route /rocketio/settings
5
+
1
6
  === 0.0.9 2013-03-21
2
7
 
3
8
  * bugfix javascrip websocket error in push() before connect
@@ -18,6 +18,15 @@ module Sinatra
18
18
  content_type 'application/javascript'
19
19
  @js ||= ERB.new(Sinatra::RocketIO.javascript).result(binding)
20
20
  end
21
+ app.get '/rocketio/settings' do
22
+ content_type 'application/json'
23
+ @setting_json ||= (
24
+ setting = {}
25
+ setting[:websocket] = websocketio_url if Sinatra::RocketIO.options[:websocket]
26
+ setting[:comet] = cometio_url if Sinatra::RocketIO.options[:comet]
27
+ setting.to_json
28
+ )
29
+ end
21
30
  app.routes["GET"].delete_if{|route|
22
31
  "/cometio/cometio.js" =~ route[0] or "/websocketio/websocketio.js" =~ route[0]
23
32
  }
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RocketIO
3
- VERSION = "0.0.9"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,62 @@
1
+ require File.expand_path '../../sinatra-rocketio/version', File.dirname(__FILE__)
2
+ require 'sinatra/cometio/client'
3
+ require 'sinatra/websocketio/client'
4
+ require 'httparty'
5
+ require 'json'
6
+ require 'event_emitter'
7
+
8
+ module Sinatra
9
+ module RocketIO
10
+ class Client
11
+ class Error < StandardError
12
+ end
13
+
14
+ include EventEmitter
15
+ attr_reader :settings, :type, :io
16
+
17
+ def initialize(url, opt={:type => :websocket})
18
+ @settings = JSON.parse HTTParty.get("#{url}/rocketio/settings").body
19
+ type = opt[:type].to_sym
20
+
21
+ if type == :websocket and @settings['websocket']
22
+ @type = :websocket
23
+ @io = Sinatra::WebSocketIO::Client.new @settings['websocket']
24
+ elsif type == :comet and @settings['comet']
25
+ @type = :comet
26
+ @io = Sinatra::CometIO::Client.new @settings['comet']
27
+ else
28
+ raise Error, "cannot find #{type} IO #{url}"
29
+ end
30
+ this = self
31
+ if @io
32
+ @io.on :* do |event_name, *args|
33
+ if args.size > 1
34
+ this.emit event_name, args[0], args[1]
35
+ else
36
+ this.emit event_name, args[0]
37
+ end
38
+ end
39
+ end
40
+ self
41
+ end
42
+
43
+ def connect
44
+ @io.connect
45
+ self
46
+ end
47
+
48
+ def close
49
+ @io.close
50
+ end
51
+
52
+ def push(type, data)
53
+ @io.push type, data
54
+ end
55
+
56
+ def method_missing(name, *args)
57
+ @io.__send__ name, *args
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -8,7 +8,7 @@ WebSocket/Comet Chat
8
8
  Install Dependencies
9
9
  --------------------
10
10
 
11
- % gem install bundler foreman
11
+ % gem install bundler
12
12
  % bundle install
13
13
 
14
14
 
@@ -17,6 +17,6 @@ Run
17
17
 
18
18
  % export PORT=5000
19
19
  % export WS_PORT=8080
20
- % foreman start
20
+ % rackup config.ru -p 5000
21
21
 
22
22
  => http://localhost:5000
@@ -2,37 +2,31 @@
2
2
  require 'rubygems'
3
3
  require 'bundler/setup'
4
4
  $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
5
- require 'eventmachine'
6
- require 'em-websocketio-client'
5
+ require 'sinatra/rocketio/client'
7
6
 
8
7
  name = `whoami`.strip || 'shokai'
9
8
 
10
- EM::run do
11
- client = EM::WebSocketIO::Client.new('ws://localhost:8080').connect
9
+ io = Sinatra::RocketIO::Client.new('http://localhost:5000').connect
10
+ #io = Sinatra::RocketIO::Client.new('http://localhost:5000', :type => :comet).connect
12
11
 
13
- client.on :connect do |session|
14
- puts "connect!! (session_id:#{session})"
15
- end
16
-
17
- client.on :chat do |data|
18
- puts "<#{data['name']}> #{data['message']}"
19
- end
20
-
21
- client.on :error do |err|
22
- STDERR.puts err
23
- end
12
+ io.on :connect do |session|
13
+ puts "#{io.type} connect!! (session_id:#{session})"
14
+ end
24
15
 
25
- client.on :disconnect do
26
- puts "disconnected!!"
27
- end
16
+ io.on :chat do |data|
17
+ puts "<#{data['name']}> #{data['message']}"
18
+ end
28
19
 
29
- EM::defer do
30
- loop do
31
- line = STDIN.gets.strip
32
- next if line.empty?
33
- client.push :chat, {:message => line, :name => name}
34
- end
35
- end
20
+ io.on :error do |err|
21
+ STDERR.puts err
36
22
  end
37
23
 
24
+ io.on :disconnect do
25
+ puts "disconnected!!"
26
+ end
38
27
 
28
+ loop do
29
+ line = STDIN.gets.strip
30
+ next if line.empty?
31
+ io.push :chat, {:message => line, :name => name}
32
+ end
@@ -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/rocketio'
12
+ require 'haml'
13
+ require 'sass'
11
14
  require File.dirname(__FILE__)+'/main'
12
15
 
13
16
  set :haml, :escape_html => true
@@ -15,8 +15,18 @@ Gem::Specification.new do |gem|
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
- gem.add_dependency "sinatra-cometio", ">= 0.3.9"
19
- gem.add_dependency "sinatra-websocketio", ">= 0.1.7"
18
+
19
+ gem.add_development_dependency "bundler", "~> 1.3"
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "minitest"
22
+ gem.add_development_dependency "thin"
23
+ gem.add_development_dependency "haml"
24
+ gem.add_development_dependency "sass"
25
+ gem.add_development_dependency "httparty"
26
+ gem.add_development_dependency "json"
27
+
28
+ gem.add_dependency "sinatra-cometio", ">= 0.4.0"
29
+ gem.add_dependency "sinatra-websocketio", ">= 0.2.0"
20
30
  gem.add_dependency "sinatra"
21
31
  gem.add_dependency "eventmachine", ">= 1.0.0"
22
32
  gem.add_dependency "event_emitter", ">= 0.2.4"
@@ -14,6 +14,10 @@ class App
14
14
  ENV['WS_PORT'] || 8080
15
15
  end
16
16
 
17
+ def self.url
18
+ "http://localhost:#{port}"
19
+ end
20
+
17
21
  def self.websocketio_url
18
22
  "ws://localhost:#{ws_port}"
19
23
  end
@@ -22,7 +26,6 @@ class App
22
26
  "http://localhost:#{port}/cometio/io"
23
27
  end
24
28
 
25
-
26
29
  def self.pid_file
27
30
  ENV['PID_FILE'] || "/tmp/sinatra-rocketio-testapp.pid"
28
31
  end
@@ -2,8 +2,9 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
4
  require 'minitest/autorun'
5
- require 'em-cometio-client'
6
- require 'em-websocketio-client'
5
+ require 'sinatra/rocketio/client'
6
+ require 'httparty'
7
+ require 'json'
7
8
  require File.expand_path 'app', File.dirname(__FILE__)
8
9
 
9
10
 
@@ -15,37 +15,29 @@ class TestRocketIO < MiniTest::Unit::TestCase
15
15
  post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
16
16
  res = nil
17
17
  res2 = nil
18
- EM::run do
19
- client = EM::WebSocketIO::Client.new(App.websocketio_url).connect
20
- client.on :message do |data|
21
- res = data
22
- end
23
-
24
- client.on :disconnect do
25
- EM.stop_event_loop
26
- end
18
+ client = Sinatra::RocketIO::Client.new(App.url, :type => :websocket).connect
19
+ client.on :message do |data|
20
+ res = data
21
+ end
27
22
 
28
- client.on :connect do |session|
29
- client2 = EM::CometIO::Client.new(App.cometio_url).connect
30
- client2.on :connect do |session2|
31
- post_data['to'] = session2
32
- client.push :message, post_data
33
- end
34
- client2.on :message do |data|
35
- res2 = data
36
- client2.close
37
- client.close
38
- end
23
+ client.on :connect do |session|
24
+ client2 = Sinatra::RocketIO::Client.new(App.url, :type => :comet).connect
25
+ client2.on :connect do |session2|
26
+ post_data['to'] = session2
27
+ client.push :message, post_data
39
28
  end
40
-
41
- EM::defer do
42
- 50.times do
43
- break if res != nil
44
- sleep 0.1
45
- end
29
+ client2.on :message do |data|
30
+ res2 = data
31
+ client2.close
46
32
  client.close
47
33
  end
48
34
  end
35
+
36
+ 50.times do
37
+ break if res != nil
38
+ sleep 0.1
39
+ end
40
+ client.close
49
41
  assert res2 != nil, 'server not respond'
50
42
  assert res2["time"] == post_data[:time]
51
43
  assert res2["msg"] == post_data[:msg]
@@ -57,40 +49,29 @@ class TestRocketIO < MiniTest::Unit::TestCase
57
49
  post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
58
50
  res = nil
59
51
  res2 = nil
60
- EM::run do
61
- client = EM::CometIO::Client.new(App.cometio_url).connect
62
- client.on :message do |data|
63
- res = data
64
- end
65
-
66
- client.on :disconnect do
67
- EM.stop_event_loop
68
- end
52
+ client = Sinatra::RocketIO::Client.new(App.url, :type => :comet).connect
53
+ client.on :message do |data|
54
+ res = data
55
+ end
69
56
 
70
- client.on :connect do |session|
71
- client2 = EM::WebSocketIO::Client.new(App.websocketio_url).connect
72
- client2.on :connect do |session2|
73
- post_data['to'] = session2
74
- client.push :message, post_data
75
- end
76
- client2.on :message do |data|
77
- res2 = data
78
- client2.close
79
- client.close
80
- end
81
- client2.on :disconnect do
82
- EM.stop_event_loop
83
- end
57
+ client.on :connect do |session|
58
+ client2 = Sinatra::RocketIO::Client.new(App.url, :type => :websocket).connect
59
+ client2.on :connect do |session2|
60
+ post_data['to'] = session2
61
+ client.push :message, post_data
84
62
  end
85
-
86
- EM::defer do
87
- 50.times do
88
- break if res != nil
89
- sleep 0.1
90
- end
63
+ client2.on :message do |data|
64
+ res2 = data
65
+ client2.close
91
66
  client.close
92
67
  end
93
68
  end
69
+
70
+ 50.times do
71
+ break if res != nil
72
+ sleep 0.1
73
+ end
74
+ client.close
94
75
  assert res2 != nil, 'server not respond'
95
76
  assert res2["time"] == post_data[:time]
96
77
  assert res2["msg"] == post_data[:msg]
@@ -0,0 +1,19 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestSettingsIO < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ App.start
7
+ end
8
+
9
+ def teardown
10
+ App.stop
11
+ end
12
+
13
+ def test_settings
14
+ res = JSON.parse HTTParty.get("#{App.url}/rocketio/settings").body
15
+ assert App.websocketio_url == res['websocket']
16
+ assert App.cometio_url == res['comet']
17
+ end
18
+
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rocketio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,136 @@ 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: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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: minitest
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: thin
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'
78
+ - !ruby/object:Gem::Dependency
79
+ name: haml
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sass
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: httparty
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: json
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
14
142
  - !ruby/object:Gem::Dependency
15
143
  name: sinatra-cometio
16
144
  requirement: !ruby/object:Gem::Requirement
@@ -18,7 +146,7 @@ dependencies:
18
146
  requirements:
19
147
  - - ! '>='
20
148
  - !ruby/object:Gem::Version
21
- version: 0.3.9
149
+ version: 0.4.0
22
150
  type: :runtime
23
151
  prerelease: false
24
152
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +154,7 @@ dependencies:
26
154
  requirements:
27
155
  - - ! '>='
28
156
  - !ruby/object:Gem::Version
29
- version: 0.3.9
157
+ version: 0.4.0
30
158
  - !ruby/object:Gem::Dependency
31
159
  name: sinatra-websocketio
32
160
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +162,7 @@ dependencies:
34
162
  requirements:
35
163
  - - ! '>='
36
164
  - !ruby/object:Gem::Version
37
- version: 0.1.7
165
+ version: 0.2.0
38
166
  type: :runtime
39
167
  prerelease: false
40
168
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +170,7 @@ dependencies:
42
170
  requirements:
43
171
  - - ! '>='
44
172
  - !ruby/object:Gem::Version
45
- version: 0.1.7
173
+ version: 0.2.0
46
174
  - !ruby/object:Gem::Dependency
47
175
  name: sinatra
48
176
  requirement: !ruby/object:Gem::Requirement
@@ -114,9 +242,7 @@ files:
114
242
  - lib/sinatra-rocketio/rocketio.rb
115
243
  - lib/sinatra-rocketio/version.rb
116
244
  - lib/sinatra/rocketio.rb
117
- - sample/Gemfile
118
- - sample/Gemfile.lock
119
- - sample/Procfile
245
+ - lib/sinatra/rocketio/client.rb
120
246
  - sample/README.md
121
247
  - sample/bin/cui_chat_client.rb
122
248
  - sample/config.ru
@@ -130,6 +256,7 @@ files:
130
256
  - test/app/main.rb
131
257
  - test/test_helper.rb
132
258
  - test/test_rocketio.rb
259
+ - test/test_settings.rb
133
260
  homepage: https://github.com/shokai/sinatra-rocketio
134
261
  licenses: []
135
262
  post_install_message:
@@ -142,12 +269,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
269
  - - ! '>='
143
270
  - !ruby/object:Gem::Version
144
271
  version: '0'
272
+ segments:
273
+ - 0
274
+ hash: 4356497738858812236
145
275
  required_rubygems_version: !ruby/object:Gem::Requirement
146
276
  none: false
147
277
  requirements:
148
278
  - - ! '>='
149
279
  - !ruby/object:Gem::Version
150
280
  version: '0'
281
+ segments:
282
+ - 0
283
+ hash: 4356497738858812236
151
284
  requirements: []
152
285
  rubyforge_project:
153
286
  rubygems_version: 1.8.24
@@ -160,3 +293,4 @@ test_files:
160
293
  - test/app/main.rb
161
294
  - test/test_helper.rb
162
295
  - test/test_rocketio.rb
296
+ - test/test_settings.rb
@@ -1,11 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'foreman'
4
- gem 'sinatra-cometio', '>= 0.3.9'
5
- gem 'sinatra-websocketio', '>= 0.1.7'
6
- gem 'sinatra'
7
- gem 'rack'
8
- gem 'thin'
9
- gem 'haml'
10
- gem 'sass'
11
- gem 'event_emitter'
@@ -1,81 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- addressable (2.3.3)
5
- backports (3.1.1)
6
- daemons (1.1.9)
7
- em-websocket (0.5.0)
8
- eventmachine (>= 0.12.9)
9
- http_parser.rb (~> 0.5.3)
10
- em-websocket-client (0.1.1)
11
- eventmachine
12
- libwebsocket
13
- event_emitter (0.2.4)
14
- eventmachine (1.0.3)
15
- foreman (0.62.0)
16
- thor (>= 0.13.6)
17
- haml (4.0.1)
18
- tilt
19
- http_parser.rb (0.5.3)
20
- httparty (0.10.2)
21
- multi_json (~> 1.0)
22
- multi_xml (>= 0.5.2)
23
- json (1.7.7)
24
- libwebsocket (0.1.7.1)
25
- addressable
26
- websocket
27
- multi_json (1.7.1)
28
- multi_xml (0.5.3)
29
- rack (1.5.2)
30
- rack-protection (1.5.0)
31
- rack
32
- rack-test (0.6.2)
33
- rack (>= 1.0)
34
- sass (3.2.7)
35
- sinatra (1.3.6)
36
- rack (~> 1.4)
37
- rack-protection (~> 1.3)
38
- tilt (~> 1.3, >= 1.3.3)
39
- sinatra-cometio (0.3.9)
40
- event_emitter (>= 0.2.4)
41
- eventmachine (>= 1.0.0)
42
- httparty (>= 0.10.2)
43
- json (>= 1.7.0)
44
- sinatra (>= 1.3.0)
45
- sinatra-contrib (>= 1.3.2)
46
- sinatra-contrib (1.3.2)
47
- backports (>= 2.0)
48
- eventmachine
49
- rack-protection
50
- rack-test
51
- sinatra (~> 1.3.0)
52
- tilt (~> 1.3)
53
- sinatra-websocketio (0.1.7)
54
- em-websocket (>= 0.5.0)
55
- em-websocket-client (>= 0.1.1)
56
- event_emitter (>= 0.2.4)
57
- eventmachine (>= 1.0.0)
58
- json (>= 1.7.0)
59
- sinatra (>= 1.3.0)
60
- sinatra-contrib (>= 1.3.2)
61
- thin (1.5.1)
62
- daemons (>= 1.0.9)
63
- eventmachine (>= 0.12.6)
64
- rack (>= 1.0.0)
65
- thor (0.17.0)
66
- tilt (1.3.6)
67
- websocket (1.0.7)
68
-
69
- PLATFORMS
70
- ruby
71
-
72
- DEPENDENCIES
73
- event_emitter
74
- foreman
75
- haml
76
- rack
77
- sass
78
- sinatra
79
- sinatra-cometio (>= 0.3.9)
80
- sinatra-websocketio (>= 0.1.7)
81
- thin
@@ -1 +0,0 @@
1
- web: bundle exec rackup config.ru -p $PORT