opal-drb 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: e4bf2949ad3c3cad7b882abb2b3f59b7d867ab2a4653b774742e47b292a21c60
4
- data.tar.gz: 658007bd8f1d9766569b48b7b62e5bdb2b5851870dc3f1acb56d2c4145975610
3
+ metadata.gz: 31f25d589721b4fd2c8e0369f9670d3582f09bc05ed356a9981d592584a255d7
4
+ data.tar.gz: 3b8019bd1301893dff9835b1c0b97c498923a02b76d157b96ef6eeba46bfe1da
5
5
  SHA512:
6
- metadata.gz: 57b4e20deac64183bacc23d3f5bc304d881306ff75af2b44215dff42cf66c35c034e2921734673d4991becf2ef37d17b4861c3639a2d72f93e51da27448f1cde
7
- data.tar.gz: 2293ad912ac41784870eb35d8823c10f06d84b3d940a908605b481a9196d18ed21f672c48ef98d486291551116fc920db3dbddb12d16e220c8090a7042e6adee
6
+ metadata.gz: f0a15f23039a6c46d313ea3b0a3a96473956d2b072b87619195d53d7b8fb5bf2348ed54c04863a358ac306b5124ca7a918e958f61fd07ae3fb90e93d8e858bea
7
+ data.tar.gz: 0262cde7ac7f3681fd067bc72e630692393d305aca8d246045a66470f7ae78c9b926f4509f8e7ee70d8910dd95c807af9051aa0bf103a4eb8be737b81e68f85a
data/example/Gemfile CHANGED
@@ -5,7 +5,7 @@ gem 'sinatra-contrib'
5
5
  gem 'thin'
6
6
 
7
7
  gem 'opal'
8
- gem 'opal-sprockets', github: 'opal/opal-sprockets'
8
+ gem 'opal-sprockets'
9
9
  gem 'drb-websocket'
10
10
 
11
11
  gem 'rake'
data/example/Gemfile.lock CHANGED
@@ -1,12 +1,3 @@
1
- GIT
2
- remote: git://github.com/opal/opal-sprockets.git
3
- revision: 8a8faeb1cfbc51cfa2e71ddb6cf219bd4a14f333
4
- specs:
5
- opal-sprockets (0.4.2.0.11.0.3.1)
6
- opal (~> 0.11.0)
7
- sprockets (~> 3.1)
8
- tilt (>= 1.4)
9
-
10
1
  GEM
11
2
  remote: https://rubygems.org/
12
3
  specs:
@@ -39,6 +30,10 @@ GEM
39
30
  hike (~> 1.2)
40
31
  parser (= 2.3.3.1)
41
32
  sourcemap (~> 0.1.0)
33
+ opal-sprockets (0.4.2.0.11.0.3.1)
34
+ opal (~> 0.11.0)
35
+ sprockets (~> 3.1)
36
+ tilt (>= 1.4)
42
37
  parser (2.3.3.1)
43
38
  ast (~> 2.2)
44
39
  rack (2.0.6)
@@ -88,7 +83,7 @@ PLATFORMS
88
83
  DEPENDENCIES
89
84
  drb-websocket
90
85
  opal
91
- opal-sprockets!
86
+ opal-sprockets
92
87
  rake
93
88
  sass
94
89
  sinatra
@@ -2,13 +2,25 @@ require 'opal'
2
2
  require 'native'
3
3
  require 'opal/drb'
4
4
 
5
- puts ">>>>>>>> Example"
5
+ obj = DRb::DRbObject.new_with_uri "ws://127.0.0.1:1234"
6
+ DRb.start_service("ws://127.0.0.1:1234/callback")
6
7
 
7
- remote = DRb::DRbObject.new_with_uri "ws://127.0.0.1:1234"
8
+ def interval(interval, &func)
9
+ %x(
10
+ setInterval(func, interval);
11
+ )
12
+ end
13
+
14
+ obj.test.then do |res|
15
+ puts res
16
+ end
17
+
18
+ obj.add_callback do |text|
19
+ puts text
20
+ end
8
21
 
9
- promise = remote.get
10
- promise.then do |obj|
11
- obj.test.then do |res|
12
- puts res
13
- end
22
+ i = 0
23
+ interval(1000) do
24
+ obj.notify("notification #{i}")
25
+ i += 1
14
26
  end
@@ -2,15 +2,23 @@ require 'drb/drb'
2
2
  require 'drb/websocket/server'
3
3
 
4
4
  class SampleObject
5
+ def initialize
6
+ @callbacks = []
7
+ end
8
+
5
9
  def test
6
10
  "ACK!"
7
11
  end
8
- end
9
12
 
10
- class SampleFactory
11
- def self.get
12
- DRbObject.new(SampleObject.new)
13
+ def notify(text)
14
+ @callbacks.each do |callback|
15
+ callback.call(text)
16
+ end
17
+ end
18
+
19
+ def add_callback(&callback)
20
+ @callbacks << callback
13
21
  end
14
22
  end
15
23
 
16
- DRb.start_service("ws://127.0.0.1:1234", SampleFactory)
24
+ DRb.start_service("ws://127.0.0.1:1234", SampleObject.new)
data/example/server.rb CHANGED
@@ -6,7 +6,7 @@ if development?
6
6
  end
7
7
 
8
8
  class Server < Sinatra::Base
9
- OPAL = Opal::Server.new do |server|
9
+ OPAL = Opal::Sprockets::Server.new do |server|
10
10
  server.append_path 'app'
11
11
  server.append_path 'assets'
12
12
  server.append_path '../lib'
@@ -44,13 +44,14 @@ module DRb
44
44
  @ref
45
45
  end
46
46
 
47
- undef :to_s
48
- undef :to_a if respond_to?(:to_a)
49
-
50
47
  def self.id2ref
51
48
  @id2ref ||= {}
52
49
  end
53
50
 
51
+ def inspect
52
+ @ref && @ref.inspect
53
+ end
54
+
54
55
  def respond_to?(msg_id, priv=false)
55
56
  case msg_id
56
57
  when :_dump
@@ -58,7 +59,7 @@ module DRb
58
59
  when :marshal_dump
59
60
  false
60
61
  else
61
- method_missing(:respond_to?, msg_id, priv)
62
+ false
62
63
  end
63
64
  end
64
65
 
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module DRb
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -85,17 +85,33 @@ end
85
85
  module DRb
86
86
  module WebSocket
87
87
  class SocketPool
88
- def initialize
89
- @sockets = {}
90
- @proxy = ENV['HTTP_PROXY']
88
+ attr_reader :uri, :ws
89
+
90
+ def initialize(uri, ws)
91
+ @uri = uri
92
+ @ws = ws
91
93
  end
92
94
 
93
- def open(uri)
94
- @sockets[uri] ||= ::WebSocket.new(uri)
95
+ def self.open(uri)
96
+ @sockets ||= {}
97
+ @sockets[uri] ||= new_connection(uri)
95
98
  end
96
99
 
97
- alias_method :[], :open
100
+ def self.new_connection(uri)
101
+ ws = ::WebSocket.new(uri)
102
+
103
+ ws.onclose do
104
+ @sockets[uri] = new_connection(uri)
105
+ end
106
+
107
+ self.new(uri, ws)
108
+ end
109
+
110
+ def [](uri)
111
+ @sockets[uri].ws
112
+ end
98
113
  end
114
+
99
115
  class StrStream
100
116
  def initialize(str='')
101
117
  @buf = str
@@ -120,12 +136,11 @@ module DRb
120
136
  end
121
137
 
122
138
  def self.open(uri, config)
123
- @pool ||= SocketPool.new
124
139
  unless uri =~ /^ws:\/\/(.*?):(\d+)(\/(.*))?$/
125
140
  raise(DRbBadScheme, uri) unless uri =~ /^ws:/
126
141
  raise(DRbBadURI, 'can\'t parse uri:' + uri)
127
142
  end
128
- ClientSide.new(uri, @pool[uri], config)
143
+ ClientSide.new(uri, config)
129
144
  end
130
145
 
131
146
  def self.open_server(uri, config)
@@ -143,27 +158,39 @@ module DRb
143
158
  def initialize(uri, config)
144
159
  @uri = "#{uri}/#{SecureRandom.uuid}"
145
160
  @config = config
161
+ reconnect
146
162
  end
147
163
 
148
164
  def close
149
165
  @ws.close
150
166
  end
151
167
 
152
- def accept
153
- ws = ::WebSocket.new(@uri)
154
- ws.onmessage do |event|
168
+ def reconnect
169
+ @ws.close if @ws
170
+
171
+ @ws = ::WebSocket.new(@uri)
172
+
173
+ @ws.onclose do |event|
174
+ reconnect
175
+ end
176
+
177
+ @ws.onmessage do |event|
155
178
  message_data = event.data.to_s
156
179
  sender_id = message_data.slice(0, 36)
157
180
  message = message_data.slice(36, message_data.length - 36)
158
181
  stream = StrStream.new(message)
159
182
  server_side = ServerSide.new(stream, @config, uri)
160
- yield server_side
183
+ @accepter.call server_side
161
184
 
162
185
  send_data = sender_id.bytes.each_slice(2).map(&:first)
163
186
  send_data += server_side.reply.bytes.each_slice(2).map(&:first)
164
- ws.send(`new Uint8Array(#{send_data}).buffer`)
187
+ @ws.send(`new Uint8Array(#{send_data}).buffer`)
165
188
  end
166
189
  end
190
+
191
+ def accept(&block)
192
+ @accepter = block
193
+ end
167
194
  end
168
195
 
169
196
  class ServerSide
@@ -204,9 +231,9 @@ module DRb
204
231
  end
205
232
 
206
233
  class ClientSide
207
- def initialize(uri, ws, config)
234
+ def initialize(uri, config)
208
235
  @uri = uri
209
- @ws = ws
236
+ @pool = SocketPool.open(uri)
210
237
  @res = nil
211
238
  @config = config
212
239
  @msg = DRbMessage.new(@config)
@@ -214,11 +241,10 @@ module DRb
214
241
  end
215
242
 
216
243
  def alive?
217
- !!@ws && @ws.open?
244
+ !!@pool.ws && @pool.ws.open?
218
245
  end
219
246
 
220
247
  def close
221
- @ws = nil
222
248
  end
223
249
 
224
250
  def send_request(ref, msg_id, *arg, &b)
@@ -250,18 +276,18 @@ module DRb
250
276
  end
251
277
 
252
278
  promise.resolve reply_stream
253
- @ws.off(event_handler)
279
+ @pool.ws.off(event_handler)
254
280
  end
255
- @ws.onmessage(&event_handler)
281
+ @pool.ws.onmessage(&event_handler)
256
282
  byte_data = @sender_id.bytes.each_slice(2).map(&:first)
257
283
  byte_data += data.bytes.each_slice(2).map(&:first)
258
284
 
259
- if @ws.connecting?
260
- @ws.onopen do
261
- @ws.send(`new Uint8Array(#{byte_data}).buffer`)
285
+ if @pool.ws.connecting?
286
+ @pool.ws.onopen do
287
+ @pool.ws.send(`new Uint8Array(#{byte_data}).buffer`)
262
288
  end
263
289
  else
264
- @ws.send(`new Uint8Array(#{byte_data}).buffer`)
290
+ @pool.ws.send(`new Uint8Array(#{byte_data}).buffer`)
265
291
  end
266
292
 
267
293
  promise
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-drb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.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: 2018-11-11 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal