ws_client 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ws_client/version.rb +3 -0
- data/lib/ws_client.rb +201 -0
- data/ws_client.gemspec +38 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fd2fc6088397e593b00bbee38113f2778502cd63
|
4
|
+
data.tar.gz: 9aa868c516d06787d697eaf73e5bae2da859926a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50eb7efd6162a6b0422a269c6c146e426b4414182000fc88fd4c698191aba0ffc0d3c749b537716d6938f76f2c9881539bd8cc5953161ffc7099646e359f2f31
|
7
|
+
data.tar.gz: 83f023b52c171e79436529a19c6a948bbe17649117c594328f04a7d235ccf2b67246c65dfc6038da6798242ff02b47234609286fb387f0d4237148a9b22b9ffe
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Brandon Dewitt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# WsClient
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ws_client`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ws_client'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ws_client
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ws_client.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ws_client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/ws_client.rb
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'event_emitter'
|
3
|
+
require 'websocket'
|
4
|
+
require 'socket'
|
5
|
+
require 'openssl'
|
6
|
+
require 'uri'
|
7
|
+
require "ws_client/version"
|
8
|
+
|
9
|
+
module WsClient
|
10
|
+
def self.connect(url, options={})
|
11
|
+
client = ::WsClient::Client.new
|
12
|
+
yield client if block_given?
|
13
|
+
client.connect url, options
|
14
|
+
return client
|
15
|
+
end
|
16
|
+
|
17
|
+
class Client
|
18
|
+
include EventEmitter
|
19
|
+
attr_reader :url, :handshake, :message_queue, :connect_options
|
20
|
+
|
21
|
+
MS_2 = (1/500.0)
|
22
|
+
FRAME_SIZE = 2048
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@message_queue = ::Queue.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def connect(url = nil, options={})
|
29
|
+
return if open?
|
30
|
+
|
31
|
+
@connect_options = options
|
32
|
+
@connect_url = @url = url || @connect_url || @url
|
33
|
+
|
34
|
+
raise "No URL to connect to" if url.nil?
|
35
|
+
|
36
|
+
uri = URI.parse url
|
37
|
+
@socket = TCPSocket.new(uri.host, uri.port || (uri.scheme == 'wss' ? 443 : 80))
|
38
|
+
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
39
|
+
|
40
|
+
if ['https', 'wss'].include?(uri.scheme)
|
41
|
+
ssl_context = options[:ssl_context] || begin
|
42
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
43
|
+
ctx.ssl_version = options[:ssl_version] || 'SSLv23'
|
44
|
+
ctx.verify_mode = options[:verify_mode] || OpenSSL::SSL::VERIFY_NONE #use VERIFY_PEER for verification
|
45
|
+
cert_store = OpenSSL::X509::Store.new
|
46
|
+
cert_store.set_default_paths
|
47
|
+
ctx.cert_store = cert_store
|
48
|
+
ctx
|
49
|
+
end
|
50
|
+
|
51
|
+
@socket = ::OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
|
52
|
+
@socket.connect
|
53
|
+
end
|
54
|
+
|
55
|
+
@pipe_broken = false
|
56
|
+
@closed = false
|
57
|
+
|
58
|
+
handshake
|
59
|
+
end
|
60
|
+
|
61
|
+
def reconnect
|
62
|
+
connect(nil)
|
63
|
+
end
|
64
|
+
|
65
|
+
def send_data(data, opt={:type => :text})
|
66
|
+
@thread ||= poll # utilize the polling interface, could probably be split into another class
|
67
|
+
write_data(data, opt)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns all responses that have been accepted
|
71
|
+
def send_data_and_wait(data, timeout, opt = { :type => :text })
|
72
|
+
response_data = []
|
73
|
+
write_data(data, opt)
|
74
|
+
pull_next_message_off_of_socket(@socket, timeout)
|
75
|
+
pull_next_message_off_of_socket(@socket, MS_2) # 2ms penalty to check for additional messages
|
76
|
+
|
77
|
+
if message_queue.length > 0
|
78
|
+
message_queue.length.times do
|
79
|
+
response_data << message_queue.pop
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
response_data
|
84
|
+
end
|
85
|
+
|
86
|
+
def close
|
87
|
+
return if @closed
|
88
|
+
|
89
|
+
send_data nil, :type => :close if !@pipe_broken
|
90
|
+
emit :close
|
91
|
+
ensure
|
92
|
+
@closed = true
|
93
|
+
@socket.close if @socket
|
94
|
+
@socket = nil
|
95
|
+
Thread.kill @thread if @thread
|
96
|
+
end
|
97
|
+
|
98
|
+
def closed?
|
99
|
+
!open?
|
100
|
+
end
|
101
|
+
|
102
|
+
def open?
|
103
|
+
@handshake && @handshake.finished? && !@closed
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def handshake
|
109
|
+
@handshake = ::WebSocket::Handshake::Client.new :url => url, :headers => @connect_options[:headers]
|
110
|
+
@handshaked = false
|
111
|
+
@socket.write @handshake.to_s
|
112
|
+
|
113
|
+
while !@handshaked
|
114
|
+
begin
|
115
|
+
read_sockets, _, _ = IO.select([@socket], nil, nil, 10)
|
116
|
+
|
117
|
+
if read_sockets && read_sockets[0]
|
118
|
+
@handshake << @socket.read_nonblock(FRAME_SIZE)
|
119
|
+
|
120
|
+
if @socket.respond_to?(:pending) # SSLSocket
|
121
|
+
@handshake << @socket.read(@socket.pending) while @socket.pending > 0
|
122
|
+
end
|
123
|
+
|
124
|
+
@handshaked = @handshake.finished?
|
125
|
+
end
|
126
|
+
rescue IO::WaitReadable
|
127
|
+
retry
|
128
|
+
rescue IO::WaitWritable
|
129
|
+
IO.select(nil, [socket])
|
130
|
+
retry
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
emit :open if @handshaked
|
135
|
+
end
|
136
|
+
|
137
|
+
def poll
|
138
|
+
return Thread.new(@socket) do |socket|
|
139
|
+
while !@closed do
|
140
|
+
pull_next_message_off_of_socket(socket)
|
141
|
+
|
142
|
+
message_queue.length.times do
|
143
|
+
emit :message, message_queue.pop
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def pull_next_message_off_of_socket(socket, timeout = 10, last_frame = nil)
|
150
|
+
read_sockets, _, _ = IO.select([socket], nil, nil, timeout)
|
151
|
+
|
152
|
+
if read_sockets && read_sockets[0]
|
153
|
+
frame = last_frame || ::WebSocket::Frame::Incoming::Client.new
|
154
|
+
|
155
|
+
begin
|
156
|
+
frame << socket.read_nonblock(FRAME_SIZE)
|
157
|
+
|
158
|
+
if socket.respond_to?(:pending)
|
159
|
+
frame << socket.read(socket.pending) while socket.pending > 0
|
160
|
+
end
|
161
|
+
|
162
|
+
if msg = frame.next
|
163
|
+
message_queue << msg
|
164
|
+
pull_next_message_off_of_socket(socket, MS_2) # 2ms penalty for new frames
|
165
|
+
else
|
166
|
+
pull_next_message_off_of_socket(socket, timeout, frame)
|
167
|
+
end
|
168
|
+
rescue IO::WaitReadable
|
169
|
+
IO.select([socket])
|
170
|
+
retry
|
171
|
+
rescue IO::WaitWritable
|
172
|
+
IO.select(nil, [socket])
|
173
|
+
retry
|
174
|
+
rescue => e
|
175
|
+
close
|
176
|
+
emit :error, e
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def write_data(data, opt)
|
182
|
+
frame = ::WebSocket::Frame::Outgoing::Client.new(:data => data, :type => opt[:type], :version => @handshake.version)
|
183
|
+
|
184
|
+
begin
|
185
|
+
@socket.write_nonblock(frame.to_s)
|
186
|
+
rescue IO::WaitReadable
|
187
|
+
IO.select([@socket]) # OpenSSL needs to read internally
|
188
|
+
retry
|
189
|
+
rescue IO::WaitWritable, Errno::EINTR
|
190
|
+
IO.select(nil, [@socket])
|
191
|
+
retry
|
192
|
+
rescue Errno::EPIPE => e
|
193
|
+
@pipe_broken = true
|
194
|
+
close
|
195
|
+
rescue => e
|
196
|
+
close
|
197
|
+
emit :error, e
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
data/ws_client.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ws_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ws_client"
|
8
|
+
spec.version = WsClient::VERSION
|
9
|
+
spec.authors = ["Brandon Dewitt", "Sho Hashimoto"]
|
10
|
+
spec.email = ["brandonsdewitt+rubygems@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ A Simple websocket client in ruby ... largely from websocket-client-simple
|
13
|
+
with changes merged to use send_data over send and includes send_data_and_wait }
|
14
|
+
spec.description = %q{ Simple websocket client in ruby }
|
15
|
+
spec.homepage = "https://github.com/abrandoned/ws_client"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
|
36
|
+
spec.add_dependency "websocket"
|
37
|
+
spec.add_dependency "event_emitter"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ws_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Dewitt
|
8
|
+
- Sho Hashimoto
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-08-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.12'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.12'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: websocket
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: event_emitter
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :runtime
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
description: " Simple websocket client in ruby "
|
99
|
+
email:
|
100
|
+
- brandonsdewitt+rubygems@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/ws_client.rb
|
113
|
+
- lib/ws_client/version.rb
|
114
|
+
- ws_client.gemspec
|
115
|
+
homepage: https://github.com/abrandoned/ws_client
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
allowed_push_host: https://rubygems.org
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.5.1
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: A Simple websocket client in ruby ... largely from websocket-client-simple
|
140
|
+
with changes merged to use send_data over send and includes send_data_and_wait
|
141
|
+
test_files: []
|