async_cable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +106 -0
- data/Rakefile +10 -0
- data/async_cable.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/async_cable.rb +24 -0
- data/lib/async_cable/config.rb +15 -0
- data/lib/async_cable/connection.rb +123 -0
- data/lib/async_cable/errors.rb +25 -0
- data/lib/async_cable/registry.rb +62 -0
- data/lib/async_cable/server.rb +50 -0
- data/lib/async_cable/version.rb +3 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a73611b6db5ff3e6f0377115d65a02dc9ce18f5
|
4
|
+
data.tar.gz: a8982f7401992a3ef2ec0dd6bbb6f10309161682
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93f951f2178542ba41962a862e59f1ab0fefffdb2528ffbf785923e654363dd78ae550aff6872c3f525cdaf0706f1666b5a295d14a5445884c9512abd2247ade
|
7
|
+
data.tar.gz: 598bd856f068b973db1339651e39089c08b28029734816bf3d13e7060cee3c868655832c682758e2cfb3ab2ff274a20177c00c5919818e70f99d06b291011960
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at senid231@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Denis Talakevich
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# AsyncCable
|
2
|
+
|
3
|
+
Very simple but functional websocket server for Rack async application.
|
4
|
+
|
5
|
+
* Works on Fibers via [async](https://github.com/socketry/async).
|
6
|
+
* Threadsafe
|
7
|
+
* Supports broadcasting.
|
8
|
+
* Works with [Falcon web server](https://github.com/socketry/falcon).
|
9
|
+
* Supports authorization with cookies/header.
|
10
|
+
|
11
|
+
Intended to use with [Falcon web server](https://github.com/socketry/falcon) or other web server based on.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'async_cable'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install async_cable
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# config.ru
|
33
|
+
require_relative 'lib/main_cable'
|
34
|
+
app = RackBuilder.new do
|
35
|
+
use Rack::Session::Cookie, key: 'test.app', secret: ENV['RACK_SECRET']
|
36
|
+
map '/cable' do
|
37
|
+
run AsyncCable::Server.new(connection_class: MainCable)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
run app
|
41
|
+
|
42
|
+
|
43
|
+
# lib/main_cable.rb
|
44
|
+
class ChatCable < AsyncCable::Connection
|
45
|
+
identified_as :main
|
46
|
+
attr_reader :current_user
|
47
|
+
|
48
|
+
def on_open
|
49
|
+
@current_user = User.find_by id: request.session['user_id']
|
50
|
+
reject_unauthorized if current_user.nil?
|
51
|
+
stream_for request.params['room_name']
|
52
|
+
|
53
|
+
logger.info { "User##{current_user.id} has joined to #{channel_name}/#{stream_name}." }
|
54
|
+
self.class.broadcast(stream_name, message: "#{current_user.username} has joined.")
|
55
|
+
transmit(message: "Welcome #{current_user.username}.")
|
56
|
+
end
|
57
|
+
|
58
|
+
def on_data(data)
|
59
|
+
self.class.broadcast(stream_name, message: data[:message].to_s, by_who: current_user.username)
|
60
|
+
end
|
61
|
+
|
62
|
+
def on_close
|
63
|
+
return if identifier.nil?
|
64
|
+
|
65
|
+
logger.info { "User##{current_user.id} has left #{channel_name}/#{stream_name}." }
|
66
|
+
self.class.broadcast(stream_name, message: "#{current_user.username} has left.")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
example of JS code for connecting with websocket server
|
72
|
+
```js
|
73
|
+
var socket = new WebSocket("ws://localhost:9292/cable");
|
74
|
+
socket.onopen = function(_event) {
|
75
|
+
console.log("WebSocket connected");
|
76
|
+
};
|
77
|
+
socket.onerror = function(error) {
|
78
|
+
console.log("WebSocket error", error);
|
79
|
+
};
|
80
|
+
socket.onclose = function(event) {
|
81
|
+
console.log("WebSocket closed", event.wasClean, event.code, event.reason);
|
82
|
+
};
|
83
|
+
socket.onmessage = function(event) {
|
84
|
+
console.log("WebSocket data received", JSON.parse(event.data));
|
85
|
+
};
|
86
|
+
|
87
|
+
var transmit = function (data) {
|
88
|
+
socket.send( JSON.generate(data) );
|
89
|
+
};
|
90
|
+
var close = function (code, reason) {
|
91
|
+
socket.close(code || 1000, reason);
|
92
|
+
};
|
93
|
+
```
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/senid231/async_cable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/async_cable/blob/master/CODE_OF_CONDUCT.md).
|
98
|
+
|
99
|
+
|
100
|
+
## License
|
101
|
+
|
102
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
103
|
+
|
104
|
+
## Code of Conduct
|
105
|
+
|
106
|
+
Everyone interacting in the AsyncCable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/async_cable/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/async_cable.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'lib/async_cable/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'async_cable'
|
5
|
+
spec.version = AsyncCable::VERSION
|
6
|
+
spec.authors = ['Denis Talakevich']
|
7
|
+
spec.email = ['senid231@gmail.com']
|
8
|
+
|
9
|
+
spec.summary = 'Dead simple websocket middleware for Rack async app.'
|
10
|
+
spec.description = 'Dead simple websocket middleware for Rack async app.'
|
11
|
+
spec.homepage = 'https://github.com/senid231/async_cable'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
|
+
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
17
|
+
spec.metadata['changelog_uri'] = File.join(spec.homepage, 'blob/master/CHANGELOG.md')
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_dependency 'async-websocket'
|
29
|
+
spec.add_dependency 'activesupport'
|
30
|
+
spec.add_dependency 'anyway_config'
|
31
|
+
spec.add_dependency 'null-logger'
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "async_cable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/async_cable.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'async_cable/version'
|
2
|
+
require 'async_cable/errors'
|
3
|
+
require 'async_cable/config'
|
4
|
+
require 'async_cable/registry'
|
5
|
+
require 'async_cable/connection'
|
6
|
+
require 'async_cable/server'
|
7
|
+
|
8
|
+
module AsyncCable
|
9
|
+
def config
|
10
|
+
@config ||= Config.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def configure
|
14
|
+
yield config
|
15
|
+
end
|
16
|
+
|
17
|
+
def broadcast(data)
|
18
|
+
config.logger.debug { "#{name}.broadcast data=#{data.inspect}" }
|
19
|
+
Registry.find.each { |conn| conn.transmit(data) }
|
20
|
+
end
|
21
|
+
|
22
|
+
module_function :configure, :config, :broadcast
|
23
|
+
config # initialize config right away to prevent racing.
|
24
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'async/websocket/connection'
|
2
|
+
require 'protocol/websocket/error'
|
3
|
+
|
4
|
+
module AsyncCable
|
5
|
+
class Connection < Async::WebSocket::Connection
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def inherited(subclass)
|
9
|
+
subclass.identified_as subclass.name.demodulize.underscore
|
10
|
+
end
|
11
|
+
|
12
|
+
def identified_as(channel)
|
13
|
+
@channel_name = channel.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def channel_name
|
17
|
+
@channel_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def logger
|
21
|
+
AsyncCable.config.logger
|
22
|
+
end
|
23
|
+
|
24
|
+
# Transmit data to all WS connections in current channel and provided stream.
|
25
|
+
# @param data [Hash]
|
26
|
+
def broadcast(stream_name, data)
|
27
|
+
logger.debug { "#{name}.broadcast data=#{data.inspect}" }
|
28
|
+
|
29
|
+
Registry.find(channel_name, stream_name).each do |conn|
|
30
|
+
conn.transmit(data) unless conn.closed?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_reader :request
|
36
|
+
attr_writer :close_code
|
37
|
+
attr_accessor :close_reason
|
38
|
+
|
39
|
+
def initialize(*args, &block)
|
40
|
+
super
|
41
|
+
@mutex = Mutex.new
|
42
|
+
end
|
43
|
+
|
44
|
+
# Will be executed when WS connection opened.
|
45
|
+
# #stream_for must be called here with stream name
|
46
|
+
def on_open
|
47
|
+
end
|
48
|
+
|
49
|
+
# Will be executed when data received from WS client.
|
50
|
+
# @param data [Hash]
|
51
|
+
def on_data(data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Will be executed when WS connection closed.
|
55
|
+
# see #close_code, #close_reason for details.
|
56
|
+
def on_close
|
57
|
+
end
|
58
|
+
|
59
|
+
# call this method to transmit data to current WS client
|
60
|
+
# @param data [Hash]
|
61
|
+
def transmit(data)
|
62
|
+
logger.debug { "#{self.class}#transmit identifier=#{identifier} data=#{data.inspect}" }
|
63
|
+
|
64
|
+
@mutex.synchronize do
|
65
|
+
write(data)
|
66
|
+
flush
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# @param stream_name [String]
|
71
|
+
def stream_for(stream_name)
|
72
|
+
@stream_name = stream_name
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return [String] stream name
|
76
|
+
def stream_name
|
77
|
+
@stream_name
|
78
|
+
end
|
79
|
+
|
80
|
+
def reject_unauthorized(reason = nil)
|
81
|
+
raise UnauthorizedError, reason
|
82
|
+
end
|
83
|
+
|
84
|
+
# WS connection close code
|
85
|
+
# 1000 - clean close
|
86
|
+
# @return [Integer]
|
87
|
+
def close_code
|
88
|
+
@close_code || Protocol::WebSocket::Error::NO_ERROR
|
89
|
+
end
|
90
|
+
|
91
|
+
# Was WS connection closed clean or dirty.
|
92
|
+
# @return [Boolean]
|
93
|
+
def close_clean?
|
94
|
+
close_code == Protocol::WebSocket::Error::NO_ERROR
|
95
|
+
end
|
96
|
+
|
97
|
+
# @param env [Hash]
|
98
|
+
# @raise [AsyncCable::Errors::StreamNameNotSet] if #stream_for was not called
|
99
|
+
# @raise [AsyncCable::Errors::UnauthorizedError] if #reject_unauthorized was called
|
100
|
+
def handle_open(env)
|
101
|
+
logger.debug { "#{self.class}#handle_open" }
|
102
|
+
@request = Rack::Request.new(env)
|
103
|
+
on_open
|
104
|
+
raise Errors::StreamNameNotSet, self.class.name unless defined?(@stream_name)
|
105
|
+
Registry.add(channel_name, stream_name, self)
|
106
|
+
end
|
107
|
+
|
108
|
+
def handle_close
|
109
|
+
logger.debug { "#{self.class}#handle_close clean=#{close_clean?} code=#{close_code} reason=#{close_reason}" }
|
110
|
+
close
|
111
|
+
Registry.remove(channel_name, stream_name, self)
|
112
|
+
on_close
|
113
|
+
end
|
114
|
+
|
115
|
+
def channel_name
|
116
|
+
self.class.channel_name
|
117
|
+
end
|
118
|
+
|
119
|
+
def logger
|
120
|
+
self.class.logger
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AsyncCable
|
2
|
+
module Errors
|
3
|
+
class Error < StandardError
|
4
|
+
def code
|
5
|
+
1999
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class StreamNameNotSet < Error
|
10
|
+
def initialize(class_name)
|
11
|
+
super("#stream_for must be called with stream name in #{class_name}#on_open")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class UnauthorizedError < Error
|
16
|
+
def initialize(reason = nil)
|
17
|
+
super(reason || 'unauthorized')
|
18
|
+
end
|
19
|
+
|
20
|
+
def code
|
21
|
+
1401
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module AsyncCable
|
5
|
+
class Registry
|
6
|
+
include Singleton
|
7
|
+
extend SingleForwardable
|
8
|
+
@mutex = Mutex.new
|
9
|
+
|
10
|
+
single_delegate [:add, :remove, :find, :all] => :instance
|
11
|
+
|
12
|
+
# Adds connection to registry.
|
13
|
+
# @param channel_name [String]
|
14
|
+
# @param stream_name [String]
|
15
|
+
# @param connection [AsyncCable::Connection]
|
16
|
+
def add(channel_name, stream_name, connection)
|
17
|
+
@mutex.synchronize do
|
18
|
+
subscribers[channel_name][stream_name].push(connection)
|
19
|
+
connection
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Removes connection from registry.
|
24
|
+
# @param channel_name [String]
|
25
|
+
# @param stream_name [String]
|
26
|
+
# @param connection [AsyncCable::Connection]
|
27
|
+
def remove(channel_name, stream_name, connection)
|
28
|
+
@mutex.synchronize do
|
29
|
+
subscribers[channel_name][stream_name].delete(connection)
|
30
|
+
subscribers[channel_name].delete(stream_name) if subscribers[channel_name][stream_name].empty?
|
31
|
+
connection
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Return all connections from all channels when `channel_name` omitted.
|
36
|
+
# Return all connections from channel when `stream_name` omitted.
|
37
|
+
# Return connections from channel stream when `channel_name` and `stream_name` provided.
|
38
|
+
# @param channel_name [String,NilClass]
|
39
|
+
# @param stream_name [String,NilClass]
|
40
|
+
# @return [Array<AsyncCable::Connection>,Array]
|
41
|
+
def find(channel_name = nil, stream_name = nil)
|
42
|
+
@mutex.synchronize do
|
43
|
+
return subscribers.values.map(&:values).flatten if channel_name.nil?
|
44
|
+
return subscribers[channel_name].values.flatten if stream_name.nil?
|
45
|
+
subscribers[channel_name][stream_name]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def subscribers
|
52
|
+
@subscribers ||= new_subscribers
|
53
|
+
end
|
54
|
+
|
55
|
+
def new_subscribers
|
56
|
+
Hash.new do |hash, channel_name|
|
57
|
+
hash[channel_name] = Hash.new { |h, stream_name| h[stream_name] = []; h }
|
58
|
+
hash
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'async/websocket'
|
2
|
+
require 'async/websocket/adapters/rack'
|
3
|
+
|
4
|
+
module AsyncCable
|
5
|
+
class Server
|
6
|
+
# Rack application should be used inside Async::Reactor loop.
|
7
|
+
|
8
|
+
attr_reader :connection_class
|
9
|
+
|
10
|
+
# @param connection_class [Class] subclass of AsyncCable::Connection.
|
11
|
+
# @param block [Proc<Hash>] yields when not valid WS request.
|
12
|
+
# @yieldreturn [Array] `[status,headers,body]`
|
13
|
+
def initialize(connection_class:, &block)
|
14
|
+
@connection_class = connection_class
|
15
|
+
@block = block
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param env [Hash]
|
19
|
+
# @return [Array] `[status,headers,body]`
|
20
|
+
def call(env)
|
21
|
+
response = Async::WebSocket::Adapters::Rack.open(env, handler: connection_class) do |connection|
|
22
|
+
connection.handle_open(env)
|
23
|
+
|
24
|
+
while (data = connection.read)
|
25
|
+
connection.on_data(data)
|
26
|
+
end
|
27
|
+
rescue Protocol::WebSocket::ProtocolError => error
|
28
|
+
logger.debug { "#{self.class}#call rescue #{error.class} message=#{error.message} code=#{error.code}" }
|
29
|
+
connection.close_code = error.code
|
30
|
+
connection.close_reason = error.message
|
31
|
+
rescue AsyncCable::Connection::Error => error
|
32
|
+
connection.close_code = error.code
|
33
|
+
connection.close_reason = error.message
|
34
|
+
ensure
|
35
|
+
logger.debug { "#{self.class}#call connection closed" }
|
36
|
+
connection.handle_close
|
37
|
+
end
|
38
|
+
# response[1] ca be Protocol::HTTP::Headers::Merged here.
|
39
|
+
# We transform it to hash because we don't want to break other middleware logic.
|
40
|
+
response[1] = response[1].to_a.to_h if !response.nil? && !response[1].is_a?(Hash)
|
41
|
+
response || [400, { 'Content-Type' => 'text/plain' }, ['Not valid ws']]
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def logger
|
47
|
+
AsyncCable.config.logger
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async_cable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Talakevich
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: async-websocket
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: anyway_config
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: null-logger
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Dead simple websocket middleware for Rack async app.
|
70
|
+
email:
|
71
|
+
- senid231@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- async_cable.gemspec
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/async_cable.rb
|
88
|
+
- lib/async_cable/config.rb
|
89
|
+
- lib/async_cable/connection.rb
|
90
|
+
- lib/async_cable/errors.rb
|
91
|
+
- lib/async_cable/registry.rb
|
92
|
+
- lib/async_cable/server.rb
|
93
|
+
- lib/async_cable/version.rb
|
94
|
+
homepage: https://github.com/senid231/async_cable
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
homepage_uri: https://github.com/senid231/async_cable
|
99
|
+
source_code_uri: https://github.com/senid231/async_cable
|
100
|
+
changelog_uri: https://github.com/senid231/async_cable/blob/master/CHANGELOG.md
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.3.0
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.8
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Dead simple websocket middleware for Rack async app.
|
121
|
+
test_files: []
|