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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '09245285a4bf2d658f2557a741ed7181382ce7a3'
4
- data.tar.gz: 152a31071adcade6e234522d0030bb16c0896060
3
+ metadata.gz: 89726ee2059536ad9626423ad6414b9e1ce71861
4
+ data.tar.gz: af8a405c335b1c845fb0bae3d4976a63a8b368f9
5
5
  SHA512:
6
- metadata.gz: 3fd1c8e41edf0584f091b3d5f3f8bd47b9ab75c62a815558273f79b734c1731747491115227e951e08452cbb18432ebd11f652f50cfc713cc050939cc04f7f0f
7
- data.tar.gz: 9f4049e8f560f5a45101c80998b81eccce9d30f88d7b3ef4acbb3f5db1691455cf2bf683cdda86ee93108df6b3d61c4b1b4d001e8e975f358f84c731c04283bb
6
+ metadata.gz: 21a810716e7ea6cada26e4e98a3b08d2bc20c2148a1d719b5f9e07b474f627e5876b4dce28861eb08ee833b9f79ee6156ab5060a197e00b79ff6e9fcb5763a17
7
+ data.tar.gz: dc78d12a2a03a992a9c25a147f1bf2a7d894ff3c78be3cd85865d4c0f435743504c52998b5105fb62025850447f3c1cc462a8a88599deb168d9ab199d52f5030
@@ -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
- @reply_stream = StrStream.new
60
- post(@uri, stream.buf)
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
- @msg.recv_reply(@reply_stream)
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 post(uri, data)
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
- @servers = {}
10
- @ws = {}
9
+ @handlers = {}
10
+ @sockets = {}
11
11
 
12
- def self.server(key)
13
- @servers[key]
12
+ def self.handler(key)
13
+ @handlers[key]
14
14
  end
15
15
 
16
- def self.ws
17
- @ws
16
+ def self.sockets
17
+ @sockets
18
18
  end
19
19
 
20
20
  def self.close(key)
21
- if @ws.has_key?(key)
22
- @ws[key].close
23
- @ws.delete(key)
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, server)
28
- @servers[key] = server
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
- key = "#{req.host}:#{req.port}"
36
- RackApp.ws[key] = ws
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
- res = RackApp.server(key).on_message(event.data)
40
- ws.send res.bytes
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(key)
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
@@ -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("#{u.host}:#{u.port}", self)
54
+ RackApp.register(uri, self)
55
55
 
56
56
  if RackApp.config.standalone
57
57
  Thread.new do
@@ -1,5 +1,5 @@
1
1
  module Drb
2
2
  module Websocket
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  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.2.1
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-09 00:00:00.000000000 Z
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket