tubesock 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -4
- data/lib/tubesock.rb +36 -6
- data/lib/tubesock/version.rb +1 -1
- data/test/tubesock_test.rb +99 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d841d7d707c6478a67478ceae2ed26d3b25b7b78
|
4
|
+
data.tar.gz: 21d0276fe0b599b83d1bcf5867bf5f2a280a3116
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3cfc1848b42e5d889a901a90279f123e9a53f2d87df6a86215cf7668e5644b7c17c39142ae51adafec517888dca0fa466344fbacfd9026aff7527aab37a4d98
|
7
|
+
data.tar.gz: 512157a89fb5f832e5b2b70cf16671cf96d8845048fa44307cf00de1131acc73b4340b02b5b1cd5ef7b3d2b8db6a0538b3cee21085a89b2fbb0b8020f7f78049
|
data/.travis.yml
CHANGED
data/lib/tubesock.rb
CHANGED
@@ -14,6 +14,11 @@ class Tubesock
|
|
14
14
|
@open_handlers = []
|
15
15
|
@message_handlers = []
|
16
16
|
@close_handlers = []
|
17
|
+
@error_handlers = []
|
18
|
+
|
19
|
+
@close_on_error = true
|
20
|
+
|
21
|
+
@active = true
|
17
22
|
end
|
18
23
|
|
19
24
|
def self.hijack(env)
|
@@ -32,6 +37,10 @@ class Tubesock
|
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
40
|
+
def prevent_close_on_error
|
41
|
+
@close_on_error = false
|
42
|
+
end
|
43
|
+
|
35
44
|
def send_data data, type = :text
|
36
45
|
frame = WebSocket::Frame::Outgoing::Server.new(
|
37
46
|
version: @version,
|
@@ -39,7 +48,7 @@ class Tubesock
|
|
39
48
|
type: type
|
40
49
|
)
|
41
50
|
@socket.write frame.to_s
|
42
|
-
rescue IOError, Errno::EPIPE
|
51
|
+
rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT
|
43
52
|
close
|
44
53
|
end
|
45
54
|
|
@@ -55,20 +64,41 @@ class Tubesock
|
|
55
64
|
@close_handlers << block
|
56
65
|
end
|
57
66
|
|
67
|
+
def onerror(&block)
|
68
|
+
@error_handlers << block
|
69
|
+
end
|
70
|
+
|
58
71
|
def listen
|
59
72
|
keepalive
|
60
73
|
Thread.new do
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
74
|
+
begin
|
75
|
+
@open_handlers.each(&:call)
|
76
|
+
each_frame do |data|
|
77
|
+
@message_handlers.each do |h|
|
78
|
+
begin
|
79
|
+
h.call(data)
|
80
|
+
rescue => e
|
81
|
+
@error_handlers.each{|eh| eh.call(e,data)}
|
82
|
+
close if @close_on_error
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
ensure
|
87
|
+
close
|
65
88
|
end
|
66
|
-
close
|
67
89
|
end
|
68
90
|
end
|
69
91
|
|
70
92
|
def close
|
93
|
+
return unless @active
|
94
|
+
|
71
95
|
@close_handlers.each(&:call)
|
96
|
+
close!
|
97
|
+
|
98
|
+
@active = false
|
99
|
+
end
|
100
|
+
|
101
|
+
def close!
|
72
102
|
if @socket.respond_to?(:closed?)
|
73
103
|
@socket.close unless @socket.closed?
|
74
104
|
else
|
data/lib/tubesock/version.rb
CHANGED
data/test/tubesock_test.rb
CHANGED
@@ -54,4 +54,103 @@ class TubesockTest < Tubesock::TestCase
|
|
54
54
|
|
55
55
|
closed.called.must_equal true
|
56
56
|
end
|
57
|
+
|
58
|
+
def test_onerror_callback
|
59
|
+
interaction = TestInteraction.new
|
60
|
+
|
61
|
+
closed = MockProc.new
|
62
|
+
errored = MockProc.new
|
63
|
+
|
64
|
+
interaction.tubesock do |tubesock|
|
65
|
+
tubesock.onclose &closed
|
66
|
+
|
67
|
+
tubesock.onerror &errored
|
68
|
+
tubesock.onmessage do |message|
|
69
|
+
raise "Really really really bad error"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
interaction.write "Hello world"
|
74
|
+
interaction.join
|
75
|
+
|
76
|
+
errored.called.must_equal true
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_prevent_close_on_error
|
81
|
+
interaction = TestInteraction.new
|
82
|
+
|
83
|
+
closed = MockProc.new
|
84
|
+
|
85
|
+
interaction.tubesock do |tubesock|
|
86
|
+
tubesock.onclose &closed
|
87
|
+
|
88
|
+
tubesock.onerror do |error, message|
|
89
|
+
tubesock.prevent_close_on_error
|
90
|
+
end
|
91
|
+
tubesock.onmessage do |message|
|
92
|
+
raise "Really really really bad error"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
interaction.write "Hello world"
|
97
|
+
closed.called.must_equal nil
|
98
|
+
|
99
|
+
interaction.close
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_default_close_on_error
|
103
|
+
interaction = TestInteraction.new
|
104
|
+
|
105
|
+
closed = MockProc.new
|
106
|
+
|
107
|
+
interaction.tubesock do |tubesock|
|
108
|
+
tubesock.onclose &closed
|
109
|
+
|
110
|
+
tubesock.onmessage do |message|
|
111
|
+
raise "Really really really bad error"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
interaction.write "Hello world"
|
116
|
+
interaction.join
|
117
|
+
|
118
|
+
closed.called.must_equal true
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_multi_frame_no_close
|
122
|
+
interaction = TestInteraction.new
|
123
|
+
|
124
|
+
closed = MockProc.new
|
125
|
+
|
126
|
+
messaged = MockProc.new
|
127
|
+
|
128
|
+
interaction.tubesock do |tubesock|
|
129
|
+
tubesock.onclose &closed
|
130
|
+
tubesock.onmessage &messaged
|
131
|
+
end
|
132
|
+
|
133
|
+
interaction.write "Hello world"
|
134
|
+
sleep 1
|
135
|
+
closed.called.must_equal nil
|
136
|
+
|
137
|
+
interaction.close
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_close_on_open_handler
|
141
|
+
interaction = TestInteraction.new
|
142
|
+
|
143
|
+
closed = MockProc.new
|
144
|
+
|
145
|
+
interaction.tubesock do |tubesock|
|
146
|
+
tubesock.onclose &closed
|
147
|
+
tubesock.onopen do
|
148
|
+
raise "Error opening socket"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
proc {interaction.join}.must_raise RuntimeError
|
153
|
+
closed.called.must_equal true
|
154
|
+
end
|
155
|
+
|
57
156
|
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
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Gauthier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
150
|
rubyforge_project:
|
151
|
-
rubygems_version: 2.
|
151
|
+
rubygems_version: 2.6.1
|
152
152
|
signing_key:
|
153
153
|
specification_version: 4
|
154
154
|
summary: Handle websocket connections via Rack and Rails 4 using concurrency
|