ws_sync_client 0.1.0 → 0.1.1

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: bd880be25c6299668ed273d8f8f4345809447191
4
- data.tar.gz: a4cf1207d81d14513c5cf0f9828570ba135b5a5e
3
+ metadata.gz: b34d7e77b43d316bf5bb2e433040172f4de60cc3
4
+ data.tar.gz: f5ae8f8251e7f64b34e503cf3a320495681926fc
5
5
  SHA512:
6
- metadata.gz: 66fc604c66760b5d417e010cc8a2d3ee3a8596a4d28ccca319d5509c5ed27ba898e1e95c229555ac44f69596ef7970e164d0a1a16506996bbc13057f9eb0d161
7
- data.tar.gz: 7621ceb91d7e8cbbb2cc4ad289ed40c27564dd20d7629108cd119a1995973f0173934bc7fb4ae42ca7c130bddf664c13c437680289ff6e2c6fc05252f4b19678
6
+ metadata.gz: bfabbd28c1ad2ff9e5ee257d2765eee3e728971203a7f3d8877aa89832e86f592a68602af680824571c63488343d02dc67ca6c2a5e1043f19f7724a9b9020e1f
7
+ data.tar.gz: 331266fc5c667fa37546c1b0b1ed33cfb7e8375f8435919dd6ae11eac6aa8f71f666ae1ca9b7c24312bcc637b8e8db3cc71857a11e319793dd1d14c48d30d935
@@ -1,9 +1,19 @@
1
- = ws_sync
1
+ # ws_sync_client
2
2
 
3
- Description goes here.
3
+ Synchronous Ruby client for
4
+ [the WebSocket protocol](http://tools.ietf.org/html/rfc6455).
5
+
6
+ ## Usage
7
+
8
+ ```ruby
9
+ ws = WsSyncClient 'ws://localhost:4567/path'
10
+ data_frame = ws.recv_frame
11
+ ws.send_frame "Data frame"
12
+ ws.close
13
+ ```
14
+
15
+ ## Contributing to ws_sync
4
16
 
5
- == Contributing to ws_sync
6
-
7
17
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
18
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
19
  * Fork the project.
@@ -12,8 +22,6 @@ Description goes here.
12
22
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
23
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
24
 
15
- == Copyright
16
-
17
- Copyright (c) 2014 Victor Costan. See LICENSE.txt for
18
- further details.
25
+ ## Copyright
19
26
 
27
+ Copyright (c) 2014 Victor Costan. See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -9,7 +9,6 @@ class WsSyncClient
9
9
  # @param [String] url a ws:// URL
10
10
  # @param [Hash] options socket creation options
11
11
  # @option options [Socket] raw create this socket on top of a raw socket.
12
- #
13
12
  def initialize(url, options = {})
14
13
  @url = url
15
14
  @handshake = WebSocket::Handshake::Client.new url: @url
@@ -25,7 +24,9 @@ class WsSyncClient
25
24
  @closed = false
26
25
  @incoming = WebSocket::Frame::Incoming::Client.new(
27
26
  version: @handshake.version)
28
- @incoming << @handshake.leftovers
27
+ if leftovers = @handshake.leftovers
28
+ @incoming << leftovers
29
+ end
29
30
  end
30
31
 
31
32
  # Send a WebSocket frame.
@@ -26,9 +26,11 @@ require 'em-websocket'
26
26
  EM.run do
27
27
  EM::WebSocket.run host: '0.0.0.0', port: 9969 do |ws|
28
28
  ws.onopen do |handshake|
29
- ws.send JSON.dump(handshake: 'completed', path: handshake.path,
30
- query: handshake.query_string,
31
- host: handshake.headers['Host'])
29
+ unless handshake.query['echo_handshake'] == 'false'
30
+ ws.send JSON.dump(handshake: 'completed', path: handshake.path,
31
+ query: handshake.query_string,
32
+ host: handshake.headers['Host'])
33
+ end
32
34
  end
33
35
 
34
36
  ws.onmessage do |message|
@@ -36,11 +38,14 @@ EM.run do
36
38
  case command['cmd']
37
39
  when 'echo'
38
40
  response = { status: 'ok', seq: command['seq'], data: command['data'] }
41
+ when 'close'
42
+ ws.close command['code'], command['reason']
43
+ response = nil
39
44
  else
40
45
  response = { status: 'error', seq: command['seq'],
41
46
  reason: 'unknown command' }
42
47
  end
43
- ws.send JSON.dump(response)
48
+ ws.send JSON.dump(response) unless response.nil?
44
49
  end
45
50
 
46
51
  ws.onclose do
@@ -63,4 +63,65 @@ describe 'WsSync' do
63
63
  end
64
64
  end
65
65
  end
66
+
67
+ describe '#close' do
68
+ it 'does not crash' do
69
+ @ws.close
70
+ end
71
+
72
+ it 'causes #send_frame to raise an exception' do
73
+ @ws.close
74
+ lambda {
75
+ @ws.send_frame JSON.dump(cmd: 'echo', seq: 1, data: 'Hello world!')
76
+ }.must_raise IOError
77
+ end
78
+
79
+ it 'causes #recv_frame to raise an exception' do
80
+ parsed = JSON.parse @ws.recv_frame
81
+ parsed['handshake'].must_equal 'completed'
82
+
83
+ @ws.close
84
+ lambda {
85
+ @ws.recv_frame
86
+ }.must_raise IOError
87
+ end
88
+ end
89
+
90
+ describe 'when the server closes the socket' do
91
+ before do
92
+ parsed = JSON.parse @ws.recv_frame
93
+ unless parsed['handshake'] == 'completed'
94
+ raise 'Setup error'
95
+ end
96
+ @ws.send_frame JSON.dump(cmd: 'close', seq: 1, code: 4321,
97
+ reason: 'testing server-side closing')
98
+ end
99
+
100
+ it '#recv_frame raises an exception' do
101
+ lambda {
102
+ @ws.recv_frame
103
+ }.must_raise IOError
104
+ end
105
+
106
+ it '#close does not crash' do
107
+ @ws.close
108
+ end
109
+ end
110
+ end
111
+
112
+ describe 'WsSync' do
113
+ describe 'without a handshake echo' do
114
+ before do
115
+ @ws = WsSyncClient.new server_root_url + "/?echo_handshake=false"
116
+ end
117
+ after do
118
+ @ws.close
119
+ end
120
+
121
+ it 'completes a send/receive cycle' do
122
+ @ws.send_frame JSON.dump(cmd: 'echo', seq: 1, data: 'Hello world!')
123
+ parsed = JSON.parse @ws.recv_frame
124
+ parsed.must_equal 'status' => 'ok', 'seq' => 1, 'data' => 'Hello world!'
125
+ end
126
+ end
66
127
  end
@@ -0,0 +1,78 @@
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 = "ws_sync_client"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Victor Costan"]
12
+ s.date = "2014-03-16"
13
+ s.description = "WebSocket client that does not require asynchronous programming"
14
+ s.email = "costan@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/ws_sync_client.rb",
28
+ "test/fixtures/ws_server.rb",
29
+ "test/helper.rb",
30
+ "test/test_ws_sync_client.rb",
31
+ "ws_sync_client.gemspec"
32
+ ]
33
+ s.homepage = "http://github.com/pwnall/ws_sync_client"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "2.0.14"
37
+ s.summary = "Synchronous WebSocket client"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 4
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<websocket>, [">= 1.1.2"])
44
+ s.add_runtime_dependency(%q<websocket-native>, [">= 1.0.0"])
45
+ s.add_development_dependency(%q<bundler>, [">= 1.5.3"])
46
+ s.add_development_dependency(%q<debugger>, [">= 1.6.6"])
47
+ s.add_development_dependency(%q<em-websocket>, [">= 0"])
48
+ s.add_development_dependency(%q<jeweler>, [">= 2.0.1"])
49
+ s.add_development_dependency(%q<minitest>, [">= 5.3.0"])
50
+ s.add_development_dependency(%q<rdoc>, [">= 4.1.1"])
51
+ s.add_development_dependency(%q<simplecov>, [">= 0.8.2"])
52
+ s.add_development_dependency(%q<yard>, [">= 0.8.7.3"])
53
+ else
54
+ s.add_dependency(%q<websocket>, [">= 1.1.2"])
55
+ s.add_dependency(%q<websocket-native>, [">= 1.0.0"])
56
+ s.add_dependency(%q<bundler>, [">= 1.5.3"])
57
+ s.add_dependency(%q<debugger>, [">= 1.6.6"])
58
+ s.add_dependency(%q<em-websocket>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, [">= 2.0.1"])
60
+ s.add_dependency(%q<minitest>, [">= 5.3.0"])
61
+ s.add_dependency(%q<rdoc>, [">= 4.1.1"])
62
+ s.add_dependency(%q<simplecov>, [">= 0.8.2"])
63
+ s.add_dependency(%q<yard>, [">= 0.8.7.3"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<websocket>, [">= 1.1.2"])
67
+ s.add_dependency(%q<websocket-native>, [">= 1.0.0"])
68
+ s.add_dependency(%q<bundler>, [">= 1.5.3"])
69
+ s.add_dependency(%q<debugger>, [">= 1.6.6"])
70
+ s.add_dependency(%q<em-websocket>, [">= 0"])
71
+ s.add_dependency(%q<jeweler>, [">= 2.0.1"])
72
+ s.add_dependency(%q<minitest>, [">= 5.3.0"])
73
+ s.add_dependency(%q<rdoc>, [">= 4.1.1"])
74
+ s.add_dependency(%q<simplecov>, [">= 0.8.2"])
75
+ s.add_dependency(%q<yard>, [">= 0.8.7.3"])
76
+ end
77
+ end
78
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ws_sync_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket
@@ -156,19 +156,20 @@ executables: []
156
156
  extensions: []
157
157
  extra_rdoc_files:
158
158
  - LICENSE.txt
159
- - README.rdoc
159
+ - README.md
160
160
  files:
161
161
  - .document
162
162
  - Gemfile
163
163
  - Gemfile.lock
164
164
  - LICENSE.txt
165
- - README.rdoc
165
+ - README.md
166
166
  - Rakefile
167
167
  - VERSION
168
168
  - lib/ws_sync_client.rb
169
169
  - test/fixtures/ws_server.rb
170
170
  - test/helper.rb
171
171
  - test/test_ws_sync_client.rb
172
+ - ws_sync_client.gemspec
172
173
  homepage: http://github.com/pwnall/ws_sync_client
173
174
  licenses:
174
175
  - MIT