sinatra-rocketio 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.1.1 2013-03-25
2
+
3
+ * ruby client - use comet if websocket port close
4
+ * fix sample cui_chat_client
5
+
1
6
  === 0.1.0 2013-03-24
2
7
 
3
8
  * add ruby client library - Sinatra::RocketIO::Client
data/lib/js/rocketio.js CHANGED
@@ -21,7 +21,7 @@ var RocketIO = function(){
21
21
  }();
22
22
  if(typeof self.io === "undefined"){
23
23
  setTimeout(function(){
24
- self.emit("error", "WebSocketIO and CometIO are not available")
24
+ self.emit("error", "WebSocketIO and CometIO are not available");
25
25
  }, 100);
26
26
  return self;
27
27
  };
@@ -39,13 +39,13 @@ var RocketIO = function(){
39
39
  self.type = "comet";
40
40
  self.connect();
41
41
  }, 3000);
42
+ self.once("connect", function(){
43
+ if(ws_close_timer) clearTimeout(ws_close_timer);
44
+ ws_close_timer = null;
45
+ });
42
46
  return self;
43
47
  };
44
48
 
45
- self.on("connect", function(){
46
- clearTimeout(ws_close_timer);
47
- });
48
-
49
49
  this.close = function(){
50
50
  self.io.close();
51
51
  };
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RocketIO
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -15,36 +15,44 @@ module Sinatra
15
15
  attr_reader :settings, :type, :io
16
16
 
17
17
  def initialize(url, opt={:type => :websocket})
18
+ @type = opt[:type].to_sym
19
+ @io = nil
18
20
  @settings = JSON.parse HTTParty.get("#{url}/rocketio/settings").body
19
- type = opt[:type].to_sym
21
+ @ws_close_thread = nil
22
+ self
23
+ end
20
24
 
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
+ def connect
26
+ this = self
27
+ if @type == :websocket and @settings.include? 'websocket'
28
+ @io = Sinatra::WebSocketIO::Client.new(@settings['websocket']).connect
29
+ elsif type == :comet or @settings.include? 'comet'
30
+ @io = Sinatra::CometIO::Client.new(@settings['comet']).connect
25
31
  @type = :comet
26
- @io = Sinatra::CometIO::Client.new @settings['comet']
27
32
  else
28
33
  raise Error, "cannot find #{type} IO #{url}"
29
34
  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]
35
+ @io.on :* do |event_name, *args|
36
+ this.emit event_name, *args
37
+ end
38
+ if @type == :websocket
39
+ @ws_close_thread = Thread.new do
40
+ sleep 3
41
+ Thread.new do
42
+ close
37
43
  end
44
+ emit :error, "websocket port is not open"
45
+ @type = :comet
46
+ connect
47
+ end
48
+ once :connect do
49
+ Thread.kill @ws_close_thread if @ws_close_thread
50
+ @ws_close_thread = nil
38
51
  end
39
52
  end
40
53
  self
41
54
  end
42
55
 
43
- def connect
44
- @io.connect
45
- self
46
- end
47
-
48
56
  def close
49
57
  @io.close
50
58
  end
data/sample/README.md CHANGED
@@ -20,3 +20,11 @@ Run
20
20
  % rackup config.ru -p 5000
21
21
 
22
22
  => http://localhost:5000
23
+
24
+
25
+ Ruby Client
26
+ -----------
27
+
28
+ % ruby bin/cui_chat_client.rb
29
+ % ruby bin/cui_chat_client.rb http://localhost:5000 websocket
30
+ % ruby bin/cui_chat_client.rb http://localhost:5000 comet
@@ -5,8 +5,10 @@ $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
5
5
  require 'sinatra/rocketio/client'
6
6
 
7
7
  name = `whoami`.strip || 'shokai'
8
+ url = ARGV.shift || 'http://localhost:5000'
9
+ type = ARGV.shift || :websocket
8
10
 
9
- io = Sinatra::RocketIO::Client.new('http://localhost:5000').connect
11
+ io = Sinatra::RocketIO::Client.new(url, :type => type).connect
10
12
  #io = Sinatra::RocketIO::Client.new('http://localhost:5000', :type => :comet).connect
11
13
 
12
14
  io.on :connect do |session|
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.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-23 00:00:00.000000000 Z
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -271,7 +271,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
271
271
  version: '0'
272
272
  segments:
273
273
  - 0
274
- hash: 4356497738858812236
274
+ hash: -1955004754093144162
275
275
  required_rubygems_version: !ruby/object:Gem::Requirement
276
276
  none: false
277
277
  requirements:
@@ -280,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  version: '0'
281
281
  segments:
282
282
  - 0
283
- hash: 4356497738858812236
283
+ hash: -1955004754093144162
284
284
  requirements: []
285
285
  rubyforge_project:
286
286
  rubygems_version: 1.8.24