websocket-client-simple 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e514163b26ef3c9a5b777947059c009d6b058ae
4
- data.tar.gz: 264e3ce953723f8e9e99c6a1bc6a54433f4a6c96
3
+ metadata.gz: a973fb2e4bcd3952b65cf09ebd8675abfe23e11c
4
+ data.tar.gz: fade26f5f38188f55c5b8366145ba7df3dfe0db2
5
5
  SHA512:
6
- metadata.gz: 852d3a16f5538e187e492410b37c4e46b1aa29eb14d25d0dc2257ce9a2d319d13428eb99fef3a860da647b27e22665a8e185efa21d351bdf03ce0021662670e1
7
- data.tar.gz: 2b889144abf61793096c056c0555395c1bf4d1bf23357c29afc1567d5c3fa401e6e0047d78a270e89fc8befc0fbe395868e4ffc281e4f5cb17865aca4e64c8f9
6
+ metadata.gz: 5ff340d539e7dce6a4afcea177e4162b2cd81c7a41d8f038d0088d41bdb9499093f5db48e652fa6133f3a36f29e5a0155dd84912e60a3db6a12a91ae9cb7e6ea
7
+ data.tar.gz: 78b41ebe27e136107797411146eab54fb1f4c7fa9501f9d47d055afca96d94ea55822e94fa49c81d8488ccf15f415b4db1826728c9b159a86f98c73e701478af
@@ -1,3 +1,7 @@
1
+ === 0.3.0 2016-02-20
2
+
3
+ * "connect" method runs a given block before connecting WebSocket #12
4
+
1
5
  === 0.2.5 2016-02-18
2
6
 
3
7
  * bugfixed sending when broken pipe #15
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
- [![Build Status](https://travis-ci.org/shokai/websocket-client-simple.png?branch=master)](https://travis-ci.org/shokai/websocket-client-simple)
9
-
8
+ [![Circle CI](https://circleci.com/gh/shokai/websocket-client-simple.svg?style=svg)](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
  ------
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: "2.2.2"
@@ -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(url, options)
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 initialize(url, options={})
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,
@@ -1,7 +1,7 @@
1
1
  module WebSocket
2
2
  module Client
3
3
  module Simple
4
- VERSION = "0.2.5"
4
+ VERSION = "0.3.0"
5
5
  end
6
6
  end
7
7
  end
@@ -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
@@ -4,5 +4,6 @@ require 'minitest/autorun'
4
4
  require 'websocket-client-simple'
5
5
  require 'eventmachine'
6
6
  require 'websocket-eventmachine-server'
7
+ require_relative 'echo_server'
7
8
 
8
9
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
@@ -1,40 +1,20 @@
1
- require File.expand_path 'test_helper', File.dirname(__FILE__)
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
- @channel = EM::Channel.new
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
- url = "ws://localhost:#{port}"
35
- client1 = WebSocket::Client::Simple.connect url
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.2.5
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-18 00:00:00.000000000 Z
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
@@ -1,7 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - '2.1.2'
5
- notifications:
6
- slack:
7
- secure: avBo0DyucPM8qK2F2qNT5yirZJNcXWfY9d4QKxZLfOa4aQZJ6y/x78Guo8fki/8ZLNnud+rsty3/QkPxBi3YUWcyIuCXMEcuYc8BWOjL+oD37DPgJxGg6ANKxuMotZMUzZweplkd/KgafugOHr7fsPt5UwX2ZYrZr+ksuemEoxs=