em-websocketio-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in em-websocketio-client.gemspec
4
+ gemspec
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2013-03-24
2
+
3
+ * EM::WebSocketIO::Client
4
+ * add tests
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sho Hashimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ em-websocketio-client
2
+ =====================
3
+ [Sinatra WebSocketIO](https://github.com/shokai/sinatra-websocketio) Client for eventmachine
4
+
5
+ * https://github.com/shokai/em-websocketio-client
6
+
7
+ Installation
8
+ ------------
9
+
10
+ % gem install em-websocketio-client
11
+
12
+
13
+ Usage
14
+ -----
15
+
16
+ ```ruby
17
+ require 'eventmachine'
18
+ require 'em-websocketio-client'
19
+
20
+ EM::run do
21
+ client = EM::WebSocketIO::Client.new('ws://localhost:8080').connect
22
+
23
+ client.on :connect do |session|
24
+ puts "connect!! (sessin_id:#{session})"
25
+ end
26
+
27
+ client.on :disconnect do
28
+ puts "disconnect websocketio"
29
+ end
30
+
31
+ client.on :error do |err|
32
+ STDERR.puts err
33
+ end
34
+
35
+ ## regist receive "chat" event
36
+ client.on :chat do |data|
37
+ puts "#{data['name']} - #{data['message']}"
38
+ end
39
+
40
+ ## push "chat" event to Server
41
+ EM::add_periodic_timer 10 do
42
+ client.push :chat, {:message => Time.now.to_s, :name => 'clock'}
43
+ end
44
+ end
45
+ ```
46
+
47
+
48
+ Sample
49
+ ------
50
+
51
+ start [chat server](https://github.com/shokai/websocketio-chat-sample)
52
+
53
+ % git clone git://github.com/shokai/websocketio-chat-sample.git
54
+ % cd websocketio-chat-sample
55
+ % bundle install
56
+ % foreman start
57
+
58
+ => http://localhost:5000
59
+
60
+
61
+ sample chat client
62
+
63
+ % ruby sample/cui_chat_client.rb
64
+
65
+
66
+ Test
67
+ ----
68
+
69
+ % gem install bundler
70
+ % bundle install
71
+ % export PORT=5000
72
+ % export WS_PORT=8080
73
+ % export PID_FILE=/tmp/em-websocketio-client-testapp.pid
74
+ % rake test
75
+
76
+
77
+ Contributing
78
+ ------------
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/test_*.rb"
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'em-websocketio-client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "em-websocketio-client"
8
+ spec.version = EventMachine::WebSocketIO::Client::VERSION
9
+ spec.authors = ["Sho Hashimoto"]
10
+ spec.email = ["hashimoto@shokai.org"]
11
+ spec.description = %q{Sinatra WebSocketIO client for eventmachine}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/shokai/em-websocketio-client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" }
17
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "sinatra-websocketio"
25
+ spec.add_development_dependency "thin"
26
+
27
+ spec.add_dependency "em-websocket-client"
28
+ spec.add_dependency "event_emitter"
29
+ end
@@ -0,0 +1,62 @@
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
@@ -0,0 +1,7 @@
1
+ module EventMachine
2
+ module WebSocketIO
3
+ class Client
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ require 'eventmachine'
2
+ require 'em-websocket-client'
3
+ require 'json'
4
+ require 'event_emitter'
5
+
6
+ require "em-websocketio-client/version"
7
+ require "em-websocketio-client/client"
8
+
9
+ module EventMachine
10
+ module WebSocketIO
11
+ class Client
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'em-websocketio-client'
5
+
6
+ name = `whoami`.strip || 'shokai'
7
+
8
+ EM::run do
9
+ client = EM::WebSocketIO::Client.new('ws://localhost:8080').connect
10
+
11
+ client.on :connect do |session|
12
+ puts "connect!! (sessin_id:#{session})"
13
+ end
14
+
15
+ client.on :disconnect do
16
+ puts "disconnect websocketio"
17
+ end
18
+
19
+ client.on :chat do |data|
20
+ puts "<#{data['name']}> #{data['message']}"
21
+ end
22
+
23
+ client.on :error do |err|
24
+ STDERR.puts err
25
+ end
26
+
27
+ EM::defer do
28
+ loop do
29
+ line = STDIN.gets.strip
30
+ next if line.empty?
31
+ client.push :chat, {:message => line, :name => name}
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require
4
+ require 'sinatra'
5
+ require 'sinatra/base'
6
+ $stdout.sync = true
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,33 @@
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::Base
7
+ register Sinatra::WebSocketIO
8
+ io = Sinatra::WebSocketIO
9
+
10
+ get '/' do
11
+ "sinatra-websocketio v#{Sinatra::WebSocketIO::VERSION}"
12
+ end
13
+
14
+ io.on :connect do |session|
15
+ puts "new client <#{session}>"
16
+ end
17
+
18
+ io.on :disconnect do |session|
19
+ puts "disconnect client <#{session}>"
20
+ end
21
+
22
+ io.on :broadcast do |data, from|
23
+ puts from
24
+ puts "broadcast <#{from}> - #{data.to_json}"
25
+ push :broadcast, data
26
+ end
27
+
28
+ io.on :message do |data, from|
29
+ puts "message <#{from}> - #{data.to_json}"
30
+ push :message, data, :to => data['to']
31
+ end
32
+
33
+ end
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/em-websocketio-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
@@ -0,0 +1,136 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestEmWebSocketIOClient < 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::WebSocketIO::Client.new(App.websocketio_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::WebSocketIO::Client.new(App.websocketio_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::WebSocketIO::Client.new(App.websocketio_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::WebSocketIO::Client.new(App.websocketio_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::WebSocketIO::Client.new(App.websocketio_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
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'minitest/autorun'
5
+ require 'em-websocketio-client'
6
+ require File.expand_path 'app', 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 ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-websocketio-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sho Hashimoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
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: minitest
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: sinatra-websocketio
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: thin
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: em-websocket-client
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: event_emitter
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Sinatra WebSocketIO client for eventmachine
127
+ email:
128
+ - hashimoto@shokai.org
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - History.txt
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - em-websocketio-client.gemspec
140
+ - lib/em-websocketio-client.rb
141
+ - lib/em-websocketio-client/client.rb
142
+ - lib/em-websocketio-client/version.rb
143
+ - sample/cui_chat_client.rb
144
+ - test/app.rb
145
+ - test/app/config.ru
146
+ - test/app/main.rb
147
+ - test/test_em_websocketio_client.rb
148
+ - test/test_helper.rb
149
+ homepage: https://github.com/shokai/em-websocketio-client
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ segments:
163
+ - 0
164
+ hash: 1279394156745028555
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ segments:
172
+ - 0
173
+ hash: 1279394156745028555
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 1.8.24
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: Sinatra WebSocketIO client for eventmachine
180
+ test_files:
181
+ - test/app.rb
182
+ - test/app/config.ru
183
+ - test/app/main.rb
184
+ - test/test_em_websocketio_client.rb
185
+ - test/test_helper.rb