drb-websocket 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/drb/websocket.rb +52 -8
- data/lib/drb/websocket/rack_app.rb +23 -17
- data/lib/drb/websocket/server.rb +3 -3
- data/lib/drb/websocket/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89726ee2059536ad9626423ad6414b9e1ce71861
|
4
|
+
data.tar.gz: af8a405c335b1c845fb0bae3d4976a63a8b368f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21a810716e7ea6cada26e4e98a3b08d2bc20c2148a1d719b5f9e07b474f627e5876b4dce28861eb08ee833b9f79ee6156ab5060a197e00b79ff6e9fcb5763a17
|
7
|
+
data.tar.gz: dc78d12a2a03a992a9c25a147f1bf2a7d894ff3c78be3cd85865d4c0f435743504c52998b5105fb62025850447f3c1cc462a8a88599deb168d9ab199d52f5030
|
data/lib/drb/websocket.rb
CHANGED
@@ -30,20 +30,54 @@ module DRb
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.open(uri, config)
|
33
|
-
unless uri =~ /^ws:\/\/(.*?):(\d+)(
|
33
|
+
unless uri =~ /^ws:\/\/(.*?):(\d+)(\/(.*))?$/
|
34
34
|
raise(DRbBadScheme, uri) unless uri =~ /^ws:/
|
35
|
-
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
35
|
+
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
36
|
+
end
|
37
|
+
|
38
|
+
path = $4
|
39
|
+
|
40
|
+
callback_handler = nil
|
41
|
+
if path == 'callback'
|
42
|
+
callback_handler = CallbackHandler.new(uri)
|
43
|
+
RackApp.register(uri, callback_handler)
|
44
|
+
elsif path != nil
|
45
|
+
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
46
|
+
end
|
47
|
+
|
48
|
+
ClientSide.new(uri, config, callback_handler)
|
49
|
+
end
|
50
|
+
|
51
|
+
class CallbackHandler
|
52
|
+
def initialize(uri)
|
53
|
+
@uri = uri
|
54
|
+
@queue = Thread::Queue.new
|
55
|
+
end
|
56
|
+
|
57
|
+
def on_message(data)
|
58
|
+
sio = StrStream.new
|
59
|
+
sio.write(data.pack('C*'))
|
60
|
+
@queue.push sio
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
|
64
|
+
def pop
|
65
|
+
@queue.pop
|
66
|
+
end
|
67
|
+
|
68
|
+
def send(data)
|
69
|
+
RackApp.sockets[@uri].send(data.bytes)
|
36
70
|
end
|
37
|
-
ClientSide.new(uri, config)
|
38
71
|
end
|
39
72
|
|
40
73
|
class ClientSide
|
41
|
-
def initialize(uri, config)
|
74
|
+
def initialize(uri, config, handler)
|
42
75
|
@uri = uri
|
43
76
|
@res = nil
|
44
77
|
@config = config
|
45
78
|
@msg = DRbMessage.new(config)
|
46
79
|
@proxy = ENV['HTTP_PROXY']
|
80
|
+
@handler = handler
|
47
81
|
end
|
48
82
|
|
49
83
|
def close
|
@@ -56,15 +90,25 @@ module DRb
|
|
56
90
|
def send_request(ref, msg_id, *arg, &b)
|
57
91
|
stream = StrStream.new
|
58
92
|
@msg.send_request(stream, ref, msg_id, *arg, &b)
|
59
|
-
@
|
60
|
-
|
93
|
+
if @handler
|
94
|
+
@handler.send(stream.buf)
|
95
|
+
else
|
96
|
+
send(@uri, stream.buf)
|
97
|
+
end
|
61
98
|
end
|
62
99
|
|
63
100
|
def recv_reply
|
64
|
-
@
|
101
|
+
@reply_stream = @handler.pop if @handler
|
102
|
+
begin
|
103
|
+
@msg.recv_reply(@reply_stream)
|
104
|
+
rescue
|
105
|
+
close
|
106
|
+
raise $!
|
107
|
+
end
|
65
108
|
end
|
66
109
|
|
67
|
-
def
|
110
|
+
def send(uri, data)
|
111
|
+
@reply_stream = StrStream.new
|
68
112
|
it = URI.parse(uri)
|
69
113
|
path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
|
70
114
|
|
@@ -6,42 +6,44 @@ module DRb
|
|
6
6
|
RackApp.config.use_rack = true
|
7
7
|
end
|
8
8
|
|
9
|
-
@
|
10
|
-
@
|
9
|
+
@handlers = {}
|
10
|
+
@sockets = {}
|
11
11
|
|
12
|
-
def self.
|
13
|
-
@
|
12
|
+
def self.handler(key)
|
13
|
+
@handlers[key]
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
17
|
-
@
|
16
|
+
def self.sockets
|
17
|
+
@sockets
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.close(key)
|
21
|
-
if @
|
22
|
-
@
|
23
|
-
@
|
21
|
+
if @sockets.has_key?(key)
|
22
|
+
@sockets[key].close
|
23
|
+
@sockets.delete(key)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
def self.register(key,
|
28
|
-
@
|
27
|
+
def self.register(key, handler)
|
28
|
+
@handlers[key] = handler
|
29
29
|
end
|
30
30
|
|
31
31
|
def call(env)
|
32
32
|
if Faye::WebSocket.websocket?(env)
|
33
33
|
ws = Faye::WebSocket.new(env)
|
34
34
|
req = Rack::Request.new(env)
|
35
|
-
|
36
|
-
RackApp.
|
35
|
+
uri = "ws://#{req.host}:#{req.port}#{req.path == '/' ? nil : req.path}"
|
36
|
+
RackApp.sockets[uri] = ws
|
37
37
|
|
38
38
|
ws.on :message do |event|
|
39
|
-
|
40
|
-
|
39
|
+
Thread.new do
|
40
|
+
res = RackApp.handler(uri).on_message(event.data)
|
41
|
+
ws.send(res.bytes) if res
|
42
|
+
end.run
|
41
43
|
end
|
42
44
|
|
43
45
|
ws.on :close do |event|
|
44
|
-
RackApp.close(
|
46
|
+
RackApp.close(uri)
|
45
47
|
ws = nil
|
46
48
|
end
|
47
49
|
|
@@ -59,7 +61,7 @@ module DRb
|
|
59
61
|
end
|
60
62
|
|
61
63
|
class Config
|
62
|
-
attr_reader :standalone
|
64
|
+
attr_reader :standalone, :callback_url
|
63
65
|
|
64
66
|
def initialize
|
65
67
|
@standalone = true
|
@@ -68,6 +70,10 @@ module DRb
|
|
68
70
|
def use_rack=(flag)
|
69
71
|
@standalone = !flag
|
70
72
|
end
|
73
|
+
|
74
|
+
def callback_url=(url)
|
75
|
+
@callback_url = url
|
76
|
+
end
|
71
77
|
end
|
72
78
|
end
|
73
79
|
end
|
data/lib/drb/websocket/server.rb
CHANGED
@@ -10,9 +10,9 @@ module DRb
|
|
10
10
|
module WebSocket
|
11
11
|
|
12
12
|
def self.open_server(uri, config)
|
13
|
-
unless uri =~ /^ws:\/\/(.*?):(\d+)(
|
13
|
+
unless uri =~ /^ws:\/\/(.*?):(\d+)(\/(.*))?$/
|
14
14
|
raise(DRbBadScheme, uri) unless uri =~ /^ws:/
|
15
|
-
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
15
|
+
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
16
16
|
end
|
17
17
|
Server.new(uri, config)
|
18
18
|
end
|
@@ -51,7 +51,7 @@ module DRb
|
|
51
51
|
Faye::WebSocket.load_adapter('thin')
|
52
52
|
|
53
53
|
u = URI.parse(uri)
|
54
|
-
RackApp.register(
|
54
|
+
RackApp.register(uri, self)
|
55
55
|
|
56
56
|
if RackApp.config.standalone
|
57
57
|
Thread.new do
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- youchan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|