drb-websocket 0.1.0 → 0.2.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: 0e85bc4e5a760ce0a2db683ff1e2ce1c8e669b7a
4
- data.tar.gz: '04272481701b4ccec845f66876ca8899f27a90eb'
3
+ metadata.gz: c4af96ea554d51d5d2e744d6d49380487d5e3633
4
+ data.tar.gz: f1f445f420f670f8b6de1b42e39aab2e8c0b5343
5
5
  SHA512:
6
- metadata.gz: 00c8520e27a1c60c12a58a07dff4d3012a0a28413b18c0de269ca0c63800d3537aa9e8e9ce052467e63c831e7e820a28baa04aa9c42a7cf095df9b30426c685a
7
- data.tar.gz: 9f0b53eb4678cf698d5c57c3bb316fade1148300e379fc1182c72c1fd72839e63f901261a6793425bc50f834bc4d0ece0bd607eb11d04eeec072ce38979fb866
6
+ metadata.gz: 9c14f3b8ff719ccf7c57f364f9f04732ff066a9130f0cee2e0679bbb7c14fb99b35d0ca2f9643e6fc6dbc28af06110ce1a9b33ac7fb21cd0ac25198f6e68af2a
7
+ data.tar.gz: 991489d663cffde1db4cc133e2364fafa65b7897cafb9ef0c3ceb7a5a9926db8b673726b78cd2a4fc676531a54a86ced87ee62324fe2206dc0c8fcb809f7a693
data/README.md CHANGED
@@ -5,12 +5,22 @@ A druby protocol of WebSocket
5
5
 
6
6
  This is experimental implementation of druby protocol with WebSocket.
7
7
 
8
+ Installation
9
+ =====
10
+
11
+ gem install drb-websocket
12
+
8
13
  How to run
9
14
  =====
10
15
 
16
+ cd example
17
+ bundle install
18
+
11
19
  run server
12
- ruby -I . server.rb
20
+
21
+ bundle exec ruby -I. server.rb
13
22
 
14
23
  run client
15
- ruby -I . client.rb
24
+
25
+ bundle exec ruby -I. client.rb
16
26
 
@@ -0,0 +1,55 @@
1
+ module DRb
2
+ module WebSocket
3
+ class RackApp
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ @servers = {}
9
+ @ws = {}
10
+
11
+ def self.server(key)
12
+ @servers[key]
13
+ end
14
+
15
+ def self.ws
16
+ @ws
17
+ end
18
+
19
+ def self.close(key)
20
+ if @ws.has_key?(key)
21
+ @ws[key].close
22
+ @ws.delete(key)
23
+ end
24
+ end
25
+
26
+ def self.register(key, server)
27
+ @servers[key] = server
28
+ end
29
+
30
+ def call(env)
31
+ if Faye::WebSocket.websocket?(env)
32
+ ws = Faye::WebSocket.new(env)
33
+ req = Rack::Request.new(env)
34
+ key = "#{req.host}:#{req.port}"
35
+ RackApp.ws[key] = ws
36
+
37
+ ws.on :message do |event|
38
+ res = RackApp.server(key).on_message(event.data)
39
+ ws.send res.bytes
40
+ end
41
+
42
+ ws.on :close do |event|
43
+ RackApp.close(key)
44
+ ws = nil
45
+ end
46
+
47
+ # Return async Rack response
48
+ ws.rack_response
49
+ else
50
+ @app.call(env)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,4 +1,5 @@
1
1
  require 'drb/drb'
2
+ require_relative './rack_app'
2
3
  require 'drb/websocket'
3
4
  require 'thread'
4
5
  require 'rack'
@@ -46,12 +47,16 @@ module DRb
46
47
  @uri = uri
47
48
  @config = config
48
49
  @queue = Thread::Queue.new
49
- setup_websock(uri)
50
+
51
+ Faye::WebSocket.load_adapter('thin')
52
+
53
+ u = URI.parse(uri)
54
+ RackApp.register("#{u.host}:#{u.port}", self)
50
55
  end
51
56
 
52
57
  def close
53
- @ws.close if @ws
54
- @ws = nil
58
+ u = URI.parse(@uri)
59
+ RackApp.close("#{u.host}:#{u.port}")
55
60
  end
56
61
 
57
62
  def push(callback)
@@ -63,40 +68,9 @@ module DRb
63
68
  ServerSide.new(client, @config, uri)
64
69
  end
65
70
 
66
- def setup_websock(uri)
67
- Faye::WebSocket.load_adapter('thin')
68
- thin = Rack::Handler.get('thin')
69
-
70
- u = URI.parse(uri)
71
-
72
- app = lambda do |env|
73
- if Faye::WebSocket.websocket?(env)
74
- ws = Faye::WebSocket.new(env)
75
-
76
- ws.on :message do |event|
77
- callback = Callback.new(self)
78
- @queue.push(callback)
79
- res = callback.recv_mesg(event.data.pack('C*'))
80
- ws.send res.bytes
81
- end
82
-
83
- ws.on :close do |event|
84
- ws = nil
85
- end
86
-
87
- @ws = ws
88
-
89
- # Return async Rack response
90
- ws.rack_response
91
- else
92
- # Normal HTTP request
93
- [400, {}, []]
94
- end
95
- end
96
-
97
- Thread.new do
98
- thin.run(app, Host: u.host, Port: u.port)
99
- end.run
71
+ def on_message(data)
72
+ callback = Callback.new(self)
73
+ callback.recv_mesg(data.pack('C*'))
100
74
  end
101
75
  end
102
76
 
@@ -1,5 +1,5 @@
1
1
  module Drb
2
2
  module Websocket
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.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.1.0
4
+ version: 0.2.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-07-24 00:00:00.000000000 Z
11
+ date: 2017-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket
@@ -115,6 +115,7 @@ files:
115
115
  - examples/client.rb
116
116
  - examples/server.rb
117
117
  - lib/drb/websocket.rb
118
+ - lib/drb/websocket/rack_app.rb
118
119
  - lib/drb/websocket/server.rb
119
120
  - lib/drb/websocket/version.rb
120
121
  homepage: https://github.com/youchan/drb-websocket