sinatra-websocketio 0.1.7 → 0.2.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-websocketio.gemspec
4
4
  gemspec
5
- gem 'minitest'
6
- gem 'thin'
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.2.0 2013-03-23
2
+
3
+ * add Sinatra::WebSocketIO::Client
4
+ * use gem.add_development_dependency
5
+ * delete sample/Gemfile
6
+
1
7
  === 0.1.7 2013-03-21
2
8
 
3
9
  * emit "error" on push before websocket connect
@@ -0,0 +1,97 @@
1
+ require File.expand_path '../../sinatra-websocketio/version', File.dirname(__FILE__)
2
+ require 'websocket-client-simple'
3
+ require 'event_emitter'
4
+ require 'json'
5
+ require 'timeout'
6
+
7
+ module Sinatra
8
+ module WebSocketIO
9
+ class Client
10
+ class Error < StandardError
11
+ end
12
+
13
+ include EventEmitter
14
+ attr_reader :url, :session
15
+ attr_accessor :running, :connecting, :timeout
16
+
17
+ def initialize(url)
18
+ @url = url
19
+ @session = nil
20
+ @websocket = nil
21
+ @connecting = false
22
+ @running = false
23
+ @timeout = 5
24
+
25
+ on :__session_id do |session_id|
26
+ @session = session_id
27
+ emit :connect, @session
28
+ end
29
+ end
30
+
31
+ def connect
32
+ return self if connecting
33
+ this = self
34
+ @running = true
35
+ url = @session ? "#{@url}/session=#{@session}" : @url
36
+ @websocket = nil
37
+ begin
38
+ @websocket = WebSocket::Client::Simple::Client.new url
39
+ rescue StandardError, Timeout::Error => e
40
+ Thread.new do
41
+ sleep 5
42
+ connect
43
+ end
44
+ end
45
+ return self unless @websocket
46
+
47
+ @websocket.on :message do |msg|
48
+ begin
49
+ data = JSON.parse msg.data
50
+ this.emit data['type'], data['data']
51
+ rescue => e
52
+ this.emit :error, e
53
+ end
54
+ end
55
+
56
+ @websocket.on :close do |e|
57
+ if this.connecting
58
+ this.connecting = false
59
+ this.emit :disconnect, e
60
+ end
61
+ if this.running
62
+ Thread.new do
63
+ sleep 5
64
+ this.connect
65
+ end
66
+ end
67
+ end
68
+
69
+ @websocket.on :open do
70
+ this.connecting = true
71
+ end
72
+
73
+ return self
74
+ end
75
+
76
+ def close
77
+ @running = false
78
+ @websocket.close
79
+ end
80
+
81
+ def push(type, data)
82
+ if !@connecting
83
+ emit :error, 'websocket not connecting'
84
+ return
85
+ end
86
+ begin
87
+ Timeout::timeout @timeout do
88
+ @websocket.send({:type => type, :data => data, :session => @session}.to_json)
89
+ end
90
+ rescue Timeout::Error, StandardError => e
91
+ emit :error, e
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module WebSocketIO
3
- VERSION = "0.1.7"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -6,7 +6,6 @@ module Sinatra
6
6
  @@running
7
7
  end
8
8
 
9
-
10
9
  def self.start
11
10
  return if running?
12
11
  @@running = true
data/sample/README.md CHANGED
@@ -8,13 +8,13 @@ WebSocket 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
 
15
15
  Run
16
16
  ---
17
17
 
18
- % foreman start
18
+ % rackup config.ru -p 5000
19
19
 
20
20
  => http://localhost:5000
@@ -3,36 +3,30 @@ require 'rubygems'
3
3
  require 'bundler/setup'
4
4
  $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
5
5
  require 'eventmachine'
6
- require 'em-websocketio-client'
6
+ require 'sinatra/websocketio/client'
7
7
 
8
8
  name = `whoami`.strip || 'shokai'
9
9
 
10
- EM::run do
11
- client = EM::WebSocketIO::Client.new('ws://localhost:8080').connect
10
+ client = Sinatra::WebSocketIO::Client.new('ws://localhost:8080').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
+ client.on :connect do |session|
13
+ puts "connect!! (session_id:#{session})"
14
+ end
24
15
 
25
- client.on :disconnect do
26
- puts "disconnected!!"
27
- end
16
+ client.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
+ client.on :error do |err|
21
+ STDERR.puts err
36
22
  end
37
23
 
24
+ client.on :disconnect do
25
+ puts "disconnected!!"
26
+ end
38
27
 
28
+ loop do
29
+ line = STDIN.gets.strip
30
+ next if line.empty?
31
+ client.push :chat, {:message => line, :name => name}
32
+ end
data/sample/config.ru CHANGED
@@ -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
@@ -15,11 +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
+
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
+ gem.add_development_dependency 'rack'
24
+
18
25
  gem.add_dependency 'sinatra-contrib', '>= 1.3.2'
19
26
  gem.add_dependency 'sinatra', '>= 1.3.0'
20
- gem.add_dependency 'em-websocket-client', '>= 0.1.1'
21
27
  gem.add_dependency 'em-websocket', '>= 0.5.0'
22
28
  gem.add_dependency 'eventmachine', '>= 1.0.0'
23
29
  gem.add_dependency 'json', '>= 1.7.0'
24
30
  gem.add_dependency 'event_emitter', '>= 0.2.4'
31
+ gem.add_dependency 'websocket-client-simple'
25
32
  end
data/test/test_helper.rb CHANGED
@@ -2,7 +2,7 @@ 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-websocketio-client'
5
+ require 'sinatra/websocketio/client'
6
6
  require File.expand_path 'app', File.dirname(__FILE__)
7
7
 
8
8
 
@@ -13,30 +13,25 @@ class TestWebSocketIO < MiniTest::Unit::TestCase
13
13
  def test_simple
14
14
  res = nil
15
15
  post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
16
- EM::run do
17
- client = EM::WebSocketIO::Client.new(App.websocketio_url).connect
16
+ client = Sinatra::WebSocketIO::Client.new(App.websocketio_url).connect
18
17
 
19
- client.on :broadcast do |data|
20
- res = data
21
- client.close
22
- end
18
+ client.on :broadcast do |data|
19
+ res = data
20
+ client.close
21
+ end
23
22
 
24
- client.on :disconnect do
25
- EM.stop_event_loop
26
- end
23
+ client.on :disconnect do
24
+ end
27
25
 
28
- client.on :connect do |session|
29
- push :broadcast, post_data
30
- end
26
+ client.on :connect do |session|
27
+ push :broadcast, post_data
28
+ end
31
29
 
32
- EM::defer do
33
- 50.times do
34
- break if res != nil
35
- sleep 0.1
36
- end
37
- client.close
38
- end
30
+ 50.times do
31
+ break if res != nil
32
+ sleep 0.1
39
33
  end
34
+ client.close
40
35
  assert res != nil, 'server not respond'
41
36
  assert res["time"] == post_data[:time]
42
37
  assert res["msg"] == post_data[:msg]
@@ -47,37 +42,29 @@ class TestWebSocketIO < MiniTest::Unit::TestCase
47
42
  post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
48
43
  res = nil
49
44
  res2 = nil
50
- EM::run do
51
- client = EM::WebSocketIO::Client.new(App.websocketio_url).connect
52
- client.on :message do |data|
53
- res = data
54
- end
55
-
56
- client.on :disconnect do
57
- EM.stop_event_loop
58
- end
45
+ client = Sinatra::WebSocketIO::Client.new(App.websocketio_url).connect
46
+ client.on :message do |data|
47
+ res = data
48
+ end
59
49
 
60
- client.on :connect do |session|
61
- client2 = EM::WebSocketIO::Client.new(App.websocketio_url).connect
62
- client2.on :connect do |session2|
63
- post_data['to'] = session2
64
- client.push :message, post_data
65
- end
66
- client2.on :message do |data|
67
- res2 = data
68
- client2.close
69
- client.close
70
- end
50
+ client.on :connect do |session|
51
+ client2 = Sinatra::WebSocketIO::Client.new(App.websocketio_url).connect
52
+ client2.on :connect do |session2|
53
+ post_data['to'] = session2
54
+ client.push :message, post_data
71
55
  end
72
-
73
- EM::defer do
74
- 50.times do
75
- break if res != nil
76
- sleep 0.1
77
- end
56
+ client2.on :message do |data|
57
+ res2 = data
58
+ client2.close
78
59
  client.close
79
60
  end
80
61
  end
62
+
63
+ 50.times do
64
+ break if res != nil
65
+ sleep 0.1
66
+ end
67
+ client.close
81
68
  assert res2 != nil, 'server not respond'
82
69
  assert res2["time"] == post_data[:time]
83
70
  assert res2["msg"] == post_data[:msg]
@@ -91,35 +78,27 @@ class TestWebSocketIO < MiniTest::Unit::TestCase
91
78
  res = nil
92
79
  res2 = nil
93
80
 
94
- EM::run do
95
- client = EM::WebSocketIO::Client.new(App.websocketio_url).connect
96
- client.on :broadcast do |data|
97
- res = data
98
- end
81
+ client = Sinatra::WebSocketIO::Client.new(App.websocketio_url).connect
82
+ client.on :broadcast do |data|
83
+ res = data
84
+ end
99
85
 
100
- client.on :disconnect do
101
- EM.stop_event_loop
86
+ client.on :connect do |session|
87
+ client2 = Sinatra::WebSocketIO::Client.new(App.websocketio_url).connect
88
+ client2.on :connect do |session2|
89
+ client.push :broadcast, post_data
102
90
  end
103
-
104
- client.on :connect do |session|
105
- client2 = EM::WebSocketIO::Client.new(App.websocketio_url).connect
106
- client2.on :connect do |session2|
107
- client.push :broadcast, post_data
108
- end
109
- client2.on :broadcast do |data|
110
- res2 = data
111
- client2.close
112
- end
91
+ client2.on :broadcast do |data|
92
+ res2 = data
93
+ client2.close
113
94
  end
95
+ end
114
96
 
115
- EM::defer do
116
- 50.times do
117
- break if res != nil and res2 != nil
118
- sleep 0.1
119
- end
120
- client.close
121
- end
97
+ 50.times do
98
+ break if res != nil and res2 != nil
99
+ sleep 0.1
122
100
  end
101
+ client.close
123
102
 
124
103
  assert res != nil, 'server not respond'
125
104
  assert res["time"] == post_data[:time]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-websocketio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,32 +9,96 @@ 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
14
  - !ruby/object:Gem::Dependency
15
- name: sinatra-contrib
15
+ name: minitest
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.3.2
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.3.2
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: sinatra
31
+ name: thin
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 1.3.0
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'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack
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: sinatra-contrib
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.2
38
102
  type: :runtime
39
103
  prerelease: false
40
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,15 +106,15 @@ dependencies:
42
106
  requirements:
43
107
  - - ! '>='
44
108
  - !ruby/object:Gem::Version
45
- version: 1.3.0
109
+ version: 1.3.2
46
110
  - !ruby/object:Gem::Dependency
47
- name: em-websocket-client
111
+ name: sinatra
48
112
  requirement: !ruby/object:Gem::Requirement
49
113
  none: false
50
114
  requirements:
51
115
  - - ! '>='
52
116
  - !ruby/object:Gem::Version
53
- version: 0.1.1
117
+ version: 1.3.0
54
118
  type: :runtime
55
119
  prerelease: false
56
120
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +122,7 @@ dependencies:
58
122
  requirements:
59
123
  - - ! '>='
60
124
  - !ruby/object:Gem::Version
61
- version: 0.1.1
125
+ version: 1.3.0
62
126
  - !ruby/object:Gem::Dependency
63
127
  name: em-websocket
64
128
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +187,22 @@ dependencies:
123
187
  - - ! '>='
124
188
  - !ruby/object:Gem::Version
125
189
  version: 0.2.4
190
+ - !ruby/object:Gem::Dependency
191
+ name: websocket-client-simple
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :runtime
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
126
206
  description: WebSocket component for Sinatra RocketIO
127
207
  email:
128
208
  - hashimoto@shokai.org
@@ -136,8 +216,6 @@ files:
136
216
  - LICENSE.txt
137
217
  - README.md
138
218
  - Rakefile
139
- - lib/em-websocketio-client.rb
140
- - lib/em-websocketio-client/client.rb
141
219
  - lib/js/event_emitter.js
142
220
  - lib/js/websocketio.js
143
221
  - lib/sinatra-websocketio.rb
@@ -148,10 +226,8 @@ files:
148
226
  - lib/sinatra-websocketio/version.rb
149
227
  - lib/sinatra-websocketio/websocketio.rb
150
228
  - lib/sinatra/websocketio.rb
229
+ - lib/sinatra/websocketio/client.rb
151
230
  - lib/sinatra/websocketio_nostart.rb
152
- - sample/Gemfile
153
- - sample/Gemfile.lock
154
- - sample/Procfile
155
231
  - sample/README.md
156
232
  - sample/bin/cui_chat_client.rb
157
233
  - sample/config.ru
@@ -1,62 +0,0 @@
1
- module EventMachine
2
- module WebSocketIO
3
- class Client
4
- include EventEmitter
5
- attr_reader :url, :session, :running
6
- attr_accessor :timeout
7
-
8
- def initialize(url)
9
- raise ArgumentError, "invalid websocket URL \"#{url}\"" unless url =~ /^ws:\/\/.+/
10
- @url = url
11
- @running = false
12
- @__reconnect_timer_id = nil
13
- @ws = nil
14
- self.on :__session_id do |session_id|
15
- @session = session_id
16
- self.emit :connect, session_id
17
- end
18
- end
19
-
20
- def connect
21
- url = @session ? "#{@url}/session=#{@session}" : @url
22
- @ws = EventMachine::WebSocketClient.connect url
23
- @running = true
24
-
25
- @ws.stream do |msg|
26
- data = JSON.parse msg
27
- self.emit data['type'], data['data']
28
- end
29
-
30
- @ws.callback do
31
- self.push :__session_id
32
- end
33
-
34
- @ws.errback do |err|
35
- self.emit :error, err
36
- end
37
-
38
- @ws.disconnect do
39
- self.emit :disconnect
40
- if @running
41
- @__reconnect_timer_id = EM::add_timer 10 do
42
- connect
43
- end
44
- end
45
- end
46
-
47
- return self
48
- end
49
-
50
- def close
51
- @running = false
52
- @ws.close_connection if @ws
53
- EM::cancel_timer @__reconnect_timer_id
54
- end
55
-
56
- def push(type, data={})
57
- @ws.send_msg({:type => type, :data => data, :session => self.session}.to_json)
58
- end
59
-
60
- end
61
- end
62
- end
@@ -1,6 +0,0 @@
1
- require 'json'
2
- require 'eventmachine'
3
- require 'event_emitter'
4
- require 'em-websocket-client'
5
- require File.expand_path 'sinatra-websocketio/version', File.dirname(__FILE__)
6
- require File.expand_path 'em-websocketio-client/client', File.dirname(__FILE__)
data/sample/Gemfile DELETED
@@ -1,13 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'foreman'
4
- gem 'sinatra-contrib'
5
- gem 'sinatra'
6
- gem 'thin'
7
- gem 'rack'
8
- gem 'event_emitter'
9
- gem 'json'
10
- gem 'haml'
11
- gem 'sass'
12
- gem 'em-websocket'
13
- gem 'em-websocket-client'
data/sample/Gemfile.lock DELETED
@@ -1,63 +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.0)
18
- tilt
19
- http_parser.rb (0.5.3)
20
- json (1.7.7)
21
- libwebsocket (0.1.7.1)
22
- addressable
23
- websocket
24
- rack (1.5.2)
25
- rack-protection (1.5.0)
26
- rack
27
- rack-test (0.6.2)
28
- rack (>= 1.0)
29
- sass (3.2.7)
30
- sinatra (1.3.6)
31
- rack (~> 1.4)
32
- rack-protection (~> 1.3)
33
- tilt (~> 1.3, >= 1.3.3)
34
- sinatra-contrib (1.3.2)
35
- backports (>= 2.0)
36
- eventmachine
37
- rack-protection
38
- rack-test
39
- sinatra (~> 1.3.0)
40
- tilt (~> 1.3)
41
- thin (1.5.1)
42
- daemons (>= 1.0.9)
43
- eventmachine (>= 0.12.6)
44
- rack (>= 1.0.0)
45
- thor (0.17.0)
46
- tilt (1.3.6)
47
- websocket (1.0.7)
48
-
49
- PLATFORMS
50
- ruby
51
-
52
- DEPENDENCIES
53
- em-websocket
54
- em-websocket-client
55
- event_emitter
56
- foreman
57
- haml
58
- json
59
- rack
60
- sass
61
- sinatra
62
- sinatra-contrib
63
- thin
data/sample/Procfile DELETED
@@ -1 +0,0 @@
1
- web: bundle exec rackup config.ru -p $PORT