rasti-web 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rasti/web/channel.rb +24 -25
- data/lib/rasti/web/stream.rb +2 -2
- data/lib/rasti/web/version.rb +1 -1
- data/lib/rasti/web.rb +2 -2
- data/rasti-web.gemspec +1 -1
- data/spec/minitest_helper.rb +4 -0
- data/spec/streaming_spec.rb +11 -7
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9172df969941d8ed2283476d1dc516f664db5f5d
|
4
|
+
data.tar.gz: 241e91821c0b1ebcea756210a53371c7ee66da7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04d240bc622d210776cbab344355d7bc076adf3958a20eea4a7b28b3e2bad1d0bf629986e34fa32565b60263eaf6b74356e2b60307c419a50e9014d87d1be95b
|
7
|
+
data.tar.gz: dce35f9da9aee7a7de97881601301e6b44f71ab7710ec424fa691e9889591f2578e3bbb6ab02a692e4550791fdc9233c5430ad580d5f0c9dfcdff91e00aa1228
|
data/lib/rasti/web/channel.rb
CHANGED
@@ -1,26 +1,39 @@
|
|
1
1
|
module Rasti
|
2
2
|
module Web
|
3
|
+
|
3
4
|
class Channel
|
4
5
|
|
5
|
-
attr_reader :
|
6
|
+
attr_reader :id
|
6
7
|
|
7
8
|
def initialize(id)
|
9
|
+
@id = id
|
10
|
+
@subscriptions = {}
|
8
11
|
@mutex = Mutex.new
|
9
|
-
@channel = Restruct::Channel.new id: Restruct::Id[Web.channels_prefix][id]
|
10
|
-
@streams = []
|
11
|
-
listen
|
12
12
|
end
|
13
13
|
|
14
14
|
def subscribe
|
15
|
-
Stream.new
|
16
|
-
|
17
|
-
|
15
|
+
stream = Stream.new
|
16
|
+
|
17
|
+
subscription_id = broadcaster.subscribe id do |message|
|
18
|
+
if stream.open?
|
19
|
+
stream.write message
|
20
|
+
else
|
21
|
+
sid = mutex.synchronize { subscriptions.delete stream }
|
22
|
+
broadcaster.unsubscribe sid
|
18
23
|
end
|
19
24
|
end
|
25
|
+
|
26
|
+
mutex.synchronize { subscriptions[stream] = subscription_id }
|
27
|
+
|
28
|
+
stream
|
20
29
|
end
|
21
30
|
|
22
31
|
def publish(message)
|
23
|
-
|
32
|
+
broadcaster.publish id, message
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.broadcaster
|
36
|
+
@broadcaster ||= Broadcaster.new id: Web.channels_prefix
|
24
37
|
end
|
25
38
|
|
26
39
|
def self.[](id)
|
@@ -30,26 +43,12 @@ module Rasti
|
|
30
43
|
|
31
44
|
private
|
32
45
|
|
33
|
-
attr_reader :
|
46
|
+
attr_reader :subscriptions, :mutex
|
34
47
|
|
35
|
-
def
|
36
|
-
|
37
|
-
channel.subscribe do |message|
|
38
|
-
broadcast message
|
39
|
-
end
|
40
|
-
end
|
48
|
+
def broadcaster
|
49
|
+
self.class.broadcaster
|
41
50
|
end
|
42
51
|
|
43
|
-
def broadcast(message)
|
44
|
-
mutex.synchronize do
|
45
|
-
streams.delete_if(&:closed?)
|
46
|
-
Rasti::Web.logger.debug(Channel) { "Broadcasting (#{streams.count} connections) -> #{message}" } unless streams.empty?
|
47
|
-
streams.each do |stream|
|
48
|
-
stream.write message
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
52
|
end
|
54
53
|
end
|
55
54
|
end
|
data/lib/rasti/web/stream.rb
CHANGED
data/lib/rasti/web/version.rb
CHANGED
data/lib/rasti/web.rb
CHANGED
@@ -7,7 +7,7 @@ require 'class_config'
|
|
7
7
|
require 'forwardable'
|
8
8
|
require 'logger'
|
9
9
|
require 'hash_ext'
|
10
|
-
require '
|
10
|
+
require 'broadcaster'
|
11
11
|
|
12
12
|
require_relative 'web/route'
|
13
13
|
require_relative 'web/router'
|
@@ -34,7 +34,7 @@ module Rasti
|
|
34
34
|
attr_config :default_layout, 'layout'
|
35
35
|
attr_config :helpers, []
|
36
36
|
attr_config :logger, Logger.new(STDOUT)
|
37
|
-
attr_config :channels_prefix,
|
37
|
+
attr_config :channels_prefix, 'rasti-web:channels'
|
38
38
|
|
39
39
|
after_config do |config|
|
40
40
|
config.helpers.each { |h| ViewContext.send :include, h }
|
data/rasti-web.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'class_config', '~> 0.0.2'
|
25
25
|
spec.add_dependency 'hash_ext', '~> 0.1'
|
26
26
|
spec.add_dependency 'content-type', '~> 0.0'
|
27
|
-
spec.add_dependency '
|
27
|
+
spec.add_dependency 'broadcaster', '~> 0.1'
|
28
28
|
|
29
29
|
spec.add_development_dependency 'bundler', '~> 1.12'
|
30
30
|
spec.add_development_dependency 'rake', '~> 11.0'
|
data/spec/minitest_helper.rb
CHANGED
@@ -15,4 +15,8 @@ Rasti::Web.configure do |config|
|
|
15
15
|
config.views_path = File.expand_path '../views', __FILE__
|
16
16
|
config.helpers << ContextMethodHelper
|
17
17
|
config.logger.level = Logger::ERROR
|
18
|
+
end
|
19
|
+
|
20
|
+
Broadcaster.configure do |config|
|
21
|
+
config.logger = Rasti::Web.logger
|
18
22
|
end
|
data/spec/streaming_spec.rb
CHANGED
@@ -5,6 +5,14 @@ describe 'Straming' do
|
|
5
5
|
let(:request) { Rack::Request.new Hash.new }
|
6
6
|
let(:response) { Rack::Response.new }
|
7
7
|
let(:render) { Rasti::Web::Render.new request, response }
|
8
|
+
|
9
|
+
def wait_for(&block)
|
10
|
+
Timeout.timeout(3) do
|
11
|
+
while !block.call
|
12
|
+
sleep 0.0001
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
8
16
|
|
9
17
|
it 'Server sent events' do
|
10
18
|
render.server_sent_events :channel_1
|
@@ -16,13 +24,13 @@ describe 'Straming' do
|
|
16
24
|
response.body.must_be_instance_of Rasti::Web::Stream
|
17
25
|
|
18
26
|
events = []
|
19
|
-
|
27
|
+
|
28
|
+
Thread.new do
|
20
29
|
response.body.each { |e| events << e }
|
21
30
|
end
|
22
31
|
|
23
32
|
channel_1 = Rasti::Web::Channel[:channel_1]
|
24
33
|
channel_2 = Rasti::Web::Channel[:channel_2]
|
25
|
-
sleep 0.05 # Wait for establish connection
|
26
34
|
|
27
35
|
3.times do |i|
|
28
36
|
data = {text: "Tick #{i}"}
|
@@ -31,11 +39,7 @@ describe 'Straming' do
|
|
31
39
|
channel_2.publish event
|
32
40
|
end
|
33
41
|
|
34
|
-
|
35
|
-
while events.count < 3;
|
36
|
-
sleep 0.0001 # Wait for subscriptions
|
37
|
-
end
|
38
|
-
end
|
42
|
+
wait_for { events.count == 3 }
|
39
43
|
|
40
44
|
response.body.close
|
41
45
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rasti-web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -95,19 +95,19 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: broadcaster
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0.
|
103
|
+
version: '0.1'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0.
|
110
|
+
version: '0.1'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: bundler
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|