sinatra-websocketio 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/History.txt +4 -0
- data/README.md +11 -0
- data/Rakefile +7 -0
- data/lib/sinatra-websocketio/version.rb +1 -1
- data/test/app.rb +61 -0
- data/test/app/config.ru +12 -0
- data/test/app/main.rb +31 -0
- data/test/test_helper.rb +12 -0
- data/test/test_websocketio.rb +132 -0
- metadata +13 -3
data/Gemfile
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -159,6 +159,17 @@ chat app
|
|
159
159
|
- https://github.com/shokai/sinatra-websocketio/tree/master/sample
|
160
160
|
|
161
161
|
|
162
|
+
Test
|
163
|
+
----
|
164
|
+
|
165
|
+
% gem install bundler
|
166
|
+
% bundle install
|
167
|
+
% export PORT=5000
|
168
|
+
% export WS_PORT=8080
|
169
|
+
% export PID_FILE=/tmp/sinatra-websocketio-test.pid
|
170
|
+
% rake test
|
171
|
+
|
172
|
+
|
162
173
|
Contributing
|
163
174
|
------------
|
164
175
|
1. Fork it
|
data/Rakefile
CHANGED
data/test/app.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
class App
|
2
|
+
|
3
|
+
def self.running
|
4
|
+
@running ? true : false
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.port
|
8
|
+
ENV['PORT'] || 5000
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.ws_port
|
12
|
+
ENV['WS_PORT'] || 8080
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.websocketio_url
|
16
|
+
"ws://localhost:#{ws_port}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.pid_file
|
20
|
+
ENV['PID_FILE'] || "/tmp/sinatra-cometio-testapp.pid"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.app_dir
|
24
|
+
File.expand_path 'app', File.dirname(__FILE__)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.pid
|
28
|
+
return unless @running
|
29
|
+
File.open(pid_file) do |f|
|
30
|
+
pid = f.gets.strip.to_i
|
31
|
+
return pid if pid > 0
|
32
|
+
end
|
33
|
+
return
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.start
|
37
|
+
return if @running
|
38
|
+
File.delete pid_file if File.exists? pid_file
|
39
|
+
Thread.new do
|
40
|
+
IO::popen "cd #{app_dir} && PID_FILE=#{pid_file} WS_PORT=#{ws_port} rackup config.ru -p #{port} > /dev/null 2>&1"
|
41
|
+
end
|
42
|
+
@running = true
|
43
|
+
100.times do
|
44
|
+
if File.exists? pid_file
|
45
|
+
sleep 1
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
sleep 0.1
|
49
|
+
end
|
50
|
+
@running = false
|
51
|
+
return false
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.stop
|
55
|
+
return unless @running
|
56
|
+
system "kill #{pid}"
|
57
|
+
File.delete pid_file if File.exists? pid_file
|
58
|
+
@running = false
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/test/app/config.ru
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require
|
4
|
+
$stdout.sync = true
|
5
|
+
$:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
|
6
|
+
require 'sinatra'
|
7
|
+
require 'sinatra/websocketio'
|
8
|
+
require File.dirname(__FILE__)+'/main'
|
9
|
+
|
10
|
+
set :websocketio, :port => ENV['WS_PORT'].to_i
|
11
|
+
|
12
|
+
run TestApp
|
data/test/app/main.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
pid_file = ENV['PID_FILE'] || "/tmp/sinatra-websocketio-test-pid"
|
2
|
+
File.open(pid_file, "w+") do |f|
|
3
|
+
f.write Process.pid.to_s
|
4
|
+
end
|
5
|
+
|
6
|
+
class TestApp < Sinatra::Application
|
7
|
+
|
8
|
+
get '/' do
|
9
|
+
"sinatra-websocketio v#{Sinatra::WebSocketIO::VERSION}"
|
10
|
+
end
|
11
|
+
|
12
|
+
WebSocketIO.on :connect do |session|
|
13
|
+
puts "new client <#{session}>"
|
14
|
+
end
|
15
|
+
|
16
|
+
WebSocketIO.on :disconnect do |session|
|
17
|
+
puts "disconnect client <#{session}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
WebSocketIO.on :broadcast do |data, from|
|
21
|
+
puts from
|
22
|
+
puts "broadcast <#{from}> - #{data.to_json}"
|
23
|
+
push :broadcast, data
|
24
|
+
end
|
25
|
+
|
26
|
+
WebSocketIO.on :message do |data, from|
|
27
|
+
puts "message <#{from}> - #{data.to_json}"
|
28
|
+
push :message, data, :to => data['to']
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'em-websocketio-client'
|
4
|
+
require File.expand_path 'app', File.dirname(__FILE__)
|
5
|
+
|
6
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
7
|
+
|
8
|
+
['SIGHUP', 'SIGINT', 'SIGKILL', 'SIGTERM'].each do |sig|
|
9
|
+
Kernel.trap sig do
|
10
|
+
App.stop
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestWebSocketIO < 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_simple
|
14
|
+
res = nil
|
15
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
16
|
+
EM::run do
|
17
|
+
client = EM::WebSocketIO::Client.new(App.websocketio_url).connect
|
18
|
+
|
19
|
+
client.on :broadcast do |data|
|
20
|
+
res = data
|
21
|
+
client.close
|
22
|
+
end
|
23
|
+
|
24
|
+
client.on :disconnect do
|
25
|
+
EM.stop_event_loop
|
26
|
+
end
|
27
|
+
|
28
|
+
client.on :connect do |session|
|
29
|
+
push :broadcast, post_data
|
30
|
+
end
|
31
|
+
|
32
|
+
EM::defer do
|
33
|
+
50.times do
|
34
|
+
break if res != nil
|
35
|
+
sleep 0.1
|
36
|
+
end
|
37
|
+
client.close
|
38
|
+
end
|
39
|
+
end
|
40
|
+
assert res != nil, 'server not respond'
|
41
|
+
assert res["time"] == post_data[:time]
|
42
|
+
assert res["msg"] == post_data[:msg]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_client_to_client2
|
46
|
+
## client --> server --> client2
|
47
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
|
48
|
+
res = nil
|
49
|
+
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
|
59
|
+
|
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
|
71
|
+
end
|
72
|
+
|
73
|
+
EM::defer do
|
74
|
+
50.times do
|
75
|
+
break if res != nil
|
76
|
+
sleep 0.1
|
77
|
+
end
|
78
|
+
client.close
|
79
|
+
end
|
80
|
+
end
|
81
|
+
assert res2 != nil, 'server not respond'
|
82
|
+
assert res2["time"] == post_data[:time]
|
83
|
+
assert res2["msg"] == post_data[:msg]
|
84
|
+
assert res == nil
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
def test_broadcast
|
89
|
+
## client --> server --> client&client2
|
90
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
91
|
+
res = nil
|
92
|
+
res2 = nil
|
93
|
+
|
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
|
99
|
+
|
100
|
+
client.on :disconnect do
|
101
|
+
EM.stop_event_loop
|
102
|
+
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
|
113
|
+
end
|
114
|
+
|
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
|
122
|
+
end
|
123
|
+
|
124
|
+
assert res != nil, 'server not respond'
|
125
|
+
assert res["time"] == post_data[:time]
|
126
|
+
assert res["msg"] == post_data[:msg]
|
127
|
+
assert res2 != nil
|
128
|
+
assert res2["time"] == post_data[:time]
|
129
|
+
assert res2["msg"] == post_data[:msg]
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
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.0.
|
4
|
+
version: 0.0.6
|
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-
|
12
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -173,6 +173,11 @@ files:
|
|
173
173
|
- sample/views/index.haml
|
174
174
|
- sample/views/main.scss
|
175
175
|
- sinatra-websocketio.gemspec
|
176
|
+
- test/app.rb
|
177
|
+
- test/app/config.ru
|
178
|
+
- test/app/main.rb
|
179
|
+
- test/test_helper.rb
|
180
|
+
- test/test_websocketio.rb
|
176
181
|
homepage: https://github.com/shokai/sinatra-websocketio
|
177
182
|
licenses: []
|
178
183
|
post_install_message:
|
@@ -197,4 +202,9 @@ rubygems_version: 1.8.24
|
|
197
202
|
signing_key:
|
198
203
|
specification_version: 3
|
199
204
|
summary: Node.js like WebSocket I/O plugin for Sinatra.
|
200
|
-
test_files:
|
205
|
+
test_files:
|
206
|
+
- test/app.rb
|
207
|
+
- test/app/config.ru
|
208
|
+
- test/app/main.rb
|
209
|
+
- test/test_helper.rb
|
210
|
+
- test/test_websocketio.rb
|