sinatra-multi-screen 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === 0.0.6 2013-02-27
2
+
3
+ * Server push to TV or Remote
4
+
1
5
  === 0.0.5 2013-02-25
2
6
 
3
7
  * echo back UI Event on TV
data/README.md CHANGED
@@ -116,6 +116,25 @@ MultiScreen.on :ui_event do |data, from|
116
116
  end
117
117
  ```
118
118
 
119
+
120
+ ### Server ---(push event)--> TV or Remote
121
+
122
+ Sinatra Side
123
+ ```ruby
124
+ data = {:message => 'hello!!'}
125
+ MultiScreen.push :foo, data # to all TV and Remote
126
+ MultiScreen.push :foo, data, {:type => "tv"} # to all TV
127
+ MultiScreen.push :foo, data, {:type => "remote", :channel => "1"}
128
+ ```
129
+
130
+ TV or Remote
131
+ ```javascript
132
+ screen.on("foo", function(data){
133
+ alert(data.message);
134
+ });
135
+ ```
136
+
137
+
119
138
  Samples
120
139
  -------
121
140
  * https://github.com/shokai/sinatra-multi-screen/tree/master/sample
@@ -9,11 +9,15 @@ var MultiScreen = function(cometio, options){
9
9
  options.channel = "__default__channel__";
10
10
  }
11
11
  var self = this;
12
+ new EventEmitter().apply(this);
12
13
  this.io = cometio;
13
14
  this.options = options;
14
15
  this.io.on("connect", function(session){
15
16
  self.io.push("__multiscreen__options", self.options);
16
17
  });
18
+ this.io.on("__multiscreen__server", function(data){
19
+ self.emit(data.event, data.data);
20
+ });
17
21
 
18
22
  var to;
19
23
  if(options.type === 'remote') to = 'tv';
@@ -24,7 +28,8 @@ var MultiScreen = function(cometio, options){
24
28
  this[to] = {};
25
29
  new EventEmitter().apply(this[to]);
26
30
  this[to].push = function(event_name, data){
27
- self.io.push("__multiscreen__data", {event: event_name, data: data, options: self.options});
31
+ self.io.push("__multiscreen__data",
32
+ {event: event_name, data: data, options: self.options});
28
33
  };
29
34
  this.io.on("__multiscreen__data", function(data){
30
35
  self[to].emit(data.event, data.data);
@@ -1,3 +1,3 @@
1
1
  module SinatraMultiScreen
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -13,6 +13,26 @@ class MultiScreen
13
13
  }
14
14
  end
15
15
 
16
+ def self.push(event, data, opts={})
17
+ ids = nil
18
+ channel = opts[:channel].to_s
19
+ type = opts[:type].to_s
20
+ if !channel.empty? and !type.empty?
21
+ ids = channels[channel][type] or []
22
+ elsif !channel.empty?
23
+ ids = channels[channel].values.flatten or []
24
+ elsif !type.empty?
25
+ ids = channels.values.map{|i| i[type] }.flatten or []
26
+ else
27
+ ids = channels.values.map{|i| i.values }.flatten or []
28
+ end
29
+ ids.each do |session_id|
30
+ CometIO.push(:__multiscreen__server,
31
+ {:event => event, :data => data},
32
+ {:to => session_id})
33
+ end
34
+ end
35
+
16
36
  end
17
37
 
18
38
  EventEmitter.apply MultiScreen
@@ -1,7 +1,7 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- backports (2.8.2)
4
+ backports (3.0.3)
5
5
  daemons (1.1.9)
6
6
  event_emitter (0.2.2)
7
7
  eventmachine (1.0.0)
@@ -16,7 +16,7 @@ GEM
16
16
  rack-test (0.6.2)
17
17
  rack (>= 1.0)
18
18
  sass (3.2.6)
19
- sinatra (1.3.4)
19
+ sinatra (1.3.5)
20
20
  rack (~> 1.4)
21
21
  rack-protection (~> 1.3)
22
22
  tilt (~> 1.3, >= 1.3.3)
@@ -11,6 +11,11 @@ end
11
11
  MultiScreen.on :connect do |client|
12
12
  puts "new client #{client.inspect}"
13
13
  p MultiScreen.channels
14
+ to = case client[:type]
15
+ when "tv" then "remote"
16
+ when "remote" then "tv"
17
+ end
18
+ MultiScreen.push :new_client, client, {:type => to, :channel => client[:channel]}
14
19
  end
15
20
 
16
21
  MultiScreen.on :disconnect do |client|
@@ -12,6 +12,11 @@ tv.on("ui_event", function(data){
12
12
  $("#message").text(data.event+' was dispatched on TV-side '+data.selector);
13
13
  });
14
14
 
15
+ // push from server
16
+ screen.on("new_client", function(e){
17
+ $("#message").text("new TV <"+e.session+"> available.");
18
+ });
19
+
15
20
  $(function(){
16
21
  generate_code();
17
22
  $("select").change(generate_code);
@@ -12,6 +12,11 @@ remote.on("ui_event", function(data){
12
12
  $("#message").text(JSON.stringify(data));
13
13
  });
14
14
 
15
+ // push from server
16
+ screen.on("new_client", function(e){
17
+ $("#message").text("new Remote <"+e.session+"> available.");
18
+ });
19
+
15
20
  $(function(){
16
21
  $(".btn").click(function(e){
17
22
  alert(e.currentTarget.innerText);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-multi-screen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2013-02-25 00:00:00.000000000 Z
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra