-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComet.js
82 lines (66 loc) · 2.1 KB
/
Comet.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Wader Comet
*
* @author Max Maximov <[email protected]>
* @version 0.3
*/
(function(ns) {
"use strict";
$.Class.extend("wader.Comet",
/* @static */
{
_instance: null,
getInstance: function (url) {
if (!wader.Comet._instance) wader.Comet._instance = new wader.Comet(url);
return wader.Comet._instance;
},
send: function (channel, data) {
if (!wader.Comet._instance) throw new Error("wader.Comet: класс должен быть инстанцирован");
wader.Comet.getInstance().send(channel, data);
}
},
/* @prototype */
{
url: null,
init: function (url) {
this.url = url;
var that = this;
setTimeout(function () { // подавляем спиннер в Файрфоксе
that._poll();
}, 1000)
},
send: function (channel, data) {
Logger.info(this, "sending to " + this.url, channel, data);
$.ajax({
url: this.url,
timeout: 10000,
async: true,
cache: false,
dataType: "jsonp",
data: { "channel": channel, "data": data }
});
},
_poll: function () {
Logger.info(this, "polling to " + this.url);
$.ajax({
url: this.url,
success: this.proxy("_onSuccess"),
error: this.proxy("_onError"),
timeout: 10000,
async: true,
cache: false,
dataType: "jsonp"
});
},
_onError: function (jqXHR, status, error) {
Logger.error(this, this.url, status, error);
this._poll();
},
_onSuccess: function (data) {
Logger.info(this, "got message", data.data, "to channel \"" + data.channel + "\"");
Hub.pub(data.channel, data.data);
this._poll();
}
});
if (ns !== wader) ns.Comet = wader.Comet;
})(window.WADER_NS || window);