sinatra-cometio 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/README.rdoc +61 -14
- data/Rakefile +1 -1
- data/lib/js/cometio.js +2 -0
- data/lib/sinatra-cometio.rb +1 -1
- data/sample/public/js/index.js +5 -1
- data/sample/views/index.haml +1 -1
- metadata +5 -5
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -12,38 +12,85 @@ Node.js like Comet I/O plugin for Sinatra
|
|
12
12
|
|
13
13
|
== SYNOPSIS:
|
14
14
|
|
15
|
-
|
15
|
+
=== Client --(Ajax)--> Server
|
16
|
+
|
17
|
+
Client Side
|
18
|
+
|
19
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
|
20
|
+
<script src="<%= cometio_js %>"></script>
|
21
|
+
|
22
|
+
var io = new CometIO().connect();
|
23
|
+
io.push("chat", {name: "shokai", message: "hello"}); // client -> server
|
24
|
+
|
25
|
+
Server Side
|
16
26
|
|
17
27
|
require 'sinatra'
|
18
28
|
require 'sinatra/cometio'
|
19
29
|
|
20
|
-
## echo
|
21
30
|
CometIO.on :chat do |data, session|
|
22
31
|
puts "#{data['name']} : #{data['message']} <#{session}>"
|
23
|
-
self.push :chat, data
|
24
32
|
end
|
33
|
+
## => "shokai : hello <12abcde345f6g7h8ijk>"
|
25
34
|
|
26
35
|
|
27
|
-
|
36
|
+
=== Server --(Comet)--> Client
|
28
37
|
|
29
|
-
|
30
|
-
<script src="<%= cometio_js %>"></script>
|
38
|
+
Server Side
|
31
39
|
|
40
|
+
CometIO.push :temperature, 35 # broadcast
|
41
|
+
CometIO.push :light, {:value => 150}, {:to => session_id} # to specific client
|
32
42
|
|
33
|
-
|
43
|
+
Client Side
|
34
44
|
|
35
|
-
|
45
|
+
io.on("temperature", function(value){
|
46
|
+
console.log("server temperature : " + value);
|
47
|
+
});
|
48
|
+
|
49
|
+
|
50
|
+
=== On "connect" Event
|
51
|
+
|
52
|
+
Client Side
|
53
|
+
|
54
|
+
io.on("connect",function(session){
|
55
|
+
alert("connect!!");
|
56
|
+
});
|
57
|
+
|
58
|
+
Server Side
|
59
|
+
|
60
|
+
CometIO.on :connect do |session|
|
61
|
+
puts "new client <#{session}>"
|
62
|
+
end
|
36
63
|
|
37
|
-
|
38
|
-
|
39
|
-
|
64
|
+
=== On "error" Event
|
65
|
+
|
66
|
+
Client Side
|
67
|
+
|
68
|
+
io.on("error", function(err){
|
69
|
+
console.error(err);
|
40
70
|
});
|
41
71
|
|
42
|
-
// client --> server
|
43
|
-
io.push("chat", {name: "shokai", message: "hello"});
|
44
72
|
|
73
|
+
=== Remove Event Listener
|
74
|
+
|
75
|
+
Server Side
|
76
|
+
|
77
|
+
id = CometIO.on :chat do |data, from|
|
78
|
+
puts "#{data} - from#{from}"
|
79
|
+
end
|
80
|
+
CometIO.removeListener id
|
81
|
+
|
82
|
+
or
|
83
|
+
|
84
|
+
CometIO.removeListener :chat # remove all chat listener
|
85
|
+
|
86
|
+
|
87
|
+
== Sample App
|
88
|
+
|
89
|
+
chat app
|
90
|
+
|
91
|
+
- http://cometio-chat.herokuapp.com
|
92
|
+
- https://github.com/shokai/sinatra-cometio/tree/master/sample
|
45
93
|
|
46
|
-
see sample app => http://cometio-chat.herokuapp.com
|
47
94
|
|
48
95
|
== REQUIREMENTS:
|
49
96
|
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ $hoe = Hoe.spec 'sinatra-cometio' do
|
|
15
15
|
self.rubyforge_name = self.name # TODO this is default value
|
16
16
|
self.extra_deps = [['sinatra','>= 1.3.3'],
|
17
17
|
['eventmachine', '>= 1.0.0'],
|
18
|
-
['json'],
|
18
|
+
['json', '>= 1.7.0'],
|
19
19
|
['sinatra-contrib', '>= 1.3.2'],
|
20
20
|
['event_emitter', '>= 0.1.0']]
|
21
21
|
|
data/lib/js/cometio.js
CHANGED
@@ -11,6 +11,7 @@ var CometIO = function(){
|
|
11
11
|
success : function(data){
|
12
12
|
},
|
13
13
|
error : function(req, stat, e){
|
14
|
+
self.emit("error", "CometIO push error");
|
14
15
|
},
|
15
16
|
complete : function(e){
|
16
17
|
},
|
@@ -42,6 +43,7 @@ var CometIO = function(){
|
|
42
43
|
self.get();
|
43
44
|
},
|
44
45
|
error : function(req, stat, e){
|
46
|
+
self.emit("error", "CometIO get error");
|
45
47
|
setTimeout(self.get, 10000);
|
46
48
|
},
|
47
49
|
complete : function(e){
|
data/lib/sinatra-cometio.rb
CHANGED
data/sample/public/js/index.js
CHANGED
@@ -6,13 +6,17 @@ io.on("chat", function(data){
|
|
6
6
|
});
|
7
7
|
|
8
8
|
io.on("connect", function(session){
|
9
|
-
|
9
|
+
console.log("connect!! "+session);
|
10
10
|
$("#chat #btn_send").click(post);
|
11
11
|
$("#chat #message").keydown(function(e){
|
12
12
|
if(e.keyCode == 13) post();
|
13
13
|
});
|
14
14
|
});
|
15
15
|
|
16
|
+
io.on("error", function(err){
|
17
|
+
console.error(err);
|
18
|
+
});
|
19
|
+
|
16
20
|
var post = function(){
|
17
21
|
var name = $("#chat #name").val();
|
18
22
|
var message = $("#chat #message").val();
|
data/sample/views/index.haml
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
%div#chat
|
18
18
|
%input{:id => 'name', :value => 'name', :size => 20}
|
19
19
|
%input{:id => 'message', :value => 'hello!!', :size => 80}
|
20
|
-
%input{:id => 'btn_send', :type => 'button', :value => '
|
20
|
+
%input{:id => 'btn_send', :type => 'button', :value => 'post'}
|
21
21
|
%ul#timeline
|
22
22
|
|
23
23
|
%hr
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-cometio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
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-
|
12
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 1.7.0
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.7.0
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: sinatra-contrib
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -194,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
194
194
|
version: '0'
|
195
195
|
segments:
|
196
196
|
- 0
|
197
|
-
hash:
|
197
|
+
hash: 3190168265440038000
|
198
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
199
199
|
none: false
|
200
200
|
requirements:
|