forked from mamonth/yam-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comet.js
100 lines (90 loc) · 3.14 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* App Comet module
*
* @author Max Maximov <[email protected]>
* @version 0.2.1
*/
define("app/Comet", ["app/Hub", "app/Logger","app/App", "app/User"], function(Hub, Logger, App, User) {
"use strict";
$.Class.extend("app.Comet",
/* @static */
{
_instance: null,
getInstance: function (url) {
if (!app.Comet._instance) app.Comet._instance = new app.Comet(url);
return app.Comet._instance;
}
},
/* @prototype */
{
url: null,
stop:false,
timer:null,
lastId:0,
lastBroadcastId:0,
errorsCnt:0,
init: function (url) {
Logger.warn(this,"START");
this.url = url;
this.key = keyGen();
this._poll();
Hub.sub("Commet.reload",this.callback('reInit'));
},
reInit:function(){
Logger.warn(this,"Commet REINIT");
var instance = this;
var url = instance.url;
instance.stop = true;
clearTimeout(instance.timer);
app.Comet._instance = new app.Comet(url);
},
_poll: function () {
Logger.info(this, this.key, "polling to " + this.url);
this.connect(this.url,this.proxy("_onSuccess"),this.proxy("_onError"));
},
_onError: function (jqXHR, status, error) {
if(this.stop){ Logger.warn(this, this.key,"Commet STOP with error"); return; }
Logger.error(this, this.url, status, error);
this.errorsCnt++;
if(this.errorsCnt>20){this.errorsCnt=20;}
setTimeout( this.proxy('_poll'), 3000*this.errorsCnt );
},
_onSuccess: function (data) {
if(this.stop){ Logger.warn(this, this.key,"Commet STOP");return; }
this.lastId=data.id;
this.lastBroadcastId = data.broadcastId ||0;
if(data.time){ Hub.pub("Time.sync",data.time); }
this.errorsCnt=0;
if(data.isAuthorized == false || ( User.userId && data.userId != User.userId )){
if(location.href.indexOf("/u/") == -1){
location.reload();
}else{
location = "/";
}
return; }
for(var i in data.stack)
{
if(data.stack[i].channel){
Logger.info(this, "got message id:",data.id," data:", data.stack[i].data, "to channel \"" +data.stack[i].channel + "\"");
Hub.pub(data.stack[i].channel, data.stack[i].data);
}
}
//this.timer = setTimeout(this.proxy('_poll'),100);
this._poll();
},
connect:function(url,success,error)
{
$.ajax({
url: url,
success: success,
error: error,
data: { "lastId": this.lastId ,"lastBroadcastId":this.lastBroadcastId },
timeout: 35000,
async: true,
cache: false,
dataType: "json"
});
}
});
return app.Comet;
});