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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c110468d58034b4bbdd08e4d39482bf24cf99207dba5f5fa478251c8751f3dbe
4
- data.tar.gz: cd992b36fb505345834c51ae31523e35e9058d846bd1b4a85ddf43143eebc17a
3
+ metadata.gz: bab8ffe6c4116453e8cf34e3b5efa75bbfc2b13323e11faa1de9c09874ade462
4
+ data.tar.gz: 35c3ee491b842ab6cfd9636b922deb2e099fbb7988043c715b106b19247de283
5
5
  SHA512:
6
- metadata.gz: 36c2509873572672d504f0de96785840b52d3993b3cc7febca16d9f08ed85f7e3e73c4995380aea74de3a2d431f4db772ed4c1b4bc268c698fea7ecf7445cf01
7
- data.tar.gz: d226a93fa0f972789b5e1f9d842fe0ec1dcffefff3b846b2e977555fc29cafa23ffe2c20782f28d39dbdea6d67bccce5fc00120e0829fe0876019f08963c39ba
6
+ metadata.gz: 3438df6dc250dcde07b786e8ddbc5821385db0dd8559887c514e0030dbdd6a9d5c99cbaaf29bf36f9ab4747b8a364cbca007375f7e3d48a963da235aa1774052
7
+ data.tar.gz: af3f2899183dd62ad36e3d74ed27796dc95eafdc6b6bf9680c8f35b81a7eb63c40a5c8bdfaee067a961066cefade12730ba6780aa06f2e8ec2d8fc1d600f8161
data/Gemfile CHANGED
@@ -1,6 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in utopia-websocket.gemspec
4
3
  gemspec
5
4
 
6
5
  group :test do
@@ -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.15.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"
@@ -2,16 +2,47 @@
2
2
 
3
3
  require 'async/websocket/server'
4
4
 
5
- $connections = []
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
- $connections << connection
10
-
11
- while message = connection.next_message
12
- $connections.each do |connection|
13
- connection.send_message(message)
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
 
@@ -95,6 +95,7 @@ module Async
95
95
 
96
96
  def close
97
97
  @driver.close
98
+ @socket.close
98
99
  end
99
100
  end
100
101
  end
@@ -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 = env['rack.hijack'].call
45
+ peer = Async::IO.try_convert(
46
+ env['rack.hijack'].call
47
+ )
42
48
 
43
49
  connection = self.new(env, peer)
44
50
 
45
- if block_given?
46
- begin
47
- yield(connection) || true
48
- ensure
49
- connection.close
50
- end
51
- else
52
- return connection
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
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module WebSocket
23
- VERSION = "0.5.0"
23
+ VERSION = "0.6.0"
24
24
  end
25
25
  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::Adapters::Rack.new(app), server_address)}
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.driver.text(line)
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.5.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-06-25 00:00:00.000000000 Z
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.15.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.15.0
68
+ version: 0.17.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement