root / trunk / web / dojo / dojox / cometd / timesync.js @ 11
History | View | Annotate | Download (1.7 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.cometd.timesync"]){ |
9 |
dojo._hasResource["dojox.cometd.timesync"]=true; |
10 |
dojo.provide("dojox.cometd.timesync");
|
11 |
dojo.require("dojox.cometd._base");
|
12 |
dojox.cometd.timesync=new function(){ |
13 |
this._window=10; |
14 |
this._lags=[];
|
15 |
this._offsets=[];
|
16 |
this.lag=0; |
17 |
this.offset=0; |
18 |
this.samples=0; |
19 |
this.getServerTime=function(){ |
20 |
return new Date().getTime()+this.offset; |
21 |
}; |
22 |
this.getServerDate=function(){ |
23 |
return new Date(this.getServerTime()); |
24 |
}; |
25 |
this.setTimeout=function(_1,_2){ |
26 |
var ts=(_2 instanceof Date)?_2.getTime():(0+_2); |
27 |
var tc=ts-this.offset; |
28 |
var _3=tc-new Date().getTime(); |
29 |
if(_3<=0){ |
30 |
_3=1;
|
31 |
} |
32 |
return setTimeout(_1,_3);
|
33 |
}; |
34 |
this._in=function(_4){ |
35 |
var _5=_4.channel;
|
36 |
if(_5&&_5.indexOf("/meta/")==0){ |
37 |
if(_4.ext&&_4.ext.timesync){
|
38 |
var _6=_4.ext.timesync;
|
39 |
var _7=new Date().getTime(); |
40 |
var l=(_7-_6.tc-_6.p)/2-_6.a; |
41 |
var o=_6.ts-_6.tc-l;
|
42 |
this._lags.push(l);
|
43 |
this._offsets.push(o);
|
44 |
if(this._offsets.length>this._window){ |
45 |
this._offsets.shift();
|
46 |
this._lags.shift();
|
47 |
} |
48 |
this.samples++;
|
49 |
l=0;
|
50 |
o=0;
|
51 |
for(var i in this._offsets){ |
52 |
l+=this._lags[i];
|
53 |
o+=this._offsets[i];
|
54 |
} |
55 |
this.offset=parseInt((o/this._offsets.length).toFixed()); |
56 |
this.lag=parseInt((l/this._lags.length).toFixed()); |
57 |
} |
58 |
} |
59 |
return _4;
|
60 |
}; |
61 |
this._out=function(_8){ |
62 |
var _9=_8.channel;
|
63 |
if(_9&&_9.indexOf("/meta/")==0){ |
64 |
var _a=new Date().getTime(); |
65 |
if(!_8.ext){
|
66 |
_8.ext={}; |
67 |
} |
68 |
_8.ext.timesync={tc:_a,l:this.lag,o:this.offset}; |
69 |
} |
70 |
return _8;
|
71 |
}; |
72 |
}; |
73 |
dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd.timesync,"_in"));
|
74 |
dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd.timesync,"_out"));
|
75 |
} |