tubesock 0.2.4 → 0.2.5

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: 9afa3d6df0f09dcb321800bb855c0fa9417d1f87
4
- data.tar.gz: ff607d84808e0078895d6131d14b58e76068edb3
3
+ metadata.gz: 5dcfa51a0e1492175493e56346c8a623ae7d3945
4
+ data.tar.gz: 223777060aa74001df982d0c736349051060ba3a
5
5
  SHA512:
6
- metadata.gz: 94a757017908ead3b021b9c2acac18ae278a59c73b47ea14f04a6866e84350158873730d2a93e4dd94bf838052bb8617c503525f3e7e5d4616305aa98371aedf
7
- data.tar.gz: 4d310ffcf12a483c7a1845ec13cbf672431c338d24407f2d89d678c34e787d126f568444143b0d93be9470fad727017628db57150c767f51e14636fc8bad76ad
6
+ metadata.gz: 55dbb0c0136509521c4176a0f9cd546f77a36efbde56abd1bc253d6bb7054073e1bf65f45b483d26c87cd6dcdc9bbbf937f478d33aafa237b55f062e4c6f363b
7
+ data.tar.gz: 095f6094b95b7bcf29aac098afc889baefe0b09339ed41d474a3b272dadf7d315ec243f0dc76d87d564be4e1c835e32bd97b3ed138d66ab2698ea1432af29a3f
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+ gem 'puma', group: :test
data/README.md CHANGED
@@ -29,21 +29,29 @@ Or install it yourself as:
29
29
  To use Tubesock with rack, you need to hijack the rack environment and then return an asynchronous response. For example:
30
30
 
31
31
  ```ruby
32
- def self.call(env)
33
- if websocket?(env)
34
- tubesock = hijack(env)
35
- tubesock.listen
36
- tubesock.onmessage do |message|
37
- puts "Got #{message}"
32
+ require 'tubesock'
33
+
34
+ class Server
35
+ def call(env)
36
+ if env["HTTP_UPGRADE"] == 'websocket'
37
+ tubesock = Tubesock.hijack(env)
38
+ tubesock.onmessage do |message|
39
+ puts "Got #{message}"
40
+ end
41
+ tubesock.listen
42
+ [ -1, {}, [] ]
43
+ else
44
+ [404, {'Content-Type' => 'text/plain'}, ['Not Found']]
38
45
  end
39
- [ -1, {}, [] ]
40
- else
41
- [404, {'Content-Type' => 'text/plain'}, ['Not Found']]
42
46
  end
43
47
  end
44
48
  ```
45
49
 
46
- NOTE: I have not gotten the above to work just yet with just puma or thin. For now, check out the rails example.
50
+ Then you could do the following in your config.ru file to use this class:
51
+
52
+ ```ruby
53
+ run Server.new
54
+ ```
47
55
 
48
56
  ### Rails 4+
49
57
 
@@ -69,7 +69,11 @@ class Tubesock
69
69
 
70
70
  def close
71
71
  @close_handlers.each(&:call)
72
- @socket.close unless @socket.closed?
72
+ if @socket.respond_to?(:closed?)
73
+ @socket.close unless @socket.closed?
74
+ else
75
+ @socket.close
76
+ end
73
77
  end
74
78
 
75
79
  def closed?
@@ -94,7 +98,11 @@ class Tubesock
94
98
  def each_frame
95
99
  framebuffer = WebSocket::Frame::Incoming::Server.new(version: @version)
96
100
  while IO.select([@socket])
97
- data, addrinfo = @socket.recvfrom(2000)
101
+ if @socket.respond_to?(:recvfrom)
102
+ data, addrinfo = @socket.recvfrom(2000)
103
+ else
104
+ data, addrinfo = @socket.readpartial(2000), @socket.peeraddr
105
+ end
98
106
  break if data.empty?
99
107
  framebuffer << data
100
108
  while frame = framebuffer.next
@@ -1,3 +1,3 @@
1
1
  class Tubesock
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -14,8 +14,9 @@ end
14
14
  # Abstract away frames and sockets so that
15
15
  # tests can focus on message interaction
16
16
  class Tubesock::TestCase::TestInteraction
17
- def initialize
17
+ def initialize(socket = nil)
18
18
  @client_socket, @server_socket = UNIXSocket.pair
19
+ @client_socket, @server_socket = socket, socket if socket
19
20
  end
20
21
 
21
22
  def handshake
@@ -70,7 +71,11 @@ class Tubesock::TestCase::TestInteraction
70
71
  tubesock = Tubesock.hijack(env)
71
72
  yield tubesock
72
73
  @thread = tubesock.listen
73
- @client_socket.recvfrom 2000 # flush the handshake
74
+ if @client_socket.respond_to?(:recvfrom)
75
+ @client_socket.recvfrom 2000 # flush the handshake
76
+ else
77
+ @client_socket.readpartial 2000 # flush the handshake
78
+ end
74
79
  tubesock
75
80
  end
76
81
  end
@@ -0,0 +1,43 @@
1
+ require_relative './test_helper'
2
+ require 'puma'
3
+ require 'puma/minissl'
4
+
5
+ class MockEngine
6
+ def read; "data" end
7
+ def inject(data); end
8
+ def extract; end
9
+ def write(data)
10
+ 1
11
+ end
12
+ end
13
+
14
+ class MockSocket < IO
15
+ def initialize; end
16
+ def close; end
17
+ def readpartial(len); end
18
+ end
19
+
20
+ class Puma::MiniSSL::Socket
21
+ def initialize
22
+ @socket = MockSocket.new
23
+ @engine = MockEngine.new
24
+ end
25
+ end
26
+
27
+ class TubesockPumaTest < Tubesock::TestCase
28
+ def test_integration
29
+ socket = Puma::MiniSSL::Socket.new
30
+
31
+ interaction = TestInteraction.new(socket)
32
+
33
+ proc { interaction.close }.must_raise NoMethodError
34
+
35
+ proc { interaction.read }.must_raise NoMethodError
36
+
37
+ interaction.tubesock do |tubesock|
38
+ proc { tubesock.close }.must_be_silent
39
+
40
+ end
41
+
42
+ end
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tubesock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Gauthier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-27 00:00:00.000000000 Z
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -126,6 +126,7 @@ files:
126
126
  - lib/tubesock/version.rb
127
127
  - test/test_helper.rb
128
128
  - test/tubesock_test.rb
129
+ - test/tubesocket_puma_test.rb
129
130
  - tubesock.gemspec
130
131
  homepage: http://github.com/ngauthier/tubesock
131
132
  licenses:
@@ -147,10 +148,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  version: '0'
148
149
  requirements: []
149
150
  rubyforge_project:
150
- rubygems_version: 2.4.1
151
+ rubygems_version: 2.4.5
151
152
  signing_key:
152
153
  specification_version: 4
153
154
  summary: Handle websocket connections via Rack and Rails 4 using concurrency
154
155
  test_files:
155
156
  - test/test_helper.rb
156
157
  - test/tubesock_test.rb
158
+ - test/tubesocket_puma_test.rb