em-websocket-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +20 -0
- data/README.md +45 -0
- data/Rakefile +40 -0
- data/VERSION +1 -0
- data/em-websocket-client.gemspec +66 -0
- data/lib/em-websocket-client.rb +57 -0
- metadata +144 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Micah Wylde
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# em-websocket-client
|
2
|
+
|
3
|
+
This gem implements a simple websocket client inside EventMachine.
|
4
|
+
This might be useful for testing web socket servers or consuming
|
5
|
+
WebSocket APIs. In particular it supports the
|
6
|
+
[hixie-76](http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76)
|
7
|
+
version of the protocol, which is also implemented in Chrome and
|
8
|
+
Safari. At this time, the wss (WebSocket over SSL) protocol is not
|
9
|
+
supported.
|
10
|
+
|
11
|
+
Using the library is simple:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'em-websocket-client'
|
15
|
+
|
16
|
+
EM.run do
|
17
|
+
conn = EventMachine::WebSocketClient.connect("ws://echo.websocket.org/")
|
18
|
+
|
19
|
+
conn.callback do
|
20
|
+
conn.send_msg "Hello!"
|
21
|
+
conn.send_msg "done"
|
22
|
+
end
|
23
|
+
|
24
|
+
conn.errback do |e|
|
25
|
+
puts "Got error: #{e}"
|
26
|
+
end
|
27
|
+
|
28
|
+
conn.stream do |msg|
|
29
|
+
puts "<#{msg}>"
|
30
|
+
if msg == "done"
|
31
|
+
conn.close_connection
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
conn.disconnect do
|
36
|
+
puts "gone"
|
37
|
+
EM::stop_event_loop
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# prints out:
|
42
|
+
# <Hello!>
|
43
|
+
# <done>
|
44
|
+
# gone
|
45
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
gem.name = "em-websocket-client"
|
17
|
+
gem.homepage = "http://github.com/mwylde/em-websocket-client"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{EventMachine WebSocket Client}
|
20
|
+
gem.description = %Q{A WebSocket client implementation for EventMachine}
|
21
|
+
gem.email = "mwylde@wesleyan.edu"
|
22
|
+
gem.authors = ["Micah Wylde"]
|
23
|
+
end
|
24
|
+
Jeweler::RubygemsDotOrgTasks.new
|
25
|
+
|
26
|
+
require 'rspec/core'
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
29
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
30
|
+
end
|
31
|
+
|
32
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{em-websocket-client}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Micah Wylde"]
|
12
|
+
s.date = %q{2011-07-30}
|
13
|
+
s.description = %q{A WebSocket client implementation for EventMachine}
|
14
|
+
s.email = %q{mwylde@wesleyan.edu}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"em-websocket-client.gemspec",
|
28
|
+
"lib/em-websocket-client.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/mwylde/em-websocket-client}
|
31
|
+
s.licenses = ["MIT"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.6.2}
|
34
|
+
s.summary = %q{EventMachine WebSocket Client}
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_runtime_dependency(%q<eventmachine>, [">= 0"])
|
41
|
+
s.add_runtime_dependency(%q<libwebsocket>, [">= 0"])
|
42
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
43
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
44
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
45
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
46
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<eventmachine>, [">= 0"])
|
49
|
+
s.add_dependency(%q<libwebsocket>, [">= 0"])
|
50
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
51
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<eventmachine>, [">= 0"])
|
58
|
+
s.add_dependency(%q<libwebsocket>, [">= 0"])
|
59
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
60
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
63
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'libwebsocket'
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
class WebSocketClient < Connection
|
6
|
+
include Deferrable
|
7
|
+
|
8
|
+
attr_accessor :url
|
9
|
+
|
10
|
+
def self.connect uri
|
11
|
+
p_uri = URI.parse(uri)
|
12
|
+
conn = EM.connect(p_uri.host, p_uri.port || 80, self) do |c|
|
13
|
+
c.url = uri
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def post_init
|
18
|
+
@handshaked = false
|
19
|
+
@frame = LibWebSocket::Frame.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def connection_completed
|
23
|
+
@hs = LibWebSocket::OpeningHandshake::Client.new(:url => @url)
|
24
|
+
send_data @hs.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def stream &cb; @stream = cb; end
|
28
|
+
def disconnect &cb; @disconnect = cb; end
|
29
|
+
|
30
|
+
def receive_data data
|
31
|
+
if !@handshaked
|
32
|
+
result = @hs.parse data
|
33
|
+
fail @hs.error unless result
|
34
|
+
|
35
|
+
if @hs.done?
|
36
|
+
@handshaked = true
|
37
|
+
succeed
|
38
|
+
end
|
39
|
+
else
|
40
|
+
@frame.append(data)
|
41
|
+
while msg = @frame.next
|
42
|
+
@stream.call(msg) if @stream
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def send_msg s
|
48
|
+
frame = LibWebSocket::Frame.new(s)
|
49
|
+
send_data frame.to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
def unbind
|
53
|
+
super
|
54
|
+
@disconnect.call if @disconnect
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-websocket-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Micah Wylde
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-30 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: eventmachine
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: libwebsocket
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: yard
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.6.0
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: bundler
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.0.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: jeweler
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.6.4
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: rcov
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: *id007
|
93
|
+
description: A WebSocket client implementation for EventMachine
|
94
|
+
email: mwylde@wesleyan.edu
|
95
|
+
executables: []
|
96
|
+
|
97
|
+
extensions: []
|
98
|
+
|
99
|
+
extra_rdoc_files:
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
files:
|
103
|
+
- .document
|
104
|
+
- .rspec
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- VERSION
|
110
|
+
- em-websocket-client.gemspec
|
111
|
+
- lib/em-websocket-client.rb
|
112
|
+
has_rdoc: true
|
113
|
+
homepage: http://github.com/mwylde/em-websocket-client
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: -4236319425160152181
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.6.2
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: EventMachine WebSocket Client
|
143
|
+
test_files: []
|
144
|
+
|