sinatra-rocketio-linda 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HEADER.erb +4 -0
- data/History.txt +4 -0
- data/README.md +37 -0
- data/Rakefile +29 -0
- data/lib/sinatra-rocketio-linda/javascript.rb +1 -1
- data/lib/sinatra/rocketio/linda/version.rb +1 -1
- data/linda.js +355 -0
- data/linda.min.js +5 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a21d349cf79dd595121baad063de1ef33c8b957
|
4
|
+
data.tar.gz: d46fa4cb8f29feae458c0d9df8fae976b89cb5b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7395d4341d8a46eb373e0757ed0397e07f08b53245bcf2e178a8fbb26c0f83a262d0c8daeb4e2350d1154ae28df3d3bf9ad449bf9481b729763cfb542f38bf27
|
7
|
+
data.tar.gz: 70ffc23c4d8b74555547a99fdbb91e7e85e4828cd56a72928a47942364fe13a61fe09dbe85d5de7207435349dab47fb2d28d3c4cf418f442404edd1a869527ca
|
data/HEADER.erb
ADDED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -147,6 +147,43 @@ linda.wait
|
|
147
147
|
```
|
148
148
|
|
149
149
|
|
150
|
+
JavaScript Lib for browser
|
151
|
+
--------------------------
|
152
|
+
|
153
|
+
### Download
|
154
|
+
|
155
|
+
- [linda.js](https://raw.github.com/shokai/sinatra-rocketio-linda/master/linda.js)
|
156
|
+
- [linda.min.js](https://raw.github.com/shokai/sinatra-rocketio-linda/master/linda.min.js)
|
157
|
+
|
158
|
+
|
159
|
+
### Usage
|
160
|
+
|
161
|
+
```html
|
162
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
|
163
|
+
<script src="/linda.min.js"></script>
|
164
|
+
```
|
165
|
+
```javascript
|
166
|
+
var io = new RocketIO().connect("http://example.com");
|
167
|
+
var linda = new Linda(io);
|
168
|
+
|
169
|
+
var ts = new linda.TupleSpace("calc");
|
170
|
+
|
171
|
+
io.on("connect", function(){
|
172
|
+
alert(io.type + " connect!! " + io.session);
|
173
|
+
ts.write([1, 2, 3]);
|
174
|
+
});
|
175
|
+
```
|
176
|
+
|
177
|
+
### Generate JS Lib
|
178
|
+
|
179
|
+
% npm install -g uglify-js
|
180
|
+
% gem install bundler
|
181
|
+
% bundle install
|
182
|
+
% rake jslib
|
183
|
+
|
184
|
+
=> linda.js and linda.min.js
|
185
|
+
|
186
|
+
|
150
187
|
Test
|
151
188
|
----
|
152
189
|
|
data/Rakefile
CHANGED
@@ -5,6 +5,35 @@ 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 = "linda.js"
|
11
|
+
dest_min = "linda.min.js"
|
12
|
+
|
13
|
+
require "rubygems"
|
14
|
+
require "sinatra/websocketio"
|
15
|
+
require "sinatra/cometio"
|
16
|
+
require "sinatra/rocketio"
|
17
|
+
require "sinatra/rocketio/linda"
|
18
|
+
require "erb"
|
19
|
+
|
20
|
+
websocketio_url = nil
|
21
|
+
cometio_url = nil
|
22
|
+
header = ERB.new(File.open(File.expand_path "HEADER.erb", File.dirname(__FILE__)).read).result(binding)
|
23
|
+
js = ERB.new(Sinatra::RocketIO::Linda.javascript).result(binding)
|
24
|
+
|
25
|
+
File.open(dest, "w+") do |f|
|
26
|
+
f.puts header
|
27
|
+
f.write js
|
28
|
+
end
|
29
|
+
puts " => #{dest}"
|
30
|
+
File.open(dest_min, "w+") do |f|
|
31
|
+
f.puts header
|
32
|
+
end
|
33
|
+
system "uglifyjs #{dest} >> #{dest_min}"
|
34
|
+
puts " => #{dest_min}"
|
35
|
+
end
|
36
|
+
|
8
37
|
desc "Start test server"
|
9
38
|
task :test_server do
|
10
39
|
require File.expand_path 'test/app', File.dirname(__FILE__)
|
data/linda.js
ADDED
@@ -0,0 +1,355 @@
|
|
1
|
+
// Linda.js v0.0.8 (rocketio v0.2.6)
|
2
|
+
// https://github.com/shokai/sinatra-rocketio-linda
|
3
|
+
// (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
|
4
|
+
// The MIT License
|
5
|
+
var Linda = function(io, opts){
|
6
|
+
var self = this;
|
7
|
+
this.io = null;
|
8
|
+
if(io === null || typeof io === "undefined"){
|
9
|
+
this.io = new RocketIO().connect();
|
10
|
+
}
|
11
|
+
else{
|
12
|
+
this.io = io;
|
13
|
+
}
|
14
|
+
this.opts = opts || {};
|
15
|
+
this.TupleSpace = function(name){
|
16
|
+
if(name === null || typeof name !== "string") name = "__default__";
|
17
|
+
this.name = name;
|
18
|
+
this.linda = self;
|
19
|
+
var space = this;
|
20
|
+
this.write = function(tuple, opts){
|
21
|
+
if(tuple === null || typeof tuple !== "object") return;
|
22
|
+
if(opts === null || typeof opts === "undefined") opts = {};
|
23
|
+
self.io.push("__linda_write", [space.name, tuple, opts]);
|
24
|
+
};
|
25
|
+
this.read = function(tuple, callback){
|
26
|
+
if(tuple === null || typeof tuple !== "object") return;
|
27
|
+
if(typeof callback !== "function") return;
|
28
|
+
var callback_id = new Date()-0+"";
|
29
|
+
self.io.once("__linda_read_callback_"+callback_id, callback);
|
30
|
+
self.io.push("__linda_read", [space.name, tuple, callback_id]);
|
31
|
+
};
|
32
|
+
this.take = function(tuple, callback){
|
33
|
+
if(tuple === null || typeof tuple !== "object") return;
|
34
|
+
if(typeof callback !== "function") return;
|
35
|
+
var callback_id = new Date()-0+"";
|
36
|
+
self.io.once("__linda_take_callback_"+callback_id, callback);
|
37
|
+
self.io.push("__linda_take", [space.name, tuple, callback_id]);
|
38
|
+
};
|
39
|
+
this.watch = function(tuple, callback){
|
40
|
+
if(tuple === null || typeof tuple !== "object") return;
|
41
|
+
if(typeof callback !== "function") return;
|
42
|
+
var callback_id = new Date()-0+"";
|
43
|
+
self.io.on("__linda_watch_callback_"+callback_id, callback);
|
44
|
+
self.io.push("__linda_watch", [space.name, tuple, callback_id]);
|
45
|
+
};
|
46
|
+
};
|
47
|
+
};
|
48
|
+
|
49
|
+
var RocketIO = function(opts){
|
50
|
+
new EventEmitter().apply(this);
|
51
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
52
|
+
this.type = opts.type || null; // "comet" or "websocket"
|
53
|
+
this.session = opts.session || null;
|
54
|
+
this.channel = null;
|
55
|
+
if(typeof opts.channel !== "undefined" && opts.channel !== null){
|
56
|
+
this.channel = ""+opts.channel;
|
57
|
+
}
|
58
|
+
var setting = {};
|
59
|
+
this.io = null;
|
60
|
+
var self = this;
|
61
|
+
var ws_close_timer = null;
|
62
|
+
self.on("__connect", function(session_id){
|
63
|
+
self.session = session_id;
|
64
|
+
self.io.push("__channel_id", self.channel);
|
65
|
+
self.emit("connect");
|
66
|
+
});
|
67
|
+
|
68
|
+
this.connect = function(url){
|
69
|
+
if(typeof url === "string"){
|
70
|
+
$.getJSON(url+"/rocketio/settings", function(res){
|
71
|
+
setting = res;
|
72
|
+
connect_io();
|
73
|
+
});
|
74
|
+
return self;
|
75
|
+
}
|
76
|
+
else{
|
77
|
+
return connect_io();
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
var connect_io = function(){
|
82
|
+
self.io = function(){
|
83
|
+
if(self.type === "comet") return;
|
84
|
+
if(typeof WebSocketIO !== "function") return;
|
85
|
+
var io = new WebSocketIO();
|
86
|
+
if(typeof setting.websocket === "string") io.url = setting.websocket;
|
87
|
+
io.session = self.session;
|
88
|
+
return io.connect();
|
89
|
+
}() || function(){
|
90
|
+
if(typeof CometIO !== "function") return;
|
91
|
+
var io = new CometIO();
|
92
|
+
if(typeof setting.comet === "string") io.url = setting.comet;
|
93
|
+
io.session = self.session;
|
94
|
+
return io.connect();
|
95
|
+
}();
|
96
|
+
if(typeof self.io === "undefined"){
|
97
|
+
setTimeout(function(){
|
98
|
+
self.emit("error", "WebSocketIO and CometIO are not available");
|
99
|
+
}, 100);
|
100
|
+
return self;
|
101
|
+
};
|
102
|
+
if(self.io.url.match(/^ws:\/\/.+/)) self.type = "websocket";
|
103
|
+
else if(self.io.url.match(/cometio/)) self.type = "comet";
|
104
|
+
else self.type = "unknown";
|
105
|
+
self.io.on("*", function(event_name, args){
|
106
|
+
if(event_name === "connect") event_name = "__connect";
|
107
|
+
self.emit(event_name, args);
|
108
|
+
});
|
109
|
+
ws_close_timer = setTimeout(function(){
|
110
|
+
self.close();
|
111
|
+
self.type = "comet";
|
112
|
+
connect_io();
|
113
|
+
}, 3000);
|
114
|
+
self.once("connect", function(){
|
115
|
+
if(ws_close_timer) clearTimeout(ws_close_timer);
|
116
|
+
ws_close_timer = null;
|
117
|
+
});
|
118
|
+
return self;
|
119
|
+
};
|
120
|
+
|
121
|
+
this.close = function(){
|
122
|
+
self.io.close();
|
123
|
+
};
|
124
|
+
|
125
|
+
this.push = function(type, data){
|
126
|
+
self.io.push(type, data);
|
127
|
+
};
|
128
|
+
};
|
129
|
+
|
130
|
+
var CometIO = function(url, opts){
|
131
|
+
new EventEmitter().apply(this);
|
132
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
133
|
+
this.url = url || "";
|
134
|
+
this.session = opts.session || null;
|
135
|
+
var running = false;
|
136
|
+
var self = this;
|
137
|
+
var post_queue = [];
|
138
|
+
|
139
|
+
var flush = function(){
|
140
|
+
if(!running || post_queue.length < 1) return;
|
141
|
+
var post_data = {
|
142
|
+
json: JSON.stringify({
|
143
|
+
session: self.session,
|
144
|
+
events: post_queue
|
145
|
+
})
|
146
|
+
};
|
147
|
+
$.ajax(
|
148
|
+
{
|
149
|
+
url : self.url,
|
150
|
+
data : post_data,
|
151
|
+
success : function(data){
|
152
|
+
},
|
153
|
+
error : function(req, stat, e){
|
154
|
+
self.emit("error", "CometIO push error");
|
155
|
+
},
|
156
|
+
complete : function(e){
|
157
|
+
},
|
158
|
+
type : "POST",
|
159
|
+
dataType : "json",
|
160
|
+
timeout : 10000
|
161
|
+
}
|
162
|
+
);
|
163
|
+
post_queue = [];
|
164
|
+
};
|
165
|
+
setInterval(flush, 1000);
|
166
|
+
|
167
|
+
this.push = function(type, data){
|
168
|
+
if(!running || !self.session){
|
169
|
+
self.emit("error", "CometIO not connected");
|
170
|
+
return;
|
171
|
+
}
|
172
|
+
post_queue.push({type: type, data: data})
|
173
|
+
};
|
174
|
+
|
175
|
+
this.connect = function(){
|
176
|
+
if(running) return self;
|
177
|
+
self.on("__session_id", function(session){
|
178
|
+
self.session = session;
|
179
|
+
self.emit("connect", self.session);
|
180
|
+
});
|
181
|
+
running = true;
|
182
|
+
get();
|
183
|
+
return self;
|
184
|
+
};
|
185
|
+
|
186
|
+
this.close = function(){
|
187
|
+
running = false;
|
188
|
+
self.removeListener("__session_id");
|
189
|
+
};
|
190
|
+
|
191
|
+
var get = function(){
|
192
|
+
if(!running) return;
|
193
|
+
$.ajax(
|
194
|
+
{
|
195
|
+
url : self.url,
|
196
|
+
data : {session : self.session},
|
197
|
+
success : function(data_arr){
|
198
|
+
if(data_arr !== null && typeof data_arr == "object" && !!data_arr.length){
|
199
|
+
for(var i = 0; i < data_arr.length; i++){
|
200
|
+
var data = data_arr[i];
|
201
|
+
if(data) self.emit(data.type, data.data);
|
202
|
+
}
|
203
|
+
}
|
204
|
+
get();
|
205
|
+
},
|
206
|
+
error : function(req, stat, e){
|
207
|
+
self.emit("error", "CometIO get error");
|
208
|
+
setTimeout(get, 10000);
|
209
|
+
},
|
210
|
+
complete : function(e){
|
211
|
+
},
|
212
|
+
type : "GET",
|
213
|
+
dataType : "json",
|
214
|
+
timeout : 130000
|
215
|
+
}
|
216
|
+
);
|
217
|
+
};
|
218
|
+
};
|
219
|
+
|
220
|
+
var WebSocketIO = function(url, opts){
|
221
|
+
new EventEmitter().apply(this);
|
222
|
+
if(typeof opts === "undefined" || opts === null) opts = {};
|
223
|
+
this.url = url || "";
|
224
|
+
this.session = opts.session || null;
|
225
|
+
this.websocket = null;
|
226
|
+
this.connecting = false;
|
227
|
+
var reconnect_timer_id = null;
|
228
|
+
var running = false;
|
229
|
+
var self = this;
|
230
|
+
|
231
|
+
self.on("__session_id", function(session_id){
|
232
|
+
self.session = session_id;
|
233
|
+
self.emit("connect", self.session);
|
234
|
+
});
|
235
|
+
|
236
|
+
this.connect = function(){
|
237
|
+
if(typeof WebSocket === "undefined"){
|
238
|
+
self.emit("error", "websocket not exists in this browser");
|
239
|
+
return null;
|
240
|
+
}
|
241
|
+
self.running = true;
|
242
|
+
var url = self.session ? self.url+"/session="+self.session : self.url;
|
243
|
+
self.websocket = new WebSocket(url);
|
244
|
+
self.websocket.onmessage = function(e){
|
245
|
+
try{
|
246
|
+
var data_ = JSON.parse(e.data);
|
247
|
+
self.emit(data_.type, data_.data);
|
248
|
+
}
|
249
|
+
catch(e){
|
250
|
+
self.emit("error", "WebSocketIO data parse error");
|
251
|
+
}
|
252
|
+
};
|
253
|
+
self.websocket.onclose = function(){
|
254
|
+
if(self.connecting){
|
255
|
+
self.connecting = false;
|
256
|
+
self.emit("disconnect");
|
257
|
+
}
|
258
|
+
if(self.running){
|
259
|
+
reconnect_timer_id = setTimeout(self.connect, 10000);
|
260
|
+
}
|
261
|
+
};
|
262
|
+
self.websocket.onopen = function(){
|
263
|
+
self.connecting = true;
|
264
|
+
};
|
265
|
+
return self;
|
266
|
+
};
|
267
|
+
|
268
|
+
this.close = function(){
|
269
|
+
clearTimeout(reconnect_timer_id);
|
270
|
+
self.running = false;
|
271
|
+
self.websocket.close();
|
272
|
+
};
|
273
|
+
|
274
|
+
this.push = function(type, data){
|
275
|
+
if(!self.connecting){
|
276
|
+
self.emit("error", "websocket not connected");
|
277
|
+
return;
|
278
|
+
}
|
279
|
+
self.websocket.send(JSON.stringify({type: type, data: data, session: self.session}));
|
280
|
+
};
|
281
|
+
};
|
282
|
+
|
283
|
+
// event_emitter.js v0.0.8
|
284
|
+
// https://github.com/shokai/event_emitter.js
|
285
|
+
// (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
|
286
|
+
// The MIT License
|
287
|
+
var EventEmitter = function(){
|
288
|
+
var self = this;
|
289
|
+
this.apply = function(target, prefix){
|
290
|
+
if(!prefix) prefix = "";
|
291
|
+
for(var func in self){
|
292
|
+
if(self.hasOwnProperty(func) && func !== "apply"){
|
293
|
+
target[prefix+func] = this[func];
|
294
|
+
}
|
295
|
+
}
|
296
|
+
};
|
297
|
+
this.__events = new Array();
|
298
|
+
this.on = function(type, listener, opts){
|
299
|
+
if(typeof listener !== "function") return;
|
300
|
+
var event_id = self.__events.length > 0 ? 1 + self.__events[self.__events.length-1].id : 0
|
301
|
+
var params = {
|
302
|
+
id: event_id,
|
303
|
+
type: type,
|
304
|
+
listener: listener
|
305
|
+
};
|
306
|
+
for(i in opts){
|
307
|
+
if(!params[i]) params[i] = opts[i];
|
308
|
+
};
|
309
|
+
self.__events.push(params);
|
310
|
+
return event_id;
|
311
|
+
};
|
312
|
+
|
313
|
+
this.once = function(type, listener){
|
314
|
+
self.on(type, listener, {once: true});
|
315
|
+
};
|
316
|
+
|
317
|
+
this.emit = function(type, data){
|
318
|
+
for(var i = 0; i < self.__events.length; i++){
|
319
|
+
var e = self.__events[i];
|
320
|
+
switch(e.type){
|
321
|
+
case type:
|
322
|
+
e.listener(data);
|
323
|
+
if(e.once) e.type = null;
|
324
|
+
break
|
325
|
+
case "*":
|
326
|
+
e.listener(type, data);
|
327
|
+
if(e.once) e.type = null;
|
328
|
+
break
|
329
|
+
}
|
330
|
+
}
|
331
|
+
self.removeListener();
|
332
|
+
};
|
333
|
+
|
334
|
+
this.removeListener = function(id_or_type){
|
335
|
+
for(var i = self.__events.length-1; i >= 0; i--){
|
336
|
+
var e = self.__events[i];
|
337
|
+
switch(typeof id_or_type){
|
338
|
+
case "number":
|
339
|
+
if(e.id === id_or_type) self.__events.splice(i,1);
|
340
|
+
break
|
341
|
+
case "string":
|
342
|
+
case "object":
|
343
|
+
if(e.type === id_or_type) self.__events.splice(i,1);
|
344
|
+
break
|
345
|
+
}
|
346
|
+
}
|
347
|
+
};
|
348
|
+
|
349
|
+
};
|
350
|
+
|
351
|
+
if(typeof module !== "undefined" && typeof module.exports !== "undefined"){
|
352
|
+
module.exports = EventEmitter;
|
353
|
+
}
|
354
|
+
|
355
|
+
|
data/linda.min.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
// Linda.js v0.0.8 (rocketio v0.2.6)
|
2
|
+
// https://github.com/shokai/sinatra-rocketio-linda
|
3
|
+
// (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
|
4
|
+
// The MIT License
|
5
|
+
var Linda=function(io,opts){var self=this;this.io=null;if(io===null||typeof io==="undefined"){this.io=(new RocketIO).connect()}else{this.io=io}this.opts=opts||{};this.TupleSpace=function(name){if(name===null||typeof name!=="string")name="__default__";this.name=name;this.linda=self;var space=this;this.write=function(tuple,opts){if(tuple===null||typeof tuple!=="object")return;if(opts===null||typeof opts==="undefined")opts={};self.io.push("__linda_write",[space.name,tuple,opts])};this.read=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.once("__linda_read_callback_"+callback_id,callback);self.io.push("__linda_read",[space.name,tuple,callback_id])};this.take=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.once("__linda_take_callback_"+callback_id,callback);self.io.push("__linda_take",[space.name,tuple,callback_id])};this.watch=function(tuple,callback){if(tuple===null||typeof tuple!=="object")return;if(typeof callback!=="function")return;var callback_id=new Date-0+"";self.io.on("__linda_watch_callback_"+callback_id,callback);self.io.push("__linda_watch",[space.name,tuple,callback_id])}}};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}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-rocketio-linda
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Hashimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -159,6 +159,7 @@ extra_rdoc_files: []
|
|
159
159
|
files:
|
160
160
|
- .gitignore
|
161
161
|
- Gemfile
|
162
|
+
- HEADER.erb
|
162
163
|
- History.txt
|
163
164
|
- LICENSE.txt
|
164
165
|
- README.md
|
@@ -174,6 +175,8 @@ files:
|
|
174
175
|
- lib/sinatra/rocketio/linda.rb
|
175
176
|
- lib/sinatra/rocketio/linda/client.rb
|
176
177
|
- lib/sinatra/rocketio/linda/version.rb
|
178
|
+
- linda.js
|
179
|
+
- linda.min.js
|
177
180
|
- sample/bin/cui_calc_watcher.rb
|
178
181
|
- sample/bin/cui_calc_worker.rb
|
179
182
|
- sample/config.ru
|