websocket-client-simple 0.2.5 → 0.3.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 +4 -4
- data/History.txt +4 -0
- data/README.md +15 -2
- data/circle.yml +3 -0
- data/lib/websocket-client-simple/client.rb +6 -2
- data/lib/websocket-client-simple/version.rb +1 -1
- data/test/echo_server.rb +26 -0
- data/test/test_connect_block.rb +33 -0
- data/test/test_helper.rb +1 -0
- data/test/test_websocket_client_simple.rb +4 -24
- metadata +7 -3
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a973fb2e4bcd3952b65cf09ebd8675abfe23e11c
|
4
|
+
data.tar.gz: fade26f5f38188f55c5b8366145ba7df3dfe0db2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ff340d539e7dce6a4afcea177e4162b2cd81c7a41d8f038d0088d41bdb9499093f5db48e652fa6133f3a36f29e5a0155dd84912e60a3db6a12a91ae9cb7e6ea
|
7
|
+
data.tar.gz: 78b41ebe27e136107797411146eab54fb1f4c7fa9501f9d47d055afca96d94ea55822e94fa49c81d8488ccf15f415b4db1826728c9b159a86f98c73e701478af
|
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -5,8 +5,7 @@ Simple WebSocket Client for Ruby
|
|
5
5
|
- https://github.com/shokai/websocket-client-simple
|
6
6
|
- https://rubygems.org/gems/websocket-client-simple
|
7
7
|
|
8
|
-
[](https://circleci.com/gh/shokai/websocket-client-simple)
|
10
9
|
|
11
10
|
Installation
|
12
11
|
------------
|
@@ -44,6 +43,20 @@ loop do
|
|
44
43
|
end
|
45
44
|
```
|
46
45
|
|
46
|
+
`connect` runs a given block before connecting websocket
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
WebSocket::Client::Simple.connect 'ws://example.com:8888' do |ws|
|
50
|
+
ws.on :open do
|
51
|
+
puts "connect!"
|
52
|
+
end
|
53
|
+
|
54
|
+
ws.on :message do |msg|
|
55
|
+
puts msg.data
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
47
60
|
|
48
61
|
Sample
|
49
62
|
------
|
data/circle.yml
ADDED
@@ -3,14 +3,18 @@ module WebSocket
|
|
3
3
|
module Simple
|
4
4
|
|
5
5
|
def self.connect(url, options={})
|
6
|
-
::WebSocket::Client::Simple::Client.new
|
6
|
+
client = ::WebSocket::Client::Simple::Client.new
|
7
|
+
yield client if block_given?
|
8
|
+
client.connect url, options
|
9
|
+
return client
|
7
10
|
end
|
8
11
|
|
9
12
|
class Client
|
10
13
|
include EventEmitter
|
11
14
|
attr_reader :url, :handshake
|
12
15
|
|
13
|
-
def
|
16
|
+
def connect(url, options={})
|
17
|
+
return if @socket
|
14
18
|
@url = url
|
15
19
|
uri = URI.parse url
|
16
20
|
@socket = TCPSocket.new(uri.host,
|
data/test/echo_server.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module EchoServer
|
2
|
+
def self.start
|
3
|
+
WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => self.port) do |ws|
|
4
|
+
@channel = EM::Channel.new
|
5
|
+
ws.onopen do
|
6
|
+
sid = @channel.subscribe do |mes|
|
7
|
+
ws.send mes # echo to client
|
8
|
+
end
|
9
|
+
ws.onmessage do |msg|
|
10
|
+
@channel.push msg
|
11
|
+
end
|
12
|
+
ws.onclose do
|
13
|
+
@channel.unsubscribe sid
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.port
|
20
|
+
(ENV['WS_PORT'] || 18080).to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.url
|
24
|
+
"ws://localhost:#{self.port}"
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestWebSocketClientSimple < MiniTest::Test
|
4
|
+
|
5
|
+
def test_onopen
|
6
|
+
|
7
|
+
EM::run{
|
8
|
+
|
9
|
+
EchoServer.start
|
10
|
+
|
11
|
+
res = nil
|
12
|
+
|
13
|
+
EM::add_timer 1 do
|
14
|
+
WebSocket::Client::Simple.connect EchoServer.url do |client|
|
15
|
+
client.on :open do
|
16
|
+
client.send "hello world"
|
17
|
+
end
|
18
|
+
|
19
|
+
client.on :message do |msg|
|
20
|
+
res = msg.to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
EM::add_timer 2 do
|
26
|
+
assert_equal res, "hello world"
|
27
|
+
EM::stop_event_loop
|
28
|
+
end
|
29
|
+
}
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,40 +1,20 @@
|
|
1
|
-
|
1
|
+
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class TestWebSocketClientSimple < MiniTest::Test
|
4
4
|
|
5
|
-
def port
|
6
|
-
(ENV['WS_PORT'] || 18080).to_i
|
7
|
-
end
|
8
|
-
|
9
5
|
def test_echo
|
10
6
|
msgs = ['foo','bar','baz']
|
11
7
|
res1 = []
|
12
8
|
res2 = []
|
13
9
|
|
14
10
|
EM::run{
|
15
|
-
|
16
|
-
|
17
|
-
## echo server
|
18
|
-
WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => port) do |ws|
|
19
|
-
ws.onopen do
|
20
|
-
sid = @channel.subscribe do |mes|
|
21
|
-
ws.send mes
|
22
|
-
end
|
23
|
-
ws.onmessage do |msg|
|
24
|
-
@channel.push msg
|
25
|
-
end
|
26
|
-
ws.onclose do
|
27
|
-
@channel.unsubscribe sid
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
11
|
+
EchoServer.start
|
31
12
|
|
32
13
|
## client1 --> server --> client2
|
33
14
|
EM::add_timer 1 do
|
34
|
-
|
35
|
-
|
15
|
+
client1 = WebSocket::Client::Simple.connect EchoServer.url
|
16
|
+
client2 = WebSocket::Client::Simple.connect EchoServer.url
|
36
17
|
assert_equal client1.open?, false
|
37
|
-
client2 = WebSocket::Client::Simple.connect url
|
38
18
|
assert_equal client2.open?, false
|
39
19
|
|
40
20
|
client1.on :message do |msg|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: websocket-client-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Hashimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,12 +116,12 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".gitignore"
|
119
|
-
- ".travis.yml"
|
120
119
|
- Gemfile
|
121
120
|
- History.txt
|
122
121
|
- LICENSE.txt
|
123
122
|
- README.md
|
124
123
|
- Rakefile
|
124
|
+
- circle.yml
|
125
125
|
- lib/websocket-client-simple.rb
|
126
126
|
- lib/websocket-client-simple/client.rb
|
127
127
|
- lib/websocket-client-simple/version.rb
|
@@ -129,6 +129,8 @@ files:
|
|
129
129
|
- sample/echo_server.rb
|
130
130
|
- sample/webbrowser/index.html
|
131
131
|
- sample/webbrowser/main.js
|
132
|
+
- test/echo_server.rb
|
133
|
+
- test/test_connect_block.rb
|
132
134
|
- test/test_helper.rb
|
133
135
|
- test/test_websocket_client_simple.rb
|
134
136
|
- websocket-client-simple.gemspec
|
@@ -157,5 +159,7 @@ signing_key:
|
|
157
159
|
specification_version: 4
|
158
160
|
summary: Simple WebSocket Client for Ruby
|
159
161
|
test_files:
|
162
|
+
- test/echo_server.rb
|
163
|
+
- test/test_connect_block.rb
|
160
164
|
- test/test_helper.rb
|
161
165
|
- test/test_websocket_client_simple.rb
|
data/.travis.yml
DELETED