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 +4 -4
- data/README.md +12 -2
- data/lib/drb/websocket/rack_app.rb +55 -0
- data/lib/drb/websocket/server.rb +11 -37
- data/lib/drb/websocket/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4af96ea554d51d5d2e744d6d49380487d5e3633
|
4
|
+
data.tar.gz: f1f445f420f670f8b6de1b42e39aab2e8c0b5343
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
20
|
+
|
21
|
+
bundle exec ruby -I. server.rb
|
13
22
|
|
14
23
|
run client
|
15
|
-
|
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
|
data/lib/drb/websocket/server.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
54
|
-
|
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
|
67
|
-
|
68
|
-
|
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
|
|
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.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-
|
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
|