blob: 5526444b4e419d41955105bbbb0a8d1af61ea526 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/* Inspired from https://gist.github.com/ismasan/299789 (MIT licensed) */
$(function() {
ServerEventsDispatcher = function(url){
var callbacks = {};
var is_open = false;
var timeout = null;
var timeoutInterval = 2; /* s */
var self = this;
var ws = null;
this.open = function(){
ws = new WebSocket(url);
ws.binaryType = "arraybuffer";
// dispatch to the right handlers
ws.onmessage = function(evt){
var json = JSON.parse(evt.data)
//console.log("EVENT", json);
dispatch(json);
};
ws.onclose = function(){
is_open = false;
dispatch({action: 'delete', object_name: 'local.connection'})
timeout = setTimeout(function() { self.open(); }, self.timeoutInterval);
}
ws.onopen = function() {
if (timeout)
clearTimeout(timeout);
is_open = true;
dispatch({action: 'insert', object_name: 'local.connection'})
}
};
this.open();
this.bind = function(action, object_name, callback){
var key = action + object_name;
callbacks[key] = callbacks[key] || [];
callbacks[key].push(callback);
return this;// chainable
};
// XXX bad
this.send = function(query){
var payload = JSON.stringify(query);
ws.send( payload ); // <= send JSON data to socket server
return this;
};
var dispatch = function(query) {
var key = query.action + query.object_name;
var chain = callbacks[key];
if(typeof chain === 'undefined') {
return; // no callbacks for this event
}
for(var i = 0; i < chain.length; i++){
chain[i](query)
}
}
};
});
|