sinatra-rocketio 0.2.5 → 0.2.6
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.
- checksums.yaml +4 -4
- data/HEADER.erb +4 -0
- data/History.txt +5 -0
- data/README.md +36 -2
- data/Rakefile +28 -0
- data/lib/js/rocketio.js +24 -7
- data/lib/sinatra-rocketio/application.rb +1 -0
- data/lib/sinatra-rocketio/version.rb +1 -1
- data/rocketio.js +310 -0
- data/rocketio.min.js +5 -0
- data/sample/classic_style/config.ru +1 -1
- data/sample/modular_style/config.ru +1 -1
- data/test/app.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f749b52c9e6cdf5d94b913387b7ed435df575a21
|
4
|
+
data.tar.gz: 26bb924f8435df3c0be29d0cd9cbfe0fe5551f3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32782a0c0c99665694a5f8ff36af55584b610db812ea7a365e2ff218ea27ae6d6423153e31e8f03254b25ed6f6db7501801800d54038d2eb4158497d1d073282
|
7
|
+
data.tar.gz: c3f910f932c95ebc2806300cfd9f846ad79ad562809a3ae4001efd4961e5cbb29acfaacb8de457f156e6afd49485ee3bce9fd536b23c30f8b571f39c7cdded9c
|
data/HEADER.erb
ADDED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -31,7 +31,7 @@ Server Side
|
|
31
31
|
```ruby
|
32
32
|
require 'sinatra'
|
33
33
|
require 'sinatra/rocketio'
|
34
|
-
set :cometio, :timeout => 120, :post_interval => 2
|
34
|
+
set :cometio, :timeout => 120, :post_interval => 2, :allow_crossdomain => false
|
35
35
|
set :websocketio, :port => 5001
|
36
36
|
set :rocketio, :websocket => true, :comet => true # enable WebSocket and Comet
|
37
37
|
|
@@ -47,7 +47,7 @@ io.push :light, {:value => 150}, {:to => session_id} # to specific client
|
|
47
47
|
Client Side
|
48
48
|
|
49
49
|
```html
|
50
|
-
<script src="//ajax.googleapis.com/ajax/libs/jquery/
|
50
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
|
51
51
|
<script src="<%= rocketio_js %>"></script>
|
52
52
|
```
|
53
53
|
```javascript
|
@@ -210,6 +210,40 @@ Sample App
|
|
210
210
|
- https://github.com/shokai/rocketio-arduino-sample
|
211
211
|
|
212
212
|
|
213
|
+
JavaScript Lib for browser
|
214
|
+
--------------------------
|
215
|
+
|
216
|
+
### Download
|
217
|
+
|
218
|
+
- [rocketio.js](https://raw.github.com/shokai/sinatra-rocketio/master/rocketio.js)
|
219
|
+
- [rocketio.min.js](https://raw.github.com/shokai/sinatra-rocketio/master/rocketio.min.js)
|
220
|
+
|
221
|
+
|
222
|
+
### Usage
|
223
|
+
|
224
|
+
```html
|
225
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
|
226
|
+
<script src="/rocketio.min.js"></script>
|
227
|
+
```
|
228
|
+
```javascript
|
229
|
+
var io = new RocketIO().connect("http://example.com");
|
230
|
+
|
231
|
+
io.on("connect", function(){
|
232
|
+
alert(io.type + " connect!! " + io.session);
|
233
|
+
});
|
234
|
+
```
|
235
|
+
|
236
|
+
### Generate JS Lib
|
237
|
+
|
238
|
+
% npm install -g uglify-js
|
239
|
+
% gem install bundler
|
240
|
+
% bundle install
|
241
|
+
% rake jslib
|
242
|
+
|
243
|
+
=> rocketio.js and rocketio.min.js
|
244
|
+
|
245
|
+
|
246
|
+
|
213
247
|
Test
|
214
248
|
----
|
215
249
|
|
data/Rakefile
CHANGED
@@ -5,6 +5,34 @@ Rake::TestTask.new do |t|
|
|
5
5
|
t.pattern = "test/test_*.rb"
|
6
6
|
end
|
7
7
|
|
8
|
+
desc "generate JavaScript lib for browser"
|
9
|
+
task :jslib do
|
10
|
+
dest = "rocketio.js"
|
11
|
+
dest_min = "rocketio.min.js"
|
12
|
+
|
13
|
+
require "rubygems"
|
14
|
+
require "sinatra/websocketio"
|
15
|
+
require "sinatra/cometio"
|
16
|
+
require "sinatra/rocketio"
|
17
|
+
require "erb"
|
18
|
+
|
19
|
+
websocketio_url = nil
|
20
|
+
cometio_url = nil
|
21
|
+
header = ERB.new(File.open(File.expand_path "HEADER.erb", File.dirname(__FILE__)).read).result(binding)
|
22
|
+
js = ERB.new(Sinatra::RocketIO.javascript).result(binding)
|
23
|
+
|
24
|
+
File.open(dest, "w+") do |f|
|
25
|
+
f.puts header
|
26
|
+
f.write js
|
27
|
+
end
|
28
|
+
puts " => #{dest}"
|
29
|
+
File.open(dest_min, "w+") do |f|
|
30
|
+
f.puts header
|
31
|
+
end
|
32
|
+
system "uglifyjs #{dest} >> #{dest_min}"
|
33
|
+
puts " => #{dest_min}"
|
34
|
+
end
|
35
|
+
|
8
36
|
desc "Start test server"
|
9
37
|
task :test_server do
|
10
38
|
require File.expand_path 'test/app', File.dirname(__FILE__)
|
data/lib/js/rocketio.js
CHANGED
@@ -1,30 +1,47 @@
|
|
1
1
|
var RocketIO = function(opts){
|
2
2
|
new EventEmitter().apply(this);
|
3
|
-
|
4
|
-
this.
|
3
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
4
|
+
this.type = opts.type || null; // "comet" or "websocket"
|
5
|
+
this.session = opts.session || null;
|
5
6
|
this.channel = null;
|
7
|
+
if(typeof opts.channel !== "undefined" && opts.channel !== null){
|
8
|
+
this.channel = ""+opts.channel;
|
9
|
+
}
|
10
|
+
var setting = {};
|
6
11
|
this.io = null;
|
7
12
|
var self = this;
|
8
13
|
var ws_close_timer = null;
|
9
|
-
if(typeof opts === "object"){
|
10
|
-
this.channel = ""+opts.channel;
|
11
|
-
}
|
12
14
|
self.on("__connect", function(session_id){
|
13
15
|
self.session = session_id;
|
14
16
|
self.io.push("__channel_id", self.channel);
|
15
17
|
self.emit("connect");
|
16
18
|
});
|
17
19
|
|
18
|
-
this.connect = function(){
|
20
|
+
this.connect = function(url){
|
21
|
+
if(typeof url === "string"){
|
22
|
+
$.getJSON(url+"/rocketio/settings", function(res){
|
23
|
+
setting = res;
|
24
|
+
connect_io();
|
25
|
+
});
|
26
|
+
return self;
|
27
|
+
}
|
28
|
+
else{
|
29
|
+
return connect_io();
|
30
|
+
}
|
31
|
+
};
|
32
|
+
|
33
|
+
var connect_io = function(){
|
19
34
|
self.io = function(){
|
20
35
|
if(self.type === "comet") return;
|
21
36
|
if(typeof WebSocketIO !== "function") return;
|
22
37
|
var io = new WebSocketIO();
|
38
|
+
if(typeof setting.websocket === "string") io.url = setting.websocket;
|
23
39
|
io.session = self.session;
|
24
40
|
return io.connect();
|
25
41
|
}() || function(){
|
26
42
|
if(typeof CometIO !== "function") return;
|
27
43
|
var io = new CometIO();
|
44
|
+
if(typeof setting.comet === "string") io.url = setting.comet;
|
28
45
|
io.session = self.session;
|
29
46
|
return io.connect();
|
30
47
|
}();
|
@@ -44,7 +61,7 @@ var RocketIO = function(opts){
|
|
44
61
|
ws_close_timer = setTimeout(function(){
|
45
62
|
self.close();
|
46
63
|
self.type = "comet";
|
47
|
-
|
64
|
+
connect_io();
|
48
65
|
}, 3000);
|
49
66
|
self.once("connect", function(){
|
50
67
|
if(ws_close_timer) clearTimeout(ws_close_timer);
|
@@ -7,6 +7,7 @@ module Sinatra
|
|
7
7
|
app.helpers Sinatra::RocketIO::Helpers
|
8
8
|
app.get '/rocketio/settings' do
|
9
9
|
content_type 'application/json'
|
10
|
+
response["Access-Control-Allow-Origin"] = "*"
|
10
11
|
@setting_json ||= (
|
11
12
|
setting = {}
|
12
13
|
setting[:websocket] = websocketio_url if Sinatra::RocketIO.options[:websocket]
|
data/rocketio.js
ADDED
@@ -0,0 +1,310 @@
|
|
1
|
+
// RocketIO.js v0.2.5 (websocketio:v0.3.1, cometio:v0.5.7)
|
2
|
+
// https://github.com/shokai/sinatra-rocketio
|
3
|
+
// (c) 2012-2013 Sho Hashimoto <hashimoto@shokai.org>
|
4
|
+
// The MIT License
|
5
|
+
var RocketIO = function(opts){
|
6
|
+
new EventEmitter().apply(this);
|
7
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
8
|
+
this.type = opts.type || null; // "comet" or "websocket"
|
9
|
+
this.session = opts.session || null;
|
10
|
+
this.channel = null;
|
11
|
+
if(typeof opts.channel !== "undefined" && opts.channel !== null){
|
12
|
+
this.channel = ""+opts.channel;
|
13
|
+
}
|
14
|
+
var setting = {};
|
15
|
+
this.io = null;
|
16
|
+
var self = this;
|
17
|
+
var ws_close_timer = null;
|
18
|
+
self.on("__connect", function(session_id){
|
19
|
+
self.session = session_id;
|
20
|
+
self.io.push("__channel_id", self.channel);
|
21
|
+
self.emit("connect");
|
22
|
+
});
|
23
|
+
|
24
|
+
this.connect = function(url){
|
25
|
+
if(typeof url === "string"){
|
26
|
+
$.getJSON(url+"/rocketio/settings", function(res){
|
27
|
+
setting = res;
|
28
|
+
connect_io();
|
29
|
+
});
|
30
|
+
return self;
|
31
|
+
}
|
32
|
+
else{
|
33
|
+
return connect_io();
|
34
|
+
}
|
35
|
+
};
|
36
|
+
|
37
|
+
var connect_io = function(){
|
38
|
+
self.io = function(){
|
39
|
+
if(self.type === "comet") return;
|
40
|
+
if(typeof WebSocketIO !== "function") return;
|
41
|
+
var io = new WebSocketIO();
|
42
|
+
if(typeof setting.websocket === "string") io.url = setting.websocket;
|
43
|
+
io.session = self.session;
|
44
|
+
return io.connect();
|
45
|
+
}() || function(){
|
46
|
+
if(typeof CometIO !== "function") return;
|
47
|
+
var io = new CometIO();
|
48
|
+
if(typeof setting.comet === "string") io.url = setting.comet;
|
49
|
+
io.session = self.session;
|
50
|
+
return io.connect();
|
51
|
+
}();
|
52
|
+
if(typeof self.io === "undefined"){
|
53
|
+
setTimeout(function(){
|
54
|
+
self.emit("error", "WebSocketIO and CometIO are not available");
|
55
|
+
}, 100);
|
56
|
+
return self;
|
57
|
+
};
|
58
|
+
if(self.io.url.match(/^ws:\/\/.+/)) self.type = "websocket";
|
59
|
+
else if(self.io.url.match(/cometio/)) self.type = "comet";
|
60
|
+
else self.type = "unknown";
|
61
|
+
self.io.on("*", function(event_name, args){
|
62
|
+
if(event_name === "connect") event_name = "__connect";
|
63
|
+
self.emit(event_name, args);
|
64
|
+
});
|
65
|
+
ws_close_timer = setTimeout(function(){
|
66
|
+
self.close();
|
67
|
+
self.type = "comet";
|
68
|
+
connect_io();
|
69
|
+
}, 3000);
|
70
|
+
self.once("connect", function(){
|
71
|
+
if(ws_close_timer) clearTimeout(ws_close_timer);
|
72
|
+
ws_close_timer = null;
|
73
|
+
});
|
74
|
+
return self;
|
75
|
+
};
|
76
|
+
|
77
|
+
this.close = function(){
|
78
|
+
self.io.close();
|
79
|
+
};
|
80
|
+
|
81
|
+
this.push = function(type, data){
|
82
|
+
self.io.push(type, data);
|
83
|
+
};
|
84
|
+
};
|
85
|
+
|
86
|
+
var CometIO = function(url, opts){
|
87
|
+
new EventEmitter().apply(this);
|
88
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
89
|
+
this.url = url || "";
|
90
|
+
this.session = opts.session || null;
|
91
|
+
var running = false;
|
92
|
+
var self = this;
|
93
|
+
var post_queue = [];
|
94
|
+
|
95
|
+
var flush = function(){
|
96
|
+
if(!running || post_queue.length < 1) return;
|
97
|
+
var post_data = {
|
98
|
+
json: JSON.stringify({
|
99
|
+
session: self.session,
|
100
|
+
events: post_queue
|
101
|
+
})
|
102
|
+
};
|
103
|
+
$.ajax(
|
104
|
+
{
|
105
|
+
url : self.url,
|
106
|
+
data : post_data,
|
107
|
+
success : function(data){
|
108
|
+
},
|
109
|
+
error : function(req, stat, e){
|
110
|
+
self.emit("error", "CometIO push error");
|
111
|
+
},
|
112
|
+
complete : function(e){
|
113
|
+
},
|
114
|
+
type : "POST",
|
115
|
+
dataType : "json",
|
116
|
+
timeout : 10000
|
117
|
+
}
|
118
|
+
);
|
119
|
+
post_queue = [];
|
120
|
+
};
|
121
|
+
setInterval(flush, 1000);
|
122
|
+
|
123
|
+
this.push = function(type, data){
|
124
|
+
if(!running || !self.session){
|
125
|
+
self.emit("error", "CometIO not connected");
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
post_queue.push({type: type, data: data})
|
129
|
+
};
|
130
|
+
|
131
|
+
this.connect = function(){
|
132
|
+
if(running) return self;
|
133
|
+
self.on("__session_id", function(session){
|
134
|
+
self.session = session;
|
135
|
+
self.emit("connect", self.session);
|
136
|
+
});
|
137
|
+
running = true;
|
138
|
+
get();
|
139
|
+
return self;
|
140
|
+
};
|
141
|
+
|
142
|
+
this.close = function(){
|
143
|
+
running = false;
|
144
|
+
self.removeListener("__session_id");
|
145
|
+
};
|
146
|
+
|
147
|
+
var get = function(){
|
148
|
+
if(!running) return;
|
149
|
+
$.ajax(
|
150
|
+
{
|
151
|
+
url : self.url,
|
152
|
+
data : {session : self.session},
|
153
|
+
success : function(data_arr){
|
154
|
+
if(data_arr !== null && typeof data_arr == "object" && !!data_arr.length){
|
155
|
+
for(var i = 0; i < data_arr.length; i++){
|
156
|
+
var data = data_arr[i];
|
157
|
+
if(data) self.emit(data.type, data.data);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
get();
|
161
|
+
},
|
162
|
+
error : function(req, stat, e){
|
163
|
+
self.emit("error", "CometIO get error");
|
164
|
+
setTimeout(get, 10000);
|
165
|
+
},
|
166
|
+
complete : function(e){
|
167
|
+
},
|
168
|
+
type : "GET",
|
169
|
+
dataType : "json",
|
170
|
+
timeout : 130000
|
171
|
+
}
|
172
|
+
);
|
173
|
+
};
|
174
|
+
};
|
175
|
+
|
176
|
+
var WebSocketIO = function(url, opts){
|
177
|
+
new EventEmitter().apply(this);
|
178
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
179
|
+
this.url = url || "";
|
180
|
+
this.session = opts.session || null;
|
181
|
+
this.websocket = null;
|
182
|
+
this.connecting = false;
|
183
|
+
var reconnect_timer_id = null;
|
184
|
+
var running = false;
|
185
|
+
var self = this;
|
186
|
+
|
187
|
+
self.on("__session_id", function(session_id){
|
188
|
+
self.session = session_id;
|
189
|
+
self.emit("connect", self.session);
|
190
|
+
});
|
191
|
+
|
192
|
+
this.connect = function(){
|
193
|
+
if(typeof WebSocket === "undefined"){
|
194
|
+
self.emit("error", "websocket not exists in this browser");
|
195
|
+
return null;
|
196
|
+
}
|
197
|
+
self.running = true;
|
198
|
+
var url = self.session ? self.url+"/session="+self.session : self.url;
|
199
|
+
self.websocket = new WebSocket(url);
|
200
|
+
self.websocket.onmessage = function(e){
|
201
|
+
try{
|
202
|
+
var data_ = JSON.parse(e.data);
|
203
|
+
self.emit(data_.type, data_.data);
|
204
|
+
}
|
205
|
+
catch(e){
|
206
|
+
self.emit("error", "WebSocketIO data parse error");
|
207
|
+
}
|
208
|
+
};
|
209
|
+
self.websocket.onclose = function(){
|
210
|
+
if(self.connecting){
|
211
|
+
self.connecting = false;
|
212
|
+
self.emit("disconnect");
|
213
|
+
}
|
214
|
+
if(self.running){
|
215
|
+
reconnect_timer_id = setTimeout(self.connect, 10000);
|
216
|
+
}
|
217
|
+
};
|
218
|
+
self.websocket.onopen = function(){
|
219
|
+
self.connecting = true;
|
220
|
+
};
|
221
|
+
return self;
|
222
|
+
};
|
223
|
+
|
224
|
+
this.close = function(){
|
225
|
+
clearTimeout(reconnect_timer_id);
|
226
|
+
self.running = false;
|
227
|
+
self.websocket.close();
|
228
|
+
};
|
229
|
+
|
230
|
+
this.push = function(type, data){
|
231
|
+
if(!self.connecting){
|
232
|
+
self.emit("error", "websocket not connected");
|
233
|
+
return;
|
234
|
+
}
|
235
|
+
self.websocket.send(JSON.stringify({type: type, data: data, session: self.session}));
|
236
|
+
};
|
237
|
+
};
|
238
|
+
|
239
|
+
// event_emitter.js v0.0.8
|
240
|
+
// https://github.com/shokai/event_emitter.js
|
241
|
+
// (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
|
242
|
+
// The MIT License
|
243
|
+
var EventEmitter = function(){
|
244
|
+
var self = this;
|
245
|
+
this.apply = function(target, prefix){
|
246
|
+
if(!prefix) prefix = "";
|
247
|
+
for(var func in self){
|
248
|
+
if(self.hasOwnProperty(func) && func !== "apply"){
|
249
|
+
target[prefix+func] = this[func];
|
250
|
+
}
|
251
|
+
}
|
252
|
+
};
|
253
|
+
this.__events = new Array();
|
254
|
+
this.on = function(type, listener, opts){
|
255
|
+
if(typeof listener !== "function") return;
|
256
|
+
var event_id = self.__events.length > 0 ? 1 + self.__events[self.__events.length-1].id : 0
|
257
|
+
var params = {
|
258
|
+
id: event_id,
|
259
|
+
type: type,
|
260
|
+
listener: listener
|
261
|
+
};
|
262
|
+
for(i in opts){
|
263
|
+
if(!params[i]) params[i] = opts[i];
|
264
|
+
};
|
265
|
+
self.__events.push(params);
|
266
|
+
return event_id;
|
267
|
+
};
|
268
|
+
|
269
|
+
this.once = function(type, listener){
|
270
|
+
self.on(type, listener, {once: true});
|
271
|
+
};
|
272
|
+
|
273
|
+
this.emit = function(type, data){
|
274
|
+
for(var i = 0; i < self.__events.length; i++){
|
275
|
+
var e = self.__events[i];
|
276
|
+
switch(e.type){
|
277
|
+
case type:
|
278
|
+
e.listener(data);
|
279
|
+
if(e.once) e.type = null;
|
280
|
+
break
|
281
|
+
case "*":
|
282
|
+
e.listener(type, data);
|
283
|
+
if(e.once) e.type = null;
|
284
|
+
break
|
285
|
+
}
|
286
|
+
}
|
287
|
+
self.removeListener();
|
288
|
+
};
|
289
|
+
|
290
|
+
this.removeListener = function(id_or_type){
|
291
|
+
for(var i = self.__events.length-1; i >= 0; i--){
|
292
|
+
var e = self.__events[i];
|
293
|
+
switch(typeof id_or_type){
|
294
|
+
case "number":
|
295
|
+
if(e.id === id_or_type) self.__events.splice(i,1);
|
296
|
+
break
|
297
|
+
case "string":
|
298
|
+
case "object":
|
299
|
+
if(e.type === id_or_type) self.__events.splice(i,1);
|
300
|
+
break
|
301
|
+
}
|
302
|
+
}
|
303
|
+
};
|
304
|
+
|
305
|
+
};
|
306
|
+
|
307
|
+
if(typeof module !== "undefined" && typeof module.exports !== "undefined"){
|
308
|
+
module.exports = EventEmitter;
|
309
|
+
}
|
310
|
+
|
data/rocketio.min.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
// RocketIO.js v0.2.5 (websocketio:v0.3.1, cometio:v0.5.7)
|
2
|
+
// https://github.com/shokai/sinatra-rocketio
|
3
|
+
// (c) 2012-2013 Sho Hashimoto <hashimoto@shokai.org>
|
4
|
+
// The MIT License
|
5
|
+
var RocketIO=function(opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.type=opts.type||null;this.session=opts.session||null;this.channel=null;if(typeof opts.channel!=="undefined"&&opts.channel!==null){this.channel=""+opts.channel}var setting={};this.io=null;var self=this;var ws_close_timer=null;self.on("__connect",function(session_id){self.session=session_id;self.io.push("__channel_id",self.channel);self.emit("connect")});this.connect=function(url){if(typeof url==="string"){$.getJSON(url+"/rocketio/settings",function(res){setting=res;connect_io()});return self}else{return connect_io()}};var connect_io=function(){self.io=function(){if(self.type==="comet")return;if(typeof WebSocketIO!=="function")return;var io=new WebSocketIO;if(typeof setting.websocket==="string")io.url=setting.websocket;io.session=self.session;return io.connect()}()||function(){if(typeof CometIO!=="function")return;var io=new CometIO;if(typeof setting.comet==="string")io.url=setting.comet;io.session=self.session;return io.connect()}();if(typeof self.io==="undefined"){setTimeout(function(){self.emit("error","WebSocketIO and CometIO are not available")},100);return self}if(self.io.url.match(/^ws:\/\/.+/))self.type="websocket";else if(self.io.url.match(/cometio/))self.type="comet";else self.type="unknown";self.io.on("*",function(event_name,args){if(event_name==="connect")event_name="__connect";self.emit(event_name,args)});ws_close_timer=setTimeout(function(){self.close();self.type="comet";connect_io()},3e3);self.once("connect",function(){if(ws_close_timer)clearTimeout(ws_close_timer);ws_close_timer=null});return self};this.close=function(){self.io.close()};this.push=function(type,data){self.io.push(type,data)}};var CometIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;var running=false;var self=this;var post_queue=[];var flush=function(){if(!running||post_queue.length<1)return;var post_data={json:JSON.stringify({session:self.session,events:post_queue})};$.ajax({url:self.url,data:post_data,success:function(data){},error:function(req,stat,e){self.emit("error","CometIO push error")},complete:function(e){},type:"POST",dataType:"json",timeout:1e4});post_queue=[]};setInterval(flush,1e3);this.push=function(type,data){if(!running||!self.session){self.emit("error","CometIO not connected");return}post_queue.push({type:type,data:data})};this.connect=function(){if(running)return self;self.on("__session_id",function(session){self.session=session;self.emit("connect",self.session)});running=true;get();return self};this.close=function(){running=false;self.removeListener("__session_id")};var get=function(){if(!running)return;$.ajax({url:self.url,data:{session:self.session},success:function(data_arr){if(data_arr!==null&&typeof data_arr=="object"&&!!data_arr.length){for(var i=0;i<data_arr.length;i++){var data=data_arr[i];if(data)self.emit(data.type,data.data)}}get()},error:function(req,stat,e){self.emit("error","CometIO get error");setTimeout(get,1e4)},complete:function(e){},type:"GET",dataType:"json",timeout:13e4})}};var WebSocketIO=function(url,opts){(new EventEmitter).apply(this);if(typeof opts==="undefined"||opts===null)opts={};this.url=url||"";this.session=opts.session||null;this.websocket=null;this.connecting=false;var reconnect_timer_id=null;var running=false;var self=this;self.on("__session_id",function(session_id){self.session=session_id;self.emit("connect",self.session)});this.connect=function(){if(typeof WebSocket==="undefined"){self.emit("error","websocket not exists in this browser");return null}self.running=true;var url=self.session?self.url+"/session="+self.session:self.url;self.websocket=new WebSocket(url);self.websocket.onmessage=function(e){try{var data_=JSON.parse(e.data);self.emit(data_.type,data_.data)}catch(e){self.emit("error","WebSocketIO data parse error")}};self.websocket.onclose=function(){if(self.connecting){self.connecting=false;self.emit("disconnect")}if(self.running){reconnect_timer_id=setTimeout(self.connect,1e4)}};self.websocket.onopen=function(){self.connecting=true};return self};this.close=function(){clearTimeout(reconnect_timer_id);self.running=false;self.websocket.close()};this.push=function(type,data){if(!self.connecting){self.emit("error","websocket not connected");return}self.websocket.send(JSON.stringify({type:type,data:data,session:self.session}))}};var EventEmitter=function(){var self=this;this.apply=function(target,prefix){if(!prefix)prefix="";for(var func in self){if(self.hasOwnProperty(func)&&func!=="apply"){target[prefix+func]=this[func]}}};this.__events=new Array;this.on=function(type,listener,opts){if(typeof listener!=="function")return;var event_id=self.__events.length>0?1+self.__events[self.__events.length-1].id:0;var params={id:event_id,type:type,listener:listener};for(i in opts){if(!params[i])params[i]=opts[i]}self.__events.push(params);return event_id};this.once=function(type,listener){self.on(type,listener,{once:true})};this.emit=function(type,data){for(var i=0;i<self.__events.length;i++){var e=self.__events[i];switch(e.type){case type:e.listener(data);if(e.once)e.type=null;break;case"*":e.listener(type,data);if(e.once)e.type=null;break}}self.removeListener()};this.removeListener=function(id_or_type){for(var i=self.__events.length-1;i>=0;i--){var e=self.__events[i];switch(typeof id_or_type){case"number":if(e.id===id_or_type)self.__events.splice(i,1);break;case"string":case"object":if(e.type===id_or_type)self.__events.splice(i,1);break}}}};if(typeof module!=="undefined"&&typeof module.exports!=="undefined"){module.exports=EventEmitter}
|
@@ -13,7 +13,7 @@ require 'sass'
|
|
13
13
|
require File.dirname(__FILE__)+'/main'
|
14
14
|
|
15
15
|
set :haml, :escape_html => true
|
16
|
-
set :cometio, :timeout => 120, :post_interval => 2
|
16
|
+
set :cometio, :timeout => 120, :post_interval => 2, :allow_crossdomain => true
|
17
17
|
set :websocketio, :port => (ENV['WS_PORT'] || 8080).to_i
|
18
18
|
set :rocketio, :comet => true, :websocket => true
|
19
19
|
|
@@ -14,7 +14,7 @@ require 'sass'
|
|
14
14
|
require File.dirname(__FILE__)+'/main'
|
15
15
|
|
16
16
|
set :haml, :escape_html => true
|
17
|
-
set :cometio, :timeout => 120, :post_interval => 2
|
17
|
+
set :cometio, :timeout => 120, :post_interval => 2, :allow_crossdomain => true
|
18
18
|
set :websocketio, :port => (ENV['WS_PORT'] || 8080).to_i
|
19
19
|
set :rocketio, :comet => true, :websocket => true
|
20
20
|
|
data/test/app.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-rocketio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Hashimoto
|
@@ -215,6 +215,7 @@ extra_rdoc_files: []
|
|
215
215
|
files:
|
216
216
|
- .gitignore
|
217
217
|
- Gemfile
|
218
|
+
- HEADER.erb
|
218
219
|
- History.txt
|
219
220
|
- LICENSE.txt
|
220
221
|
- README.md
|
@@ -232,6 +233,8 @@ files:
|
|
232
233
|
- lib/sinatra-rocketio/version.rb
|
233
234
|
- lib/sinatra/rocketio.rb
|
234
235
|
- lib/sinatra/rocketio/client.rb
|
236
|
+
- rocketio.js
|
237
|
+
- rocketio.min.js
|
235
238
|
- sample/classic_style/README.md
|
236
239
|
- sample/classic_style/bin/cui_chat_client.rb
|
237
240
|
- sample/classic_style/config.ru
|