sinatra-rocketio 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sinatra-rocketio.gemspec
4
4
  gemspec
5
+ gem 'thin'
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.2 2013-03-18
2
+
3
+ * sinatra config - set :rocketio, :cometio => true, :websocket => true
4
+
1
5
  === 0.0.1 2013-03-17
2
6
 
3
7
  * first release
data/README.md CHANGED
@@ -32,6 +32,7 @@ require 'sinatra'
32
32
  require 'sinatra/rocketio'
33
33
  set :cometio, :timeout => 120
34
34
  set :websocketio, :port => 8080
35
+ set :rocketio, :websocket => true, :comet => true # enable WebSocket and Comet
35
36
 
36
37
  run Sinatra::Application
37
38
  ```
data/lib/js/rocketio.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var RocketIO = function(){
2
2
  new EventEmitter().apply(this);
3
- this.type = null; // comet, websocket
3
+ this.type = null; // "comet", "websocket"
4
4
  this.session = null;
5
5
  this.io = null;
6
6
  var self = this;
@@ -9,14 +9,22 @@ var RocketIO = function(){
9
9
  this.connect = function(){
10
10
  self.io = function(){
11
11
  if(self.type === "comet") return;
12
+ if(typeof WebSocketIO === "undefined") return;
12
13
  var io = new WebSocketIO();
13
14
  io.session = self.session;
14
15
  return io.connect();
15
16
  }() || function(){
17
+ if(typeof CometIO === "undefined") return;
16
18
  var io = new CometIO();
17
19
  io.session = self.session;
18
20
  return io.connect();
19
21
  }();
22
+ if(typeof self.io === "undefined"){
23
+ setTimeout(function(){
24
+ self.emit("error", "WebSocketIO and CometIO are not available")
25
+ }, 100);
26
+ return self;
27
+ };
20
28
  if(self.io.url.match(/^ws:\/\/.+/)) self.type = "websocket";
21
29
  else if(self.io.url.match(/cometio/)) self.type = "comet";
22
30
  else self.type = "unknown";
@@ -1,6 +1,5 @@
1
1
  require 'eventmachine'
2
- require 'sinatra/cometio'
3
- require 'sinatra/websocketio'
2
+ require 'event_emitter'
4
3
  require File.expand_path '../sinatra-rocketio/version', File.dirname(__FILE__)
5
4
  require File.expand_path '../sinatra-rocketio/helpers', File.dirname(__FILE__)
6
5
  require File.expand_path '../sinatra-rocketio/options', File.dirname(__FILE__)
@@ -2,13 +2,24 @@ module Sinatra
2
2
  module RocketIO
3
3
 
4
4
  def self.registered(app)
5
- app.register Sinatra::CometIO
6
- app.register Sinatra::WebSocketIO
7
5
  app.helpers Sinatra::RocketIO::Helpers
8
-
9
- app.get '/rocketio/rocketio.js' do
10
- content_type 'application/javascript'
11
- @js ||= ERB.new(Sinatra::RocketIO.javascript).result(binding)
6
+ EM::defer do
7
+ while !EM::reactor_running? do
8
+ sleep 1
9
+ end
10
+ if options[:comet]
11
+ require 'sinatra/cometio'
12
+ app.register Sinatra::CometIO
13
+ end
14
+ if options[:websocket]
15
+ require 'sinatra/websocketio'
16
+ app.register Sinatra::WebSocketIO
17
+ end
18
+ app.get '/rocketio/rocketio.js' do
19
+ content_type 'application/javascript'
20
+ @js ||= ERB.new(Sinatra::RocketIO.javascript).result(binding)
21
+ end
22
+ Sinatra::RocketIO.emit :regist_events
12
23
  end
13
24
  end
14
25
 
@@ -7,9 +7,9 @@ module Sinatra
7
7
  js_file_names.each do |i|
8
8
  js += case i
9
9
  when 'cometio.js'
10
- Sinatra::CometIO.javascript 'cometio.js'
10
+ options[:comet] ? Sinatra::CometIO.javascript('cometio.js') : ''
11
11
  when 'websocketio.js'
12
- Sinatra::WebSocketIO.javascript 'websocketio.js'
12
+ options[:websocket] ? Sinatra::WebSocketIO.javascript('websocketio.js') : ''
13
13
  else
14
14
  j = ''
15
15
  File.open(File.expand_path "../js/#{i}", File.dirname(__FILE__)) do |f|
@@ -11,6 +11,8 @@ module Sinatra
11
11
 
12
12
  def self.default_options
13
13
  {
14
+ :comet => [true, lambda{|v| [true, false].include? v }],
15
+ :websocket => [true, lambda{|v| [true, false].include? v }]
14
16
  }
15
17
  end
16
18
 
@@ -2,8 +2,8 @@ module Sinatra
2
2
  module RocketIO
3
3
 
4
4
  def self.push(type, data, opt={})
5
- Sinatra::CometIO.push type, data, opt
6
- Sinatra::WebSocketIO.push type, data, opt
5
+ Sinatra::CometIO.push type, data, opt if options[:comet]
6
+ Sinatra::WebSocketIO.push type, data, opt if options[:websocket]
7
7
  end
8
8
 
9
9
  def self.sessions
@@ -15,18 +15,26 @@ module Sinatra
15
15
 
16
16
  end
17
17
  end
18
+
18
19
  EventEmitter.apply Sinatra::RocketIO
19
- Sinatra::CometIO.on :* do |event_name, *args|
20
- if args.size > 1
21
- Sinatra::RocketIO.emit event_name, args[0], args[1], :comet
22
- else
23
- Sinatra::RocketIO.emit event_name, args[0], :comet
20
+
21
+ Sinatra::RocketIO.once :regist_events do
22
+ if options[:comet]
23
+ Sinatra::CometIO.on :* do |event_name, *args|
24
+ if args.size > 1
25
+ Sinatra::RocketIO.emit event_name, args[0], args[1], :comet
26
+ else
27
+ Sinatra::RocketIO.emit event_name, args[0], :comet
28
+ end
29
+ end
24
30
  end
25
- end
26
- Sinatra::WebSocketIO.on :* do |event_name, *args|
27
- if args.size > 1
28
- Sinatra::RocketIO.emit event_name, args[0], args[1], :websocket
29
- else
30
- Sinatra::RocketIO.emit event_name, args[0], :websocket
31
+ if options[:websocket]
32
+ Sinatra::WebSocketIO.on :* do |event_name, *args|
33
+ if args.size > 1
34
+ Sinatra::RocketIO.emit event_name, args[0], args[1], :websocket
35
+ else
36
+ Sinatra::RocketIO.emit event_name, args[0], :websocket
37
+ end
38
+ end
31
39
  end
32
40
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RocketIO
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/sample/Gemfile CHANGED
@@ -1,9 +1,10 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'foreman'
4
4
  gem 'rack'
5
5
  gem 'sinatra'
6
6
  gem 'thin'
7
+ gem 'event_emitter'
7
8
  gem 'sinatra-cometio'
8
9
  gem 'sinatra-websocketio'
9
10
  gem 'haml'
data/sample/Gemfile.lock CHANGED
@@ -1,5 +1,5 @@
1
1
  GEM
2
- remote: http://rubygems.org/
2
+ remote: https://rubygems.org/
3
3
  specs:
4
4
  addressable (2.3.3)
5
5
  backports (3.1.1)
@@ -72,6 +72,7 @@ PLATFORMS
72
72
  ruby
73
73
 
74
74
  DEPENDENCIES
75
+ event_emitter
75
76
  foreman
76
77
  haml
77
78
  rack
data/sample/config.ru CHANGED
@@ -13,6 +13,7 @@ require File.dirname(__FILE__)+'/main'
13
13
  set :haml, :escape_html => true
14
14
  set :cometio, :timeout => 120
15
15
  set :websocketio, :port => 8080
16
+ set :rocketio, :comet => true, :websocket => true
16
17
 
17
18
  case RUBY_PLATFORM
18
19
  when /linux/i then EM.epoll
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.version = Sinatra::RocketIO::VERSION
8
8
  gem.authors = ["Sho Hashimoto"]
9
9
  gem.email = ["hashimoto@shokai.org"]
10
- gem.description = %q{Node.js like I/O plugin for Sinatra}
10
+ gem.description = %q{Node.js like WebSocket/Comet I/O plugin for Sinatra}
11
11
  gem.summary = gem.description
12
12
  gem.homepage = "https://github.com/shokai/sinatra-rocketio"
13
13
 
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_dependency "rack"
19
19
  gem.add_dependency "sinatra"
20
20
  gem.add_dependency "eventmachine"
21
+ gem.add_dependency "event_emitter"
21
22
  gem.add_dependency "sinatra-contrib"
22
23
  gem.add_dependency "sinatra-cometio"
23
24
  gem.add_dependency "sinatra-websocketio"
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: event_emitter
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: sinatra-contrib
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -107,7 +123,7 @@ dependencies:
107
123
  - - ! '>='
108
124
  - !ruby/object:Gem::Version
109
125
  version: '0'
110
- description: Node.js like I/O plugin for Sinatra
126
+ description: Node.js like WebSocket/Comet I/O plugin for Sinatra
111
127
  email:
112
128
  - hashimoto@shokai.org
113
129
  executables: []
@@ -164,5 +180,5 @@ rubyforge_project:
164
180
  rubygems_version: 1.8.24
165
181
  signing_key:
166
182
  specification_version: 3
167
- summary: Node.js like I/O plugin for Sinatra
183
+ summary: Node.js like WebSocket/Comet I/O plugin for Sinatra
168
184
  test_files: []