em-cometio-client 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 +3 -0
- data/History.txt +4 -0
- data/README.md +11 -1
- data/Rakefile +7 -0
- data/lib/em-cometio-client/version.rb +1 -1
- data/test/app.rb +57 -0
- data/test/app/config.ru +9 -0
- data/test/app/main.rb +31 -0
- data/test/test_em_cometio_client.rb +136 -0
- data/test/test_helper.rb +15 -0
- metadata +12 -2
data/Gemfile
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
em-cometio-client
|
2
2
|
=================
|
3
|
-
[Sinatra CometIO](https://github.com/shokai/em-cometio-client)
|
3
|
+
[Sinatra CometIO](https://github.com/shokai/em-cometio-client) Client for eventmachine
|
4
4
|
|
5
5
|
* https://github.com/shokai/em-cometio-client
|
6
6
|
|
@@ -61,6 +61,16 @@ sample chat client
|
|
61
61
|
% ruby sample/cui_chat_client.rb
|
62
62
|
|
63
63
|
|
64
|
+
Test
|
65
|
+
----
|
66
|
+
|
67
|
+
% gem install bundler
|
68
|
+
% bundle install
|
69
|
+
% export PORT=5000
|
70
|
+
% export PID_FILE=/tmp/em-cometio-client-testapp.pid
|
71
|
+
% rake test
|
72
|
+
|
73
|
+
|
64
74
|
Contributing
|
65
75
|
------------
|
66
76
|
1. Fork it
|
data/Rakefile
CHANGED
data/test/app.rb
ADDED
@@ -0,0 +1,57 @@
|
|
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.cometio_url
|
12
|
+
"http://localhost:#{port}/cometio/io"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.pid_file
|
16
|
+
ENV['PID_FILE'] || "/tmp/em-cometio-client-testapp.pid"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.app_dir
|
20
|
+
File.expand_path 'app', File.dirname(__FILE__)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.pid
|
24
|
+
return unless @running
|
25
|
+
File.open(pid_file) do |f|
|
26
|
+
pid = f.gets.strip.to_i
|
27
|
+
return pid if pid > 0
|
28
|
+
end
|
29
|
+
return
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.start
|
33
|
+
return if @running
|
34
|
+
File.delete pid_file if File.exists? pid_file
|
35
|
+
Thread.new do
|
36
|
+
IO::popen "cd #{app_dir} && PID_FILE=#{pid_file} rackup config.ru -p #{port} > /dev/null 2>&1"
|
37
|
+
end
|
38
|
+
@running = true
|
39
|
+
100.times do
|
40
|
+
if File.exists? pid_file
|
41
|
+
sleep 1
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
sleep 0.1
|
45
|
+
end
|
46
|
+
@running = false
|
47
|
+
return false
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.stop
|
51
|
+
return unless @running
|
52
|
+
system "kill #{pid}"
|
53
|
+
File.delete pid_file if File.exists? pid_file
|
54
|
+
@running = false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/test/app/config.ru
ADDED
data/test/app/main.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
pid_file = ENV['PID_FILE'] || "/tmp/sinatra-cometio-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-cometio v#{Sinatra::CometIO::VERSION}"
|
10
|
+
end
|
11
|
+
|
12
|
+
CometIO.on :connect do |session|
|
13
|
+
puts "new client <#{session}>"
|
14
|
+
end
|
15
|
+
|
16
|
+
CometIO.on :disconnect do |session|
|
17
|
+
puts "disconnect client <#{session}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
CometIO.on :broadcast do |data, from|
|
21
|
+
puts from
|
22
|
+
puts "broadcast <#{from}> - #{data.to_json}"
|
23
|
+
push :broadcast, data
|
24
|
+
end
|
25
|
+
|
26
|
+
CometIO.on :message do |data, from|
|
27
|
+
puts "message <#{from}> - #{data.to_json}"
|
28
|
+
push :message, data, :to => data['to']
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestEmCometioClient < 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
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
15
|
+
res = nil
|
16
|
+
|
17
|
+
EM::run do
|
18
|
+
client = EM::CometIO::Client.new(App.cometio_url).connect
|
19
|
+
client.on :broadcast do |data|
|
20
|
+
res = data
|
21
|
+
client.close
|
22
|
+
EM::add_timer 1 do
|
23
|
+
EM::stop
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
client.on :connect do |session|
|
28
|
+
push :broadcast, post_data
|
29
|
+
end
|
30
|
+
|
31
|
+
client.on :error do |err|
|
32
|
+
EM::add_timer 1 do
|
33
|
+
EM::stop
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
assert res != nil, 'server not respond'
|
39
|
+
assert res["time"] == post_data[:time]
|
40
|
+
assert res["msg"] == post_data[:msg]
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_client_to_client2
|
44
|
+
## client --> server --> client2
|
45
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
|
46
|
+
res = nil
|
47
|
+
res2 = nil
|
48
|
+
EM::run do
|
49
|
+
client = EM::CometIO::Client.new(App.cometio_url).connect
|
50
|
+
client.on :message do |data|
|
51
|
+
res = data
|
52
|
+
end
|
53
|
+
|
54
|
+
client.on :error do |err|
|
55
|
+
EM::add_timer 1 do
|
56
|
+
EM::stop
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
client.on :connect do |session|
|
61
|
+
client2 = EM::CometIO::Client.new(App.cometio_url).connect
|
62
|
+
client2.on :error do |err|
|
63
|
+
EM::add_timer 1 do
|
64
|
+
EM::stop
|
65
|
+
end
|
66
|
+
end
|
67
|
+
client2.on :connect do |session2|
|
68
|
+
post_data['to'] = session2
|
69
|
+
client.push :message, post_data
|
70
|
+
end
|
71
|
+
client2.on :message do |data|
|
72
|
+
res2 = data
|
73
|
+
client2.close
|
74
|
+
client.close
|
75
|
+
EM::add_timer 1 do
|
76
|
+
EM::stop
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
assert res2 != nil, 'server not respond'
|
84
|
+
assert res2["time"] == post_data[:time]
|
85
|
+
assert res2["msg"] == post_data[:msg]
|
86
|
+
assert res == nil
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def test_broadcast
|
91
|
+
## client --> server --> client&client2
|
92
|
+
post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
|
93
|
+
res = nil
|
94
|
+
res2 = nil
|
95
|
+
EM::run do
|
96
|
+
client = EM::CometIO::Client.new(App.cometio_url).connect
|
97
|
+
client.on :broadcast do |data|
|
98
|
+
res = data
|
99
|
+
client.close
|
100
|
+
end
|
101
|
+
|
102
|
+
client.on :error do |err|
|
103
|
+
EM::add_timer 1 do
|
104
|
+
EM::stop
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
client.on :connect do |session|
|
109
|
+
client2 = EM::CometIO::Client.new(App.cometio_url).connect
|
110
|
+
client2.on :error do |err|
|
111
|
+
EM::add_timer 1 do
|
112
|
+
EM::stop
|
113
|
+
end
|
114
|
+
end
|
115
|
+
client2.on :connect do |session2|
|
116
|
+
client.push :broadcast, post_data
|
117
|
+
end
|
118
|
+
client2.on :broadcast do |data|
|
119
|
+
res2 = data
|
120
|
+
client2.close
|
121
|
+
EM::add_timer 1 do
|
122
|
+
EM::stop
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
assert res != nil, 'server not respond'
|
129
|
+
assert res["time"] == post_data[:time]
|
130
|
+
assert res["msg"] == post_data[:msg]
|
131
|
+
assert res2 != nil
|
132
|
+
assert res2["time"] == post_data[:time]
|
133
|
+
assert res2["msg"] == post_data[:msg]
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'em-cometio-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
|
+
EM::add_timer do
|
12
|
+
EM::stop
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-cometio-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -93,6 +93,11 @@ files:
|
|
93
93
|
- lib/em-cometio-client/client.rb
|
94
94
|
- lib/em-cometio-client/version.rb
|
95
95
|
- sample/cui_chat_client.rb
|
96
|
+
- test/app.rb
|
97
|
+
- test/app/config.ru
|
98
|
+
- test/app/main.rb
|
99
|
+
- test/test_em_cometio_client.rb
|
100
|
+
- test/test_helper.rb
|
96
101
|
homepage: https://github.com/shokai/em-cometio-client
|
97
102
|
licenses: []
|
98
103
|
post_install_message:
|
@@ -117,4 +122,9 @@ rubygems_version: 1.8.24
|
|
117
122
|
signing_key:
|
118
123
|
specification_version: 3
|
119
124
|
summary: Sinatra CometIO client for eventmachine
|
120
|
-
test_files:
|
125
|
+
test_files:
|
126
|
+
- test/app.rb
|
127
|
+
- test/app/config.ru
|
128
|
+
- test/app/main.rb
|
129
|
+
- test/test_em_cometio_client.rb
|
130
|
+
- test/test_helper.rb
|