r4s 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.md +6 -3
  2. data/lib/r4s.rb +18 -12
  3. data/lib/r4s/version.rb +1 -1
  4. metadata +2 -2
data/README.md CHANGED
@@ -23,6 +23,9 @@ If you make a connection and then close the browser the srv will crash stating t
23
23
  who, why, where or how these headers are sent are a mistery to me. My solution was to remove the turbolinks.
24
24
  This might be fixed in the future.
25
25
 
26
+ You need to enable threads in your config and your server needs to
27
+ support threads.
28
+
26
29
  ## Installation
27
30
 
28
31
  Add this line to your application's Gemfile:
@@ -45,7 +48,7 @@ R4S uses only two functions
45
48
 
46
49
  One to create the stream(s)
47
50
 
48
- R4S.add_stream(response,"key").start
51
+ R4S.add_stream(response,session,"key").start
49
52
 
50
53
  One to push to the stream(s)
51
54
 
@@ -57,7 +60,7 @@ I created a "stream" controller to handle my streams
57
60
  class StreamController < ApplicationController
58
61
  include ActionController::Live
59
62
  def stream1
60
- R4S.add_stream(response,"stream.key").start
63
+ R4S.add_stream(response,session,"stream.key").start
61
64
  end
62
65
  end
63
66
 
@@ -80,7 +83,7 @@ Create a action that triggers the event.
80
83
 
81
84
  class HomeController < ApplicationController
82
85
  def index
83
- R4S.push_data('kanban',{:time=>Time.now},:event=>"refresh")
86
+ R4S.push_data('stream.key',{:time=>Time.now},:event=>"refresh")
84
87
  render :json => {:success=>true}
85
88
  end
86
89
 
data/lib/r4s.rb CHANGED
@@ -1,22 +1,25 @@
1
1
  require 'thread_safe'
2
2
  require 'json'
3
- class R4S
3
+ module R4S
4
4
  SSES = ThreadSafe::Hash.new
5
5
 
6
- def self.add_stream(response,key="none")
6
+ #The session_id is used to identify users who already have a connection
7
+ #The key is used to group "identical" sse-streams so you can push data to a group of streams
8
+ def self.add_stream(response,session,key="none")
7
9
 
8
- if !R4S::SSES.keys.include?(key)
9
- R4S::SSES[key] = ThreadSafe::Hash.new
10
- key_count = 0
11
- else
12
- key_count = R4S::SSES[key].keys.count
10
+ id = session["session_id"]
11
+
12
+ R4S::SSES[key] = ThreadSafe::Hash.new unless R4S::SSES.keys.include?(key)
13
+
14
+ if R4S::SSES[key].keys.include?(id)
15
+ sse = R4S::SSES[key][id]
16
+ sse.close
13
17
  end
14
18
 
15
- sse = R4S::SSE.new(response,key,key_count)
16
- R4S::SSES[key][key_count] = sse
19
+ sse = R4S::SSE.new(response,key,id)
20
+ R4S::SSES[key][id] = sse
17
21
 
18
22
  return sse
19
-
20
23
  end
21
24
 
22
25
  def self.push_data(key,data,options={})
@@ -51,8 +54,10 @@ class R4S
51
54
  end
52
55
  rescue
53
56
  ensure
54
- R4S::SSES[@key][@id].close
55
- R4S::SSES[@key].delete(@id)
57
+ #it might have been reopened
58
+ unless !R4S::SSES[@key][@id].closed?
59
+ R4S::SSES[@key].delete(@id)
60
+ end
56
61
  end
57
62
  end
58
63
 
@@ -64,4 +69,5 @@ class R4S
64
69
  @io.close
65
70
  end
66
71
  end
72
+
67
73
  end
@@ -1,3 +1,3 @@
1
1
  module R4s
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r4s
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thread_safe