em-cometio-client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/History.txt +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +1 -0
- data/em-cometio-client.gemspec +22 -0
- data/lib/em-cometio-client/client.rb +63 -0
- data/lib/em-cometio-client/version.rb +7 -0
- data/lib/em-cometio-client.rb +7 -0
- data/sample/cui_chat_client.rb +30 -0
- metadata +120 -0
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
.DS_Store
|
2
|
+
*.tmp
|
3
|
+
*~
|
4
|
+
*#*
|
5
|
+
vendor
|
6
|
+
.sass-cache
|
7
|
+
.bundle
|
8
|
+
config.yml
|
9
|
+
*.gem
|
10
|
+
*.rbc
|
11
|
+
.config
|
12
|
+
.yardoc
|
13
|
+
InstalledFiles
|
14
|
+
_yardoc
|
15
|
+
coverage
|
16
|
+
doc/
|
17
|
+
lib/bundler/man
|
18
|
+
pkg
|
19
|
+
rdoc
|
20
|
+
spec/reports
|
21
|
+
test/tmp
|
22
|
+
test/version_tmp
|
23
|
+
tmp
|
data/Gemfile
ADDED
data/History.txt
ADDED
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,70 @@
|
|
1
|
+
em-cometio-client
|
2
|
+
=================
|
3
|
+
[Sinatra CometIO](https://github.com/shokai/em-cometio-client) client for eventmachine
|
4
|
+
|
5
|
+
* https://github.com/shokai/em-cometio-client
|
6
|
+
|
7
|
+
|
8
|
+
Installation
|
9
|
+
------------
|
10
|
+
|
11
|
+
% gem install em-cometio-client
|
12
|
+
|
13
|
+
|
14
|
+
Usage
|
15
|
+
-----
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'eventmachine'
|
19
|
+
require 'em-cometio-client'
|
20
|
+
|
21
|
+
EM::run do
|
22
|
+
client = EM::CometIO::Client.new('http://localhost:5000/cometio/io').connect
|
23
|
+
|
24
|
+
client.on :connect do |session|
|
25
|
+
puts "connect!! (sessin_id:#{session})"
|
26
|
+
end
|
27
|
+
|
28
|
+
client.on :error do |err|
|
29
|
+
STDERR.puts err
|
30
|
+
end
|
31
|
+
|
32
|
+
## regist receive "chat" event
|
33
|
+
client.on :chat do |data|
|
34
|
+
puts "#{data['name']} - #{data['message']}"
|
35
|
+
end
|
36
|
+
|
37
|
+
## push "chat" event to Server
|
38
|
+
EM::defer do
|
39
|
+
loop do
|
40
|
+
client.push :chat, {:message => Time.now.to_s, :name => 'clock'}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Sample
|
47
|
+
------
|
48
|
+
|
49
|
+
start [chat server](https://github.com/shokai/cometio-chat-sample)
|
50
|
+
|
51
|
+
% git clone git://github.com/shokai/cometio-chat-sample.git
|
52
|
+
% cd cometio-chat-sample
|
53
|
+
% bundle install
|
54
|
+
% foreman start
|
55
|
+
|
56
|
+
=> http://localhost:5000
|
57
|
+
|
58
|
+
|
59
|
+
sample chat client
|
60
|
+
|
61
|
+
% ruby sample/cui_chat_client.rb
|
62
|
+
|
63
|
+
|
64
|
+
Contributing
|
65
|
+
------------
|
66
|
+
1. Fork it
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'em-cometio-client/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "em-cometio-client"
|
7
|
+
gem.version = EventMachine::CometIO::Client::VERSION
|
8
|
+
gem.authors = ["Sho Hashimoto"]
|
9
|
+
gem.email = ["hashimoto@shokai.org"]
|
10
|
+
gem.description = %q{Sinatra CometIO client for eventmachine}
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.homepage = "https://github.com/shokai/em-cometio-client"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" }
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.add_dependency 'eventmachine'
|
19
|
+
gem.add_dependency 'em-http-request'
|
20
|
+
gem.add_dependency 'json'
|
21
|
+
gem.add_dependency 'event_emitter', '>= 0.2.0'
|
22
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module EventMachine
|
2
|
+
module CometIO
|
3
|
+
class Client
|
4
|
+
include EventEmitter
|
5
|
+
attr_reader :url, :session
|
6
|
+
|
7
|
+
def initialize(url)
|
8
|
+
raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
|
9
|
+
@url = url
|
10
|
+
@session = nil
|
11
|
+
@running = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def push(type, data)
|
15
|
+
that = self
|
16
|
+
http = EM::HttpRequest.new(@url, :connect_timeout => 60000).
|
17
|
+
post(:body => {:type => type, :data => data, :session => @session})
|
18
|
+
http.callback do |res|
|
19
|
+
end
|
20
|
+
http.errback do |err|
|
21
|
+
self.emit :error, "CometIO push error"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def connect
|
26
|
+
return self if @running
|
27
|
+
self.on :__session_id do |session|
|
28
|
+
@session = session
|
29
|
+
self.emit :connect, @session
|
30
|
+
end
|
31
|
+
@running = true
|
32
|
+
get
|
33
|
+
return self
|
34
|
+
end
|
35
|
+
|
36
|
+
def close
|
37
|
+
@running = false
|
38
|
+
self.remove_listener :__session_id
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def get
|
43
|
+
http = EM::HttpRequest.new("#{@url}?session=#{@session}", :connect_timeout => 60000).get
|
44
|
+
http.callback do |res|
|
45
|
+
begin
|
46
|
+
data = JSON.parse res.response
|
47
|
+
self.emit data['type'], data['data']
|
48
|
+
rescue
|
49
|
+
self.emit :error, "CometIO get error"
|
50
|
+
sleep 10
|
51
|
+
end
|
52
|
+
get
|
53
|
+
end
|
54
|
+
http.errback do |err|
|
55
|
+
self.emit :error, "CometIO get error"
|
56
|
+
sleep 10
|
57
|
+
get
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
4
|
+
require 'em-cometio-client'
|
5
|
+
|
6
|
+
name = `whoami`.strip || 'shokai'
|
7
|
+
|
8
|
+
EM::run do
|
9
|
+
client = EM::CometIO::Client.new('http://localhost:5000/cometio/io').connect
|
10
|
+
|
11
|
+
client.on :connect do |session|
|
12
|
+
puts "connect!! (sessin_id:#{session})"
|
13
|
+
end
|
14
|
+
|
15
|
+
client.on :chat do |data|
|
16
|
+
puts "<#{data['name']}> #{data['message']}"
|
17
|
+
end
|
18
|
+
|
19
|
+
client.on :error do |err|
|
20
|
+
STDERR.puts err
|
21
|
+
end
|
22
|
+
|
23
|
+
EM::defer do
|
24
|
+
loop do
|
25
|
+
line = STDIN.gets.strip
|
26
|
+
next if line.empty?
|
27
|
+
client.push :chat, {:message => line, :name => name}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-cometio-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-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: eventmachine
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: em-http-request
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
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: event_emitter
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.2.0
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.2.0
|
78
|
+
description: Sinatra CometIO client for eventmachine
|
79
|
+
email:
|
80
|
+
- hashimoto@shokai.org
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- History.txt
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- em-cometio-client.gemspec
|
92
|
+
- lib/em-cometio-client.rb
|
93
|
+
- lib/em-cometio-client/client.rb
|
94
|
+
- lib/em-cometio-client/version.rb
|
95
|
+
- sample/cui_chat_client.rb
|
96
|
+
homepage: https://github.com/shokai/em-cometio-client
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Sinatra CometIO client for eventmachine
|
120
|
+
test_files: []
|