async-websocket 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/async-websocket.gemspec +1 -1
- data/examples/chat/config.ru +37 -6
- data/lib/async/websocket/connection.rb +1 -0
- data/lib/async/websocket/server.rb +16 -9
- data/lib/async/websocket/version.rb +1 -1
- data/spec/async/websocket/connection_spec.rb +2 -2
- data/spec/async/websocket/connection_spec.ru +3 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab8ffe6c4116453e8cf34e3b5efa75bbfc2b13323e11faa1de9c09874ade462
|
4
|
+
data.tar.gz: 35c3ee491b842ab6cfd9636b922deb2e099fbb7988043c715b106b19247de283
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3438df6dc250dcde07b786e8ddbc5821385db0dd8559887c514e0030dbdd6a9d5c99cbaaf29bf36f9ab4747b8a364cbca007375f7e3d48a963da235aa1774052
|
7
|
+
data.tar.gz: af3f2899183dd62ad36e3d74ed27796dc95eafdc6b6bf9680c8f35b81a7eb63c40a5c8bdfaee067a961066cefade12730ba6780aa06f2e8ec2d8fc1d600f8161
|
data/Gemfile
CHANGED
data/async-websocket.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_dependency "async-io"
|
21
21
|
|
22
22
|
spec.add_development_dependency "async-rspec"
|
23
|
-
spec.add_development_dependency "falcon", "~> 0.
|
23
|
+
spec.add_development_dependency "falcon", "~> 0.17.0"
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.6"
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.6"
|
data/examples/chat/config.ru
CHANGED
@@ -2,16 +2,47 @@
|
|
2
2
|
|
3
3
|
require 'async/websocket/server'
|
4
4
|
|
5
|
-
|
5
|
+
require 'async/actor'
|
6
|
+
require 'set'
|
7
|
+
|
8
|
+
bus = Async::Actor::Bus::Redis.new
|
9
|
+
|
10
|
+
class Room
|
11
|
+
def initialize
|
12
|
+
@connections = Set.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def connect connection
|
16
|
+
@connections << connection
|
17
|
+
end
|
18
|
+
|
19
|
+
def disconnect connection
|
20
|
+
@connections.delete(connection)
|
21
|
+
end
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
@connections.each(&block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
bus.supervise(:room) do
|
29
|
+
Room.new
|
30
|
+
end
|
6
31
|
|
7
32
|
run lambda {|env|
|
33
|
+
room = bus[:room]
|
34
|
+
|
8
35
|
Async::WebSocket::Server.open(env) do |connection|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
connection
|
36
|
+
begin
|
37
|
+
room.connect(connection)
|
38
|
+
|
39
|
+
while message = connection.next_message
|
40
|
+
room.each do |connection|
|
41
|
+
connection.send_message(message)
|
42
|
+
end
|
14
43
|
end
|
44
|
+
rescue
|
45
|
+
room.disconnect(connection)
|
15
46
|
end
|
16
47
|
end
|
17
48
|
|
@@ -35,21 +35,28 @@ module Async
|
|
35
35
|
attr :env
|
36
36
|
attr :url
|
37
37
|
|
38
|
+
HIJACK_RESPONSE = [-1, {}, []].freeze
|
39
|
+
|
38
40
|
def self.open(env)
|
39
41
|
if ::WebSocket::Driver.websocket?(env)
|
42
|
+
return nil unless env['rack.hijack?']
|
43
|
+
|
40
44
|
# https://github.com/rack/rack/blob/master/SPEC#L89-L93
|
41
|
-
peer =
|
45
|
+
peer = Async::IO.try_convert(
|
46
|
+
env['rack.hijack'].call
|
47
|
+
)
|
42
48
|
|
43
49
|
connection = self.new(env, peer)
|
44
50
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
return connection unless block_given?
|
52
|
+
|
53
|
+
begin
|
54
|
+
yield(connection)
|
55
|
+
|
56
|
+
return HIJACK_RESPONSE
|
57
|
+
ensure
|
58
|
+
connection.close
|
59
|
+
peer.close
|
53
60
|
end
|
54
61
|
end
|
55
62
|
end
|
@@ -25,12 +25,12 @@ require 'rack/test'
|
|
25
25
|
require 'falcon/server'
|
26
26
|
require 'falcon/adapters/rack'
|
27
27
|
|
28
|
-
RSpec.describe Async::WebSocket::Connection do
|
28
|
+
RSpec.describe Async::WebSocket::Connection, timeout: 5 do
|
29
29
|
include_context Async::RSpec::Reactor
|
30
30
|
|
31
31
|
let(:server_address) {Async::IO::Endpoint.tcp('0.0.0.0', 9000)}
|
32
32
|
let(:app) {Rack::Builder.parse_file(File.expand_path('../connection_spec.ru', __FILE__)).first}
|
33
|
-
let(:server) {Falcon::Server.new(Falcon::
|
33
|
+
let(:server) {Falcon::Server.new(Falcon::Server.middleware(app, verbose: true), server_address)}
|
34
34
|
|
35
35
|
it "should connect to the websocket server" do
|
36
36
|
server_task = reactor.async do
|
@@ -7,15 +7,16 @@ class Upgrade
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def call(env)
|
10
|
-
Async::WebSocket::Server.open(env) do |server|
|
10
|
+
result = Async::WebSocket::Server.open(env) do |server|
|
11
11
|
read, write = IO.pipe
|
12
12
|
|
13
13
|
Process.spawn("ls -lah", :out => write)
|
14
14
|
write.close
|
15
15
|
|
16
16
|
read.each_line do |line|
|
17
|
-
server.
|
17
|
+
server.send_text(line)
|
18
18
|
end
|
19
|
+
|
19
20
|
end or @app.call(env)
|
20
21
|
end
|
21
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: websocket-driver
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 0.17.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 0.17.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: bundler
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|