sinatra-rocketio 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33e9321349e2c8ad2b3f8dc6bae895fc702a803d
4
+ data.tar.gz: 416b39ec174fe75633467e72a4066c9dccc1f09c
5
+ SHA512:
6
+ metadata.gz: d9cabcb7973308e6325af32e8c7289220c92fa417004a5428c15e5cb825f65115b87c7f4193ed22ee815bebd88e9a68db57311eef8893925529bbacf882d4c2c
7
+ data.tar.gz: d295ace4d4bcfa0431285529a8664b78df9f0380413a50a83b657e2c7d09d121c6ba85156d8a755d453ece7d917687e4dd9234714a88d17195347762d3430fdf
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .DS_Store
2
+ .ruby-version
2
3
  *.tmp
3
4
  *~
4
5
  *#*
@@ -1,3 +1,10 @@
1
+ === 0.2.0 2013-03-31
2
+
3
+ * add "channel" for grouping clients
4
+ * update tests
5
+ * Sinatra::RocketIO.sessions
6
+ * use Sinatra::RocketIO::ClientInfo in server side events
7
+
1
8
  === 0.1.2 2013-03-25
2
9
 
3
10
  * retry get /rocketio/settings
data/README.md CHANGED
@@ -11,15 +11,15 @@ sinatra-rocketio
11
11
  Installation
12
12
  ------------
13
13
 
14
- gem install sinatra-rocketio
14
+ % gem install sinatra-rocketio
15
15
 
16
16
 
17
17
  Requirements
18
18
  ------------
19
19
  * Ruby 1.8.7 or 1.9.2 or 1.9.3 or 2.0.0
20
20
  * Sinatra 1.3.0+
21
- * EventMachine
22
- * jQuery
21
+ * [EventMachine](http://rubyeventmachine.com)
22
+ * [jQuery](http://jquery.com)
23
23
 
24
24
 
25
25
  Usage
@@ -67,7 +67,7 @@ io.on("light", function(data){
67
67
  Client Side
68
68
 
69
69
  ```javascript
70
- io.on("connect", function(session){
70
+ io.on("connect", function(){
71
71
  io.push("chat", {name: "shokai", message: "hello"}); // client -> server
72
72
  });
73
73
  ```
@@ -75,8 +75,8 @@ io.on("connect", function(session){
75
75
  Server Side
76
76
 
77
77
  ```ruby
78
- io.on :chat do |data, session, type|
79
- puts "#{data['name']} : #{data['message']} <#{session}> type:#{type}"
78
+ io.on :chat do |data, client|
79
+ puts "#{data['name']} : #{data['message']} <#{client.session}> type:#{client.type}"
80
80
  end
81
81
  ## => "shokai : hello <12abcde345f6g7h8ijk> type:websocket"
82
82
  ```
@@ -86,21 +86,21 @@ end
86
86
  Client Side
87
87
 
88
88
  ```javascript
89
- io.on("connect", function(session){
90
- alert("connect!!");
89
+ io.on("connect", function(){
90
+ alert("connect!! "+io.session);
91
91
  });
92
92
  ```
93
93
 
94
94
  Server Side
95
95
 
96
96
  ```ruby
97
- io.on :connect do |session, type|
98
- puts "new client <#{session}> type:#{type}"
97
+ io.on :connect do |client|
98
+ puts "new client <#{client.session}> type:#{client.type}"
99
99
  io.push :hello, "hello new client!!"
100
100
  end
101
101
 
102
- io.on :disconnect do |session, type|
103
- puts "client disconnected <#{session}> type:#{type}"
102
+ io.on :disconnect do |client|
103
+ puts "client disconnected <#{client.session}> type:#{client.type}"
104
104
  end
105
105
  ```
106
106
 
@@ -119,8 +119,8 @@ io.on("error", function(err){
119
119
  Server Side
120
120
 
121
121
  ```ruby
122
- event_id = io.on :chat do |data, from, type|
123
- puts "#{data} - from:#{from} type:#{type}"
122
+ event_id = io.on :chat do |data, client|
123
+ puts "chat #{data} - from:#{client.session} type:#{client.type}"
124
124
  end
125
125
  io.removeListener event_id
126
126
  ```
@@ -148,6 +148,40 @@ io.removeListener("error"); // remove all "error" listener
148
148
  ```
149
149
 
150
150
 
151
+ ### Chanel
152
+
153
+ make client groups.
154
+
155
+ Client Side
156
+ ```javascript
157
+ var io = new RocketIO({channel: "ch1"}).connect(); // set channel "ch1"
158
+
159
+ io.on("connect", function(){
160
+ io.push("hi", "haaaaaaaai!!");
161
+ });
162
+
163
+ io.on("announce", function(msg){
164
+ alert(msg);
165
+ // => alert "new client a1b2cde345fg in ch1"
166
+ });
167
+ ```
168
+
169
+ Server Side
170
+ ```ruby
171
+ io = Sinatra::RocketIO
172
+
173
+ io.on :connect do |client|
174
+ msg = "new client #{client.session} in #{client.channel}"
175
+ io.push :announce, msg, :channel => client.channel # to all clients in Channel "ch1"
176
+ end
177
+
178
+ io.on :hi do |msg, client|
179
+ puts "client says #{msg} (channel:#{client.channel})"
180
+ # => "client says haaaaaaaai!! (channel:ch1)"
181
+ end
182
+ ```
183
+
184
+
151
185
  Sample App
152
186
  ----------
153
187
  chat app
@@ -157,6 +191,21 @@ chat app
157
191
  - https://github.com/shokai/rocketio-arduino-sample
158
192
 
159
193
 
194
+ Test
195
+ ----
196
+
197
+ % gem install bundler
198
+ % bundle install
199
+
200
+ start server
201
+
202
+ % rake test_server
203
+
204
+ run test
205
+
206
+ % rake test
207
+
208
+
160
209
  Contributing
161
210
  ------------
162
211
 
data/Rakefile CHANGED
@@ -5,4 +5,10 @@ Rake::TestTask.new do |t|
5
5
  t.pattern = "test/test_*.rb"
6
6
  end
7
7
 
8
+ desc "Start test server"
9
+ task :test_server do
10
+ require File.expand_path 'test/app', File.dirname(__FILE__)
11
+ App.start
12
+ end
13
+
8
14
  task :default => :test
@@ -1,10 +1,14 @@
1
- var RocketIO = function(){
1
+ var RocketIO = function(opts){
2
2
  new EventEmitter().apply(this);
3
3
  this.type = null; // "comet", "websocket"
4
4
  this.session = null;
5
+ this.channel = null;
5
6
  this.io = null;
6
7
  var self = this;
7
8
  var ws_close_timer = null;
9
+ if(typeof opts === "object"){
10
+ this.channel = ""+opts.channel;
11
+ }
8
12
 
9
13
  this.connect = function(){
10
14
  self.io = function(){
@@ -29,10 +33,13 @@ var RocketIO = function(){
29
33
  else if(self.io.url.match(/cometio/)) self.type = "comet";
30
34
  else self.type = "unknown";
31
35
  self.io.on("*", function(event_name, args){
36
+ if(event_name === "connect") event_name = "__connect";
32
37
  self.emit(event_name, args);
33
38
  });
34
- self.io.on("connect", function(session_id){
39
+ self.on("__connect", function(session_id){
35
40
  self.session = session_id;
41
+ io.push("__channel_id", self.channel);
42
+ self.emit("connect");
36
43
  });
37
44
  ws_close_timer = setTimeout(function(){
38
45
  self.close();
@@ -0,0 +1,18 @@
1
+ module Sinatra
2
+ module RocketIO
3
+
4
+ def self.channels
5
+ @@channels ||= {}
6
+ end
7
+
8
+ end
9
+ end
10
+
11
+ Sinatra::RocketIO.on :__channel_id do |channel, client|
12
+ channels[client[:session]] = channel
13
+ client.channel = channel
14
+ emit :connect, client
15
+ end
16
+ Sinatra::RocketIO.on :disconnect do |client, type|
17
+ channels.delete client[:session]
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'hashie'
2
+
3
+ module Sinatra
4
+ module RocketIO
5
+ class ClientInfo < Hashie::Mash
6
+ def to_s
7
+ "session=\"#{session}\" type=\"#{type}\" channel=\"#{channel}\""
8
+ end
9
+ end
10
+ end
11
+ end
@@ -4,13 +4,21 @@ module Sinatra
4
4
  module RocketIO
5
5
 
6
6
  def self.push(type, data, opt={})
7
- if options[:websocket]
8
- if !opt[:to] or Sinatra::WebSocketIO.sessions.include? opt[:to]
7
+ if opt.include? :to and opt[:to].kind_of? Array
8
+ opt[:to].each do |to|
9
+ push type, data, :to => to
10
+ end
11
+ elsif opt.include? :channel
12
+ channels.select{|session, channel|
13
+ channel == opt[:channel].to_s
14
+ }.each do |session|
15
+ push type, data, :to => session
16
+ end
17
+ else
18
+ if options[:websocket]
9
19
  Sinatra::WebSocketIO.push type, data, opt
10
20
  end
11
- end
12
- if options[:comet]
13
- if !opt[:to] or Sinatra::CometIO.sessions.include? opt[:to]
21
+ if options[:comet]
14
22
  Sinatra::CometIO.push type, data, opt
15
23
  end
16
24
  end
@@ -18,8 +26,8 @@ module Sinatra
18
26
 
19
27
  def self.sessions
20
28
  {
21
- :websocket => Sinatra::WebSocketIO.sessions,
22
- :comet => Sinatra::CometIO.sessions
29
+ :websocket => Sinatra::WebSocketIO.sessions.keys,
30
+ :comet => Sinatra::CometIO.sessions.keys
23
31
  }
24
32
  end
25
33
 
@@ -29,19 +37,21 @@ end
29
37
  Sinatra::RocketIO.once :start do
30
38
  if options[:comet]
31
39
  Sinatra::CometIO.on :* do |event_name, *args|
40
+ event_name = :__connect if event_name == :connect
32
41
  if args.size > 1
33
- Sinatra::RocketIO.emit event_name, args[0], args[1], :comet
42
+ Sinatra::RocketIO.emit event_name, args[0], Sinatra::RocketIO::ClientInfo.new(:session => args[1], :channel => Sinatra::RocketIO.channels[args[1]], :type => :comet)
34
43
  else
35
- Sinatra::RocketIO.emit event_name, args[0], :comet
44
+ Sinatra::RocketIO.emit event_name, Sinatra::RocketIO::ClientInfo.new(:session => args[0], :channel => Sinatra::RocketIO.channels[args[0]], :type => :comet)
36
45
  end
37
46
  end
38
47
  end
39
48
  if options[:websocket]
40
49
  Sinatra::WebSocketIO.on :* do |event_name, *args|
50
+ event_name = :__connect if event_name == :connect
41
51
  if args.size > 1
42
- Sinatra::RocketIO.emit event_name, args[0], args[1], :websocket
52
+ Sinatra::RocketIO.emit event_name, args[0], Sinatra::RocketIO::ClientInfo.new(:session => args[1], :channel => Sinatra::RocketIO.channels[args[1]], :type => :websocket)
43
53
  else
44
- Sinatra::RocketIO.emit event_name, args[0], :websocket
54
+ Sinatra::RocketIO.emit event_name, Sinatra::RocketIO::ClientInfo.new(:session => args[0], :channel => Sinatra::RocketIO.channels[args[0]], :type => :websocket)
45
55
  end
46
56
  end
47
57
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RocketIO
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,13 +1,16 @@
1
1
  require 'eventmachine'
2
2
  require 'event_emitter'
3
+ require 'hashie'
3
4
  require 'sinatra/cometio'
4
5
  require 'sinatra/websocketio_nostart'
5
6
  require File.expand_path '../sinatra-rocketio/version', File.dirname(__FILE__)
7
+ require File.expand_path '../sinatra-rocketio/client_info', File.dirname(__FILE__)
6
8
  require File.expand_path '../sinatra-rocketio/helpers', File.dirname(__FILE__)
7
9
  require File.expand_path '../sinatra-rocketio/options', File.dirname(__FILE__)
8
10
  require File.expand_path '../sinatra-rocketio/rocketio', File.dirname(__FILE__)
9
11
  require File.expand_path '../sinatra-rocketio/javascript', File.dirname(__FILE__)
10
12
  require File.expand_path '../sinatra-rocketio/application', File.dirname(__FILE__)
13
+ require File.expand_path '../sinatra-rocketio/channel', File.dirname(__FILE__)
11
14
 
12
15
  module Sinatra
13
16
  module RocketIO
@@ -12,12 +12,13 @@ module Sinatra
12
12
  end
13
13
 
14
14
  include EventEmitter
15
- attr_reader :settings, :type, :io
15
+ attr_reader :settings, :type, :io, :channel
16
16
 
17
17
  public
18
- def initialize(url, opt={:type => :websocket})
18
+ def initialize(url, opt={:type => :websocket, :channel => nil})
19
19
  @url = url
20
20
  @type = opt[:type].to_sym
21
+ @channel = opt[:channel] ? opt[:channel].to_s : nil
21
22
  @io = nil
22
23
  @settings = nil
23
24
  @ws_close_thread = nil
@@ -56,8 +57,13 @@ module Sinatra
56
57
  raise Error, "cannot find #{type} IO #{url}"
57
58
  end
58
59
  @io.on :* do |event_name, *args|
60
+ event_name = :__connect if event_name == :connect
59
61
  this.emit event_name, *args
60
62
  end
63
+ this.on :__connect do
64
+ this.io.push :__channel_id, this.channel
65
+ this.emit :connect
66
+ end
61
67
  if @type == :websocket
62
68
  @ws_close_thread = Thread.new do
63
69
  sleep 3
@@ -8,11 +8,11 @@ name = `whoami`.strip || 'shokai'
8
8
  url = ARGV.shift || 'http://localhost:5000'
9
9
  type = ARGV.shift || :websocket
10
10
 
11
- io = Sinatra::RocketIO::Client.new(url, :type => type).connect
11
+ io = Sinatra::RocketIO::Client.new(url, :type => type, :channel => "1").connect
12
12
  #io = Sinatra::RocketIO::Client.new('http://localhost:5000', :type => :comet).connect
13
13
 
14
- io.on :connect do |session|
15
- puts "#{io.type} connect!! (session_id:#{session})"
14
+ io.on :connect do
15
+ puts "#{io.type} connect!! (session_id:#{io.session})"
16
16
  end
17
17
 
18
18
  io.on :chat do |data|
@@ -6,20 +6,20 @@ class ChatApp < Sinatra::Base
6
6
  puts "RocketIO start!!!"
7
7
  end
8
8
 
9
- io.on :connect do |session, type|
10
- puts "new client <#{session}> (type:#{type})"
11
- push :chat, {:name => "system", :message => "new #{type} client <#{session}>"}
12
- push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
9
+ io.on :connect do |client|
10
+ puts "new client - #{client}"
11
+ push :chat, {:name => "system", :message => "new #{client.type} client <#{client.session}>"}, :channel => client.channel
12
+ push :chat, {:name => "system", :message => "welcome <#{client.session}>"}, :to => client.session
13
13
  end
14
14
 
15
- io.on :disconnect do |session, type|
16
- puts "disconnect client <#{session}> (type:#{type})"
17
- push :chat, {:name => "system", :message => "bye <#{session}>"}
15
+ io.on :disconnect do |client|
16
+ puts "disconnect client - #{client}"
17
+ push :chat, {:name => "system", :message => "bye <#{client.session}>"}, :channel => client.channel
18
18
  end
19
19
 
20
- io.on :chat do |data, from, type|
21
- puts "#{data['name']} : #{data['message']} (from:#{from} type:#{type})"
22
- push :chat, data
20
+ io.on :chat do |data, client|
21
+ puts "#{data['name']} : #{data['message']} - #{client}"
22
+ push :chat, data, :channel => client.channel
23
23
  end
24
24
 
25
25
  io.on :error do |err|
@@ -27,7 +27,12 @@ class ChatApp < Sinatra::Base
27
27
  end
28
28
 
29
29
  get '/' do
30
- haml :index
30
+ redirect '/chat/1'
31
+ end
32
+
33
+ get '/chat/:channel' do
34
+ @channel = params[:channel]
35
+ haml :chat
31
36
  end
32
37
 
33
38
  get '/:source.css' do
@@ -1,4 +1,4 @@
1
- var io = new RocketIO().connect();
1
+ var io = new RocketIO({channel: channel}).connect();
2
2
 
3
3
  $(function(){
4
4
  $("#chat #btn_send").click(post);
@@ -12,12 +12,12 @@ io.on("chat", function(data){
12
12
  $("#chat #timeline").prepend(m);
13
13
  });
14
14
 
15
- io.on("connect", function(session){
16
- console.log("connect!! "+session);
15
+ io.on("connect", function(){
16
+ console.log("connect!! "+io.session);
17
17
  $("#type").text("type : "+io.type);
18
18
  });
19
19
 
20
- io.on("disconnect", function(session){
20
+ io.on("disconnect", function(){
21
21
  console.log("disconnect!!");
22
22
  });
23
23
 
@@ -4,14 +4,16 @@
4
4
  %meta{'http-equiv' => 'Content-Type', :content => 'text/html', :charset => 'UTF-8'}
5
5
  %title sinatra-rocketio chat
6
6
  %link{:rel => 'stylesheet', :href => "/main.css", :type => 'text/css'}
7
+ :javascript
8
+ var channel = "#{@channel}";
7
9
  %script{:type => 'text/javascript', :src => "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"}
8
10
  %script{:type => 'text/javascript', :src => rocketio_js}
9
- %script{:type => 'text/javascript', :src => "/js/index.js"}
11
+ %script{:type => 'text/javascript', :src => "/js/chat.js"}
10
12
 
11
13
  %body
12
14
  %div#content
13
15
  %div#header
14
- %h1 rocketio chat
16
+ %h1 rocketio chat [ch:#{@channel}]
15
17
  %span#type
16
18
  %div#main
17
19
  %div#chat
@@ -25,9 +25,10 @@ Gem::Specification.new do |gem|
25
25
  gem.add_development_dependency "httparty"
26
26
  gem.add_development_dependency "json"
27
27
 
28
- gem.add_dependency "sinatra-cometio", ">= 0.4.0"
29
- gem.add_dependency "sinatra-websocketio", ">= 0.2.0"
28
+ gem.add_dependency "sinatra-cometio", ">= 0.4.3"
29
+ gem.add_dependency "sinatra-websocketio", ">= 0.2.2"
30
30
  gem.add_dependency "sinatra"
31
31
  gem.add_dependency "eventmachine", ">= 1.0.0"
32
32
  gem.add_dependency "event_emitter", ">= 0.2.4"
33
+ gem.add_dependency "hashie"
33
34
  end
@@ -1,5 +1,3 @@
1
- require 'httparty'
2
-
3
1
  class App
4
2
 
5
3
  def self.running
@@ -26,48 +24,15 @@ class App
26
24
  "http://localhost:#{port}/cometio/io"
27
25
  end
28
26
 
29
- def self.pid_file
30
- ENV['PID_FILE'] || "/tmp/sinatra-rocketio-testapp.pid"
31
- end
32
-
33
27
  def self.app_dir
34
28
  File.expand_path 'app', File.dirname(__FILE__)
35
29
  end
36
30
 
37
- def self.pid
38
- return unless @running
39
- File.open(pid_file) do |f|
40
- pid = f.gets.strip.to_i
41
- return pid if pid > 0
42
- end
43
- return
44
- end
45
-
46
31
  def self.start
47
- return if @running
48
- File.delete pid_file if File.exists? pid_file
49
- Thread.new do
50
- IO::popen "cd #{app_dir} && PID_FILE=#{pid_file} WS_PORT=#{ws_port} rackup config.ru -p #{port} > /dev/null 2>&1"
51
- end
32
+ return if running
52
33
  @running = true
53
- 100.times do
54
- if File.exists? pid_file
55
- sleep 2
56
- return true
57
- end
58
- sleep 0.1
59
- end
60
- @running = false
61
- return false
62
- end
63
-
64
- def self.stop
65
- return unless @running
66
- if File.exists? pid_file
67
- system "kill -9 #{pid}"
68
- File.delete pid_file
69
- end
70
- @running = false
34
+ cmd = "cd #{app_dir} && WS_PORT=#{ws_port} rackup config.ru -p #{port}"
35
+ system cmd
71
36
  end
72
37
 
73
38
  end
@@ -1,13 +1,3 @@
1
- pid_file = ENV['PID_FILE'] || "/tmp/sinatra-rocketio-test-pid"
2
- EM::defer do
3
- while !EM::reactor_running? do
4
- sleep 0.1
5
- end
6
- File.open(pid_file, "w+") do |f|
7
- f.write Process.pid.to_s
8
- end
9
- end
10
-
11
1
  class TestApp < Sinatra::Base
12
2
  register Sinatra::RocketIO
13
3
  io = Sinatra::RocketIO
@@ -16,23 +6,28 @@ class TestApp < Sinatra::Base
16
6
  "sinatra-rocketio v#{Sinatra::RocketIO::VERSION}"
17
7
  end
18
8
 
19
- io.on :connect do |session, type|
20
- puts "new client <session:#{session}> <type:#{type}>"
9
+ io.on :connect do |client|
10
+ puts "new client <session:#{client.session}> <type:#{client.type}>"
21
11
  end
22
12
 
23
- io.on :disconnect do |session, type|
24
- puts "disconnect client <session:#{session}> <type:#{type}>"
13
+ io.on :disconnect do |client|
14
+ puts "disconnect client <session:#{client.session}> <type:#{client.type}>"
25
15
  end
26
16
 
27
- io.on :broadcast do |data, from, type|
17
+ io.on :broadcast do |data, client|
28
18
  puts from
29
- puts "broadcast <session:#{from}> <type:#{type}> - #{data.to_json}"
19
+ puts "broadcast <session:#{client.session}> <type:#{client.type}> - #{data.to_json}"
30
20
  push :broadcast, data
31
21
  end
32
22
 
33
- io.on :message do |data, from, type|
34
- puts "message <session:#{from}> <type:#{type}> - #{data.to_json}"
23
+ io.on :message do |data, client|
24
+ puts "message <session:#{client.session}> <type:#{client.type}> - #{data.to_json}"
35
25
  push :message, data, :to => data['to']
36
26
  end
37
27
 
28
+ io.on :to_channel do |data, client|
29
+ puts "message to channel:#{client.channel} <type:#{client.type}> - #{data.to_json}"
30
+ push :to_channel, data, :channel => client.channel
31
+ end
32
+
38
33
  end
@@ -0,0 +1,49 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestChannel < MiniTest::Unit::TestCase
4
+
5
+ def test_channel
6
+ ## client1(comet, channel "aaa") --> server --> client3(websocket, channel "aaa")
7
+ ## client2(websocket, channel "bbb") not receive data from channel "aaa"
8
+ post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
9
+ res = nil
10
+ res2 = nil
11
+ res3 = nil
12
+ client = Sinatra::RocketIO::Client.new(App.url, :type => :comet, :channel => "aaa").connect
13
+ client.on :to_channel do |data|
14
+ res = data
15
+ end
16
+
17
+ client.on :connect do
18
+ client2 = Sinatra::RocketIO::Client.new(App.url, :type => :websocket, :channel => "bbb").connect
19
+ client2.on :connect do
20
+ client3 = Sinatra::RocketIO::Client.new(App.url, :type => :websocket, :channel => "aaa").connect
21
+ client3.on :connect do
22
+ client.push :to_channel, post_data
23
+ end
24
+ client3.on :to_channel do |data|
25
+ res3 = data
26
+ client3.close
27
+ end
28
+ end
29
+ client2.on :to_channel do |data|
30
+ res2 = data
31
+ client2.close
32
+ end
33
+ end
34
+
35
+ 50.times do
36
+ break if res != nil and res3 != nil
37
+ sleep 0.1
38
+ end
39
+ client.close
40
+ assert res != nil, 'comet server not respond'
41
+ assert res["time"] == post_data[:time]
42
+ assert res["msg"] == post_data[:msg]
43
+ assert res3 != nil, 'websocket server not respond'
44
+ assert res3["time"] == post_data[:time]
45
+ assert res3["msg"] == post_data[:msg]
46
+ assert res2 == nil, 'channel error'
47
+ end
48
+
49
+ end
@@ -6,10 +6,3 @@ require 'sinatra/rocketio/client'
6
6
  require 'httparty'
7
7
  require 'json'
8
8
  require File.expand_path 'app', File.dirname(__FILE__)
9
-
10
-
11
- ['SIGHUP', 'SIGINT', 'SIGKILL', 'SIGTERM'].each do |sig|
12
- Kernel.trap sig do
13
- App.stop
14
- end
15
- end
@@ -0,0 +1,49 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestPushMultiClient < MiniTest::Unit::TestCase
4
+
5
+ def test_push_multi
6
+ ## client1(comet) --> server --> client2(comet) & client3(websocket)
7
+ post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
8
+ res = nil
9
+ res2 = nil
10
+ res3 = nil
11
+ client = Sinatra::RocketIO::Client.new(App.url, :type => :comet).connect
12
+ client.on :message do |data|
13
+ res = data
14
+ end
15
+
16
+ client.on :connect do
17
+ client2 = Sinatra::RocketIO::Client.new(App.url, :type => :comet).connect
18
+ client2.on :connect do
19
+ client3 = Sinatra::RocketIO::Client.new(App.url, :type => :websocket).connect
20
+ client3.on :connect do
21
+ post_data[:to] = [client2.session, client3.session]
22
+ client.push :message, post_data
23
+ end
24
+ client3.on :message do |data|
25
+ res3 = data
26
+ client3.close
27
+ end
28
+ end
29
+ client2.on :message do |data|
30
+ res2 = data
31
+ client2.close
32
+ end
33
+ end
34
+
35
+ 50.times do
36
+ break if res2 != nil and res3 != nil
37
+ sleep 0.1
38
+ end
39
+ client.close
40
+ assert res2 != nil, 'server not respond'
41
+ assert res2["time"] == post_data[:time]
42
+ assert res2["msg"] == post_data[:msg]
43
+ assert res3 != nil, 'server not respond'
44
+ assert res3["time"] == post_data[:time]
45
+ assert res3["msg"] == post_data[:msg]
46
+ assert res == nil
47
+ end
48
+
49
+ end
@@ -2,14 +2,6 @@ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
2
 
3
3
  class TestRocketIO < MiniTest::Unit::TestCase
4
4
 
5
- def setup
6
- App.start
7
- end
8
-
9
- def teardown
10
- App.stop
11
- end
12
-
13
5
  def test_websocket_to_comet
14
6
  ## websocket --> server --> comet
15
7
  post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
@@ -20,10 +12,10 @@ class TestRocketIO < MiniTest::Unit::TestCase
20
12
  res = data
21
13
  end
22
14
 
23
- client.on :connect do |session|
15
+ client.on :connect do
24
16
  client2 = Sinatra::RocketIO::Client.new(App.url, :type => :comet).connect
25
- client2.on :connect do |session2|
26
- post_data['to'] = session2
17
+ client2.on :connect do
18
+ post_data['to'] = client2.session
27
19
  client.push :message, post_data
28
20
  end
29
21
  client2.on :message do |data|
@@ -34,7 +26,7 @@ class TestRocketIO < MiniTest::Unit::TestCase
34
26
  end
35
27
 
36
28
  50.times do
37
- break if res != nil
29
+ break if res2 != nil
38
30
  sleep 0.1
39
31
  end
40
32
  client.close
@@ -54,10 +46,10 @@ class TestRocketIO < MiniTest::Unit::TestCase
54
46
  res = data
55
47
  end
56
48
 
57
- client.on :connect do |session|
49
+ client.on :connect do
58
50
  client2 = Sinatra::RocketIO::Client.new(App.url, :type => :websocket).connect
59
- client2.on :connect do |session2|
60
- post_data['to'] = session2
51
+ client2.on :connect do
52
+ post_data['to'] = client2.session
61
53
  client.push :message, post_data
62
54
  end
63
55
  client2.on :message do |data|
@@ -68,7 +60,7 @@ class TestRocketIO < MiniTest::Unit::TestCase
68
60
  end
69
61
 
70
62
  50.times do
71
- break if res != nil
63
+ break if res2 != nil
72
64
  sleep 0.1
73
65
  end
74
66
  client.close
@@ -2,14 +2,6 @@ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
2
 
3
3
  class TestSettingsIO < MiniTest::Unit::TestCase
4
4
 
5
- def setup
6
- App.start
7
- end
8
-
9
- def teardown
10
- App.stop
11
- end
12
-
13
5
  def test_settings
14
6
  res = JSON.parse HTTParty.get("#{App.url}/rocketio/settings").body
15
7
  assert App.websocketio_url == res['websocket']
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rocketio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sho Hashimoto
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-24 00:00:00.000000000 Z
11
+ date: 2013-03-31 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,195 +27,185 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: minitest
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: thin
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: haml
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '>='
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '>='
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: sass
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - '>='
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - '>='
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: httparty
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - '>='
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - '>='
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: json
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - '>='
132
116
  - !ruby/object:Gem::Version
133
117
  version: '0'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - '>='
140
123
  - !ruby/object:Gem::Version
141
124
  version: '0'
142
125
  - !ruby/object:Gem::Dependency
143
126
  name: sinatra-cometio
144
127
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
128
  requirements:
147
- - - ! '>='
129
+ - - '>='
148
130
  - !ruby/object:Gem::Version
149
- version: 0.4.0
131
+ version: 0.4.3
150
132
  type: :runtime
151
133
  prerelease: false
152
134
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
135
  requirements:
155
- - - ! '>='
136
+ - - '>='
156
137
  - !ruby/object:Gem::Version
157
- version: 0.4.0
138
+ version: 0.4.3
158
139
  - !ruby/object:Gem::Dependency
159
140
  name: sinatra-websocketio
160
141
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
142
  requirements:
163
- - - ! '>='
143
+ - - '>='
164
144
  - !ruby/object:Gem::Version
165
- version: 0.2.0
145
+ version: 0.2.2
166
146
  type: :runtime
167
147
  prerelease: false
168
148
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
149
  requirements:
171
- - - ! '>='
150
+ - - '>='
172
151
  - !ruby/object:Gem::Version
173
- version: 0.2.0
152
+ version: 0.2.2
174
153
  - !ruby/object:Gem::Dependency
175
154
  name: sinatra
176
155
  requirement: !ruby/object:Gem::Requirement
177
- none: false
178
156
  requirements:
179
- - - ! '>='
157
+ - - '>='
180
158
  - !ruby/object:Gem::Version
181
159
  version: '0'
182
160
  type: :runtime
183
161
  prerelease: false
184
162
  version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
163
  requirements:
187
- - - ! '>='
164
+ - - '>='
188
165
  - !ruby/object:Gem::Version
189
166
  version: '0'
190
167
  - !ruby/object:Gem::Dependency
191
168
  name: eventmachine
192
169
  requirement: !ruby/object:Gem::Requirement
193
- none: false
194
170
  requirements:
195
- - - ! '>='
171
+ - - '>='
196
172
  - !ruby/object:Gem::Version
197
173
  version: 1.0.0
198
174
  type: :runtime
199
175
  prerelease: false
200
176
  version_requirements: !ruby/object:Gem::Requirement
201
- none: false
202
177
  requirements:
203
- - - ! '>='
178
+ - - '>='
204
179
  - !ruby/object:Gem::Version
205
180
  version: 1.0.0
206
181
  - !ruby/object:Gem::Dependency
207
182
  name: event_emitter
208
183
  requirement: !ruby/object:Gem::Requirement
209
- none: false
210
184
  requirements:
211
- - - ! '>='
185
+ - - '>='
212
186
  - !ruby/object:Gem::Version
213
187
  version: 0.2.4
214
188
  type: :runtime
215
189
  prerelease: false
216
190
  version_requirements: !ruby/object:Gem::Requirement
217
- none: false
218
191
  requirements:
219
- - - ! '>='
192
+ - - '>='
220
193
  - !ruby/object:Gem::Version
221
194
  version: 0.2.4
195
+ - !ruby/object:Gem::Dependency
196
+ name: hashie
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
222
209
  description: WebSocket/Comet IO plugin for Sinatra
223
210
  email:
224
211
  - hashimoto@shokai.org
@@ -236,6 +223,8 @@ files:
236
223
  - lib/js/rocketio.js
237
224
  - lib/sinatra-rocketio.rb
238
225
  - lib/sinatra-rocketio/application.rb
226
+ - lib/sinatra-rocketio/channel.rb
227
+ - lib/sinatra-rocketio/client_info.rb
239
228
  - lib/sinatra-rocketio/helpers.rb
240
229
  - lib/sinatra-rocketio/javascript.rb
241
230
  - lib/sinatra-rocketio/options.rb
@@ -247,50 +236,47 @@ files:
247
236
  - sample/bin/cui_chat_client.rb
248
237
  - sample/config.ru
249
238
  - sample/main.rb
250
- - sample/public/js/index.js
251
- - sample/views/index.haml
239
+ - sample/public/js/chat.js
240
+ - sample/views/chat.haml
252
241
  - sample/views/main.scss
253
242
  - sinatra-rocketio.gemspec
254
243
  - test/app.rb
255
244
  - test/app/config.ru
256
245
  - test/app/main.rb
246
+ - test/test_channel.rb
257
247
  - test/test_helper.rb
248
+ - test/test_push_multi_client.rb
258
249
  - test/test_rocketio.rb
259
250
  - test/test_settings.rb
260
251
  homepage: https://github.com/shokai/sinatra-rocketio
261
252
  licenses: []
253
+ metadata: {}
262
254
  post_install_message:
263
255
  rdoc_options: []
264
256
  require_paths:
265
257
  - lib
266
258
  required_ruby_version: !ruby/object:Gem::Requirement
267
- none: false
268
259
  requirements:
269
- - - ! '>='
260
+ - - '>='
270
261
  - !ruby/object:Gem::Version
271
262
  version: '0'
272
- segments:
273
- - 0
274
- hash: 1593017263457838199
275
263
  required_rubygems_version: !ruby/object:Gem::Requirement
276
- none: false
277
264
  requirements:
278
- - - ! '>='
265
+ - - '>='
279
266
  - !ruby/object:Gem::Version
280
267
  version: '0'
281
- segments:
282
- - 0
283
- hash: 1593017263457838199
284
268
  requirements: []
285
269
  rubyforge_project:
286
- rubygems_version: 1.8.24
270
+ rubygems_version: 2.0.3
287
271
  signing_key:
288
- specification_version: 3
272
+ specification_version: 4
289
273
  summary: WebSocket/Comet IO plugin for Sinatra
290
274
  test_files:
291
275
  - test/app.rb
292
276
  - test/app/config.ru
293
277
  - test/app/main.rb
278
+ - test/test_channel.rb
294
279
  - test/test_helper.rb
280
+ - test/test_push_multi_client.rb
295
281
  - test/test_rocketio.rb
296
282
  - test/test_settings.rb