drb-websocket 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +33 -9
- data/lib/drb/websocket.rb +2 -0
- data/lib/drb/websocket/callback_server.rb +74 -0
- data/lib/drb/websocket/client.rb +33 -38
- data/lib/drb/websocket/server.rb +42 -37
- data/lib/drb/websocket/version.rb +1 -1
- data/lib/drb/websocket/ws_client.rb +63 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c2b32ebe2bfd968d7c059fe793877dc8d991c40cd783aa3e09747e74cdfebb6b
|
4
|
+
data.tar.gz: ab794959933f89028c7b31b75e61a0dfd7444f268a5de262b8d4e8f8e2ad2e6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36cdc524946deb717b49c50183a48c87f8efe2857986b6d0a9f6ced795d3380853258a68e9d86b95fef6c0dd31e11512b13ce5329b4b9b559681ff4dd456bba1
|
7
|
+
data.tar.gz: 8fe1f109859622d83654b8fe9981c0cc7585bd7b0b05e033afcb615063d52ae67b16a5e4368081e1cbbd05a2dc955d67c6ff590366bf1a1471430419a8699982
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
drb-websocket (0.4.0)
|
5
|
+
faye-websocket
|
6
|
+
rack
|
7
|
+
thin
|
8
|
+
|
1
9
|
GEM
|
2
10
|
remote: https://rubygems.org/
|
3
11
|
specs:
|
4
|
-
daemons (1.2.
|
5
|
-
|
12
|
+
daemons (1.2.6)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
eventmachine (1.2.5)
|
6
15
|
faye-websocket (0.10.7)
|
7
16
|
eventmachine (>= 0.12.0)
|
8
17
|
websocket-driver (>= 0.5.1)
|
9
|
-
rack (2.0.
|
18
|
+
rack (2.0.4)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.7.0)
|
21
|
+
rspec-core (~> 3.7.0)
|
22
|
+
rspec-expectations (~> 3.7.0)
|
23
|
+
rspec-mocks (~> 3.7.0)
|
24
|
+
rspec-core (3.7.0)
|
25
|
+
rspec-support (~> 3.7.0)
|
26
|
+
rspec-expectations (3.7.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.7.0)
|
29
|
+
rspec-mocks (3.7.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.7.0)
|
32
|
+
rspec-support (3.7.0)
|
10
33
|
thin (1.7.2)
|
11
34
|
daemons (~> 1.0, >= 1.0.9)
|
12
35
|
eventmachine (~> 1.0, >= 1.0.4)
|
13
36
|
rack (>= 1, < 3)
|
14
|
-
websocket-driver (0.
|
37
|
+
websocket-driver (0.7.0)
|
15
38
|
websocket-extensions (>= 0.1.0)
|
16
|
-
websocket-extensions (0.1.
|
39
|
+
websocket-extensions (0.1.3)
|
17
40
|
|
18
41
|
PLATFORMS
|
19
42
|
ruby
|
20
43
|
|
21
44
|
DEPENDENCIES
|
22
|
-
|
23
|
-
|
24
|
-
|
45
|
+
bundler (~> 1.14)
|
46
|
+
drb-websocket!
|
47
|
+
rake (~> 10.0)
|
48
|
+
rspec (~> 3.0)
|
25
49
|
|
26
50
|
BUNDLED WITH
|
27
|
-
1.
|
51
|
+
1.16.1
|
data/lib/drb/websocket.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
module DRb
|
2
|
+
module WebSocket
|
3
|
+
class CallbackServer
|
4
|
+
attr_reader :uri
|
5
|
+
|
6
|
+
def initialize(uri, config)
|
7
|
+
@uri = uri
|
8
|
+
@config = config
|
9
|
+
@wsclient = WSClient.new(uri)
|
10
|
+
@queue = Thread::Queue.new
|
11
|
+
|
12
|
+
@wsclient.on(:message) do |event|
|
13
|
+
message = event.data
|
14
|
+
sender_id = message.shift(36).pack('C*')
|
15
|
+
@queue << [sender_id, message.pack('C*')]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def close
|
20
|
+
@wsclient.close
|
21
|
+
@wsclient = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def accept
|
25
|
+
(sender_id, message) = @queue.pop
|
26
|
+
ServerSide.new(@wsclient, sender_id, message, @config, @uri)
|
27
|
+
end
|
28
|
+
|
29
|
+
class ServerSide
|
30
|
+
attr_reader :uri
|
31
|
+
|
32
|
+
def initialize(wsclient, sender_id, message, config, uri)
|
33
|
+
@message = message
|
34
|
+
@sender_id = sender_id
|
35
|
+
@wsclient = wsclient
|
36
|
+
@uri = uri
|
37
|
+
@config = config
|
38
|
+
@msg = DRbMessage.new(@config)
|
39
|
+
end
|
40
|
+
|
41
|
+
def close
|
42
|
+
@wsclient = nil
|
43
|
+
end
|
44
|
+
|
45
|
+
def alive?
|
46
|
+
!!@wsclient
|
47
|
+
end
|
48
|
+
|
49
|
+
def recv_request
|
50
|
+
begin
|
51
|
+
@req_stream = StrStream.new(@message)
|
52
|
+
@msg.recv_request(@req_stream)
|
53
|
+
rescue
|
54
|
+
close
|
55
|
+
raise $!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def send_reply(succ, result)
|
60
|
+
begin
|
61
|
+
return unless alive?
|
62
|
+
stream = StrStream.new
|
63
|
+
@msg.send_reply(stream, succ, result)
|
64
|
+
@wsclient.send((@sender_id + stream.buf).bytes)
|
65
|
+
rescue
|
66
|
+
puts $!.full_message
|
67
|
+
close
|
68
|
+
raise $!
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/drb/websocket/client.rb
CHANGED
@@ -16,21 +16,25 @@ module DRb
|
|
16
16
|
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
17
17
|
end
|
18
18
|
|
19
|
-
handler = RackApp.handler(uri)
|
20
|
-
|
21
|
-
ClientSide.new(uri, config, callback_handler)
|
19
|
+
handler = RackApp.handler(uri) || Handler.new(uri, config)
|
20
|
+
ClientSide.new(uri, config, handler)
|
22
21
|
end
|
23
22
|
|
24
23
|
class CallbackHandler
|
25
24
|
def initialize(uri)
|
26
25
|
@uri = uri
|
27
26
|
@queue = Thread::Queue.new
|
27
|
+
@sender_id = SecureRandom.uuid
|
28
28
|
end
|
29
29
|
|
30
30
|
def on_message(data)
|
31
|
-
|
32
|
-
|
33
|
-
@
|
31
|
+
sender_id = data.shift(36).pack('C*')
|
32
|
+
|
33
|
+
if sender_id == @sender_id
|
34
|
+
sio = StrStream.new
|
35
|
+
sio.write(data.pack('C*'))
|
36
|
+
@queue.push sio
|
37
|
+
end
|
34
38
|
end
|
35
39
|
|
36
40
|
def on_session_start(ws)
|
@@ -41,51 +45,44 @@ module DRb
|
|
41
45
|
@queue.pop
|
42
46
|
end
|
43
47
|
|
44
|
-
def send(
|
45
|
-
@ws.send(data.bytes)
|
48
|
+
def send(uri, data)
|
49
|
+
@ws.send((@sender_id + data).bytes)
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
49
|
-
class
|
53
|
+
class Handler
|
50
54
|
def initialize(uri, config)
|
51
55
|
@uri = uri
|
52
56
|
@config = config
|
53
57
|
@queue = Thread::Queue.new
|
58
|
+
@sender_id = SecureRandom.uuid
|
54
59
|
end
|
55
60
|
|
56
|
-
def stream
|
57
|
-
@queue.pop
|
58
|
-
end
|
59
|
-
|
60
|
-
def fiber=(fiber)
|
61
|
-
@fiber = fiber
|
61
|
+
def stream(&block)
|
62
|
+
@queue.pop(&block)
|
62
63
|
end
|
63
64
|
|
64
65
|
def send(uri, data)
|
65
|
-
|
66
|
-
|
66
|
+
@wsclient = WSClient.new(uri)
|
67
|
+
@wsclient.on(:message) do |event|
|
68
|
+
message = event.data
|
69
|
+
sender_id = message.shift(36).pack('C*')
|
67
70
|
|
68
|
-
|
69
|
-
EM.run do
|
70
|
-
ws = Faye::WebSocket::Client.new(uri + path)
|
71
|
+
next if sender_id != @sender_id
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
@queue.push sio
|
73
|
+
sio = StrStream.new
|
74
|
+
sio.write(message.pack('C*'))
|
75
|
+
@queue.push sio
|
76
76
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
ws.close
|
77
|
+
if @config[:load_limit] < sio.buf.size
|
78
|
+
raise TypeError, 'too large packet'
|
79
|
+
end
|
82
80
|
|
83
|
-
|
84
|
-
|
85
|
-
end
|
81
|
+
@wsclient.close
|
82
|
+
end
|
86
83
|
|
87
|
-
|
88
|
-
|
84
|
+
@wsclient.on(:open) do
|
85
|
+
@wsclient.send((@sender_id + data).bytes)
|
89
86
|
end
|
90
87
|
end
|
91
88
|
end
|
@@ -98,7 +95,6 @@ module DRb
|
|
98
95
|
@msg = DRbMessage.new(config)
|
99
96
|
@proxy = ENV['HTTP_PROXY']
|
100
97
|
@handler = handler
|
101
|
-
@queue = Thread::Queue.new
|
102
98
|
end
|
103
99
|
|
104
100
|
def close
|
@@ -115,10 +111,9 @@ module DRb
|
|
115
111
|
end
|
116
112
|
|
117
113
|
def recv_reply
|
118
|
-
|
119
|
-
|
114
|
+
reply_stream = @handler.stream
|
120
115
|
begin
|
121
|
-
@msg.recv_reply(
|
116
|
+
@msg.recv_reply(reply_stream)
|
122
117
|
rescue
|
123
118
|
close
|
124
119
|
raise $!
|
data/lib/drb/websocket/server.rb
CHANGED
@@ -11,26 +11,11 @@ module DRb
|
|
11
11
|
raise(DRbBadScheme, uri) unless uri =~ /^ws:/
|
12
12
|
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
13
13
|
end
|
14
|
-
Server.new(uri, config)
|
15
|
-
end
|
16
|
-
|
17
|
-
class Messages
|
18
|
-
def initialize
|
19
|
-
@request_message = Thread::Queue.new
|
20
|
-
@reply_message = Thread::Queue.new
|
21
|
-
end
|
22
|
-
|
23
|
-
def recv_message(message)
|
24
|
-
@request_message.push message
|
25
|
-
@reply_message.pop
|
26
|
-
end
|
27
|
-
|
28
|
-
def request_message
|
29
|
-
@request_message.pop
|
30
|
-
end
|
31
14
|
|
32
|
-
|
33
|
-
|
15
|
+
if $4 == 'callback'
|
16
|
+
CallbackServer.new(uri, config)
|
17
|
+
else
|
18
|
+
Server.new(uri, config)
|
34
19
|
end
|
35
20
|
end
|
36
21
|
|
@@ -41,7 +26,6 @@ module DRb
|
|
41
26
|
@uri = uri
|
42
27
|
@config = config
|
43
28
|
@queue = Thread::Queue.new
|
44
|
-
|
45
29
|
Faye::WebSocket.load_adapter('thin')
|
46
30
|
|
47
31
|
u = URI.parse(uri)
|
@@ -57,47 +41,68 @@ module DRb
|
|
57
41
|
end
|
58
42
|
|
59
43
|
def close
|
60
|
-
@ws.close
|
61
|
-
@ws = nil
|
62
44
|
end
|
63
45
|
|
64
46
|
def accept
|
65
|
-
|
66
|
-
ServerSide.new(
|
47
|
+
ws = @queue.pop
|
48
|
+
ServerSide.new(ws, @config, uri)
|
67
49
|
end
|
68
50
|
|
69
51
|
def on_message(data)
|
70
52
|
end
|
71
53
|
|
72
54
|
def on_session_start(ws)
|
73
|
-
@ws
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
55
|
+
@queue.push(ws)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Messages
|
60
|
+
def initialize
|
61
|
+
@request_message = Thread::Queue.new
|
62
|
+
@reply_message = Thread::Queue.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def recv_message(message)
|
66
|
+
@request_message.push message
|
67
|
+
@reply_message.pop
|
68
|
+
end
|
69
|
+
|
70
|
+
def request_message
|
71
|
+
@request_message.pop
|
72
|
+
end
|
73
|
+
|
74
|
+
def reply(body)
|
75
|
+
@reply_message.push(body)
|
82
76
|
end
|
83
77
|
end
|
84
78
|
|
85
79
|
class ServerSide
|
86
80
|
attr_reader :uri
|
87
81
|
|
88
|
-
def initialize(
|
82
|
+
def initialize(ws, config, uri)
|
89
83
|
@uri = uri
|
90
|
-
@messages = messages
|
91
84
|
@config = config
|
92
85
|
@msg = DRbMessage.new(@config)
|
86
|
+
@ws = ws
|
87
|
+
|
88
|
+
@messages = Messages.new
|
89
|
+
@ws.on(:message) do |event|
|
90
|
+
message = event.data
|
91
|
+
sender_id = message.shift(36)
|
92
|
+
EM.defer do
|
93
|
+
res = @messages.recv_message(message.pack('C*'))
|
94
|
+
@ws.send(sender_id + res.bytes)
|
95
|
+
end
|
96
|
+
end
|
93
97
|
end
|
94
98
|
|
95
99
|
def close
|
96
|
-
@
|
100
|
+
@ws.close
|
101
|
+
@ws = nil
|
97
102
|
end
|
98
103
|
|
99
104
|
def alive?
|
100
|
-
!!@
|
105
|
+
!!@ws
|
101
106
|
end
|
102
107
|
|
103
108
|
def recv_request
|
@@ -0,0 +1,63 @@
|
|
1
|
+
|
2
|
+
module DRb
|
3
|
+
module WebSocket
|
4
|
+
class WSClient
|
5
|
+
attr_reader :uri
|
6
|
+
|
7
|
+
def initialize(uri)
|
8
|
+
@uri = uri
|
9
|
+
WSClient.start
|
10
|
+
|
11
|
+
@handlers = { open: [], message: [] }
|
12
|
+
@ws = Faye::WebSocket::Client.new(uri)
|
13
|
+
|
14
|
+
EM.defer do
|
15
|
+
@ws.on :open do |event|
|
16
|
+
@handlers[:open].each do |proc|
|
17
|
+
proc.call event
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
@ws.on :message do |event|
|
22
|
+
@handlers[:message].each do |proc|
|
23
|
+
proc.call event
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def on(event, &block)
|
30
|
+
@handlers[event] << block
|
31
|
+
end
|
32
|
+
|
33
|
+
def send(data)
|
34
|
+
@ws.send(data)
|
35
|
+
end
|
36
|
+
|
37
|
+
def close
|
38
|
+
@ws.close
|
39
|
+
@ws = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.start
|
43
|
+
if @thread
|
44
|
+
return
|
45
|
+
end
|
46
|
+
|
47
|
+
@thread = Thread.new do
|
48
|
+
EM.run
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.thread
|
53
|
+
@thread
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.stop
|
57
|
+
EM.stop
|
58
|
+
@thread.join
|
59
|
+
@thread = nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: drb-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- youchan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -115,10 +115,12 @@ files:
|
|
115
115
|
- examples/client.rb
|
116
116
|
- examples/server.rb
|
117
117
|
- lib/drb/websocket.rb
|
118
|
+
- lib/drb/websocket/callback_server.rb
|
118
119
|
- lib/drb/websocket/client.rb
|
119
120
|
- lib/drb/websocket/rack_app.rb
|
120
121
|
- lib/drb/websocket/server.rb
|
121
122
|
- lib/drb/websocket/version.rb
|
123
|
+
- lib/drb/websocket/ws_client.rb
|
122
124
|
homepage: https://github.com/youchan/drb-websocket
|
123
125
|
licenses: []
|
124
126
|
metadata: {}
|
@@ -138,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
140
|
version: '0'
|
139
141
|
requirements: []
|
140
142
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.7.3
|
142
144
|
signing_key:
|
143
145
|
specification_version: 4
|
144
146
|
summary: A druby protocol of WebSocket.
|