timesync.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (c) 2004-2012, 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. if(!dojo._hasResource["dojox.cometd.timesync"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.cometd.timesync"] = true;
  8. dojo.provide("dojox.cometd.timesync");
  9. dojo.require("dojox.cometd._base");
  10. /**
  11. * this file provides the time synchronization extension to cometd.
  12. * Timesync allows the client and server to exchange time information on every
  13. * handshake and connect message so that the client may calculate an approximate
  14. * offset from it's own clock epoch to that of the server.
  15. *
  16. * With each handshake or connect, the extension sends timestamps within the
  17. * ext field like: <code>{ext:{timesync:{tc:12345567890,l:23,o:4567},...},...}</code>
  18. * where:<ul>
  19. * <li>tc is the client timestamp in ms since 1970 of when the message was sent.
  20. * <li>l is the network lag that the client has calculated.
  21. * <li>o is the clock offset that the client has calculated.
  22. * </ul>
  23. * The accuracy of the offset and lag may be calculated with tc-now-l-o,
  24. * which should be zero if the calculated offset and lag are perfectly
  25. * accurate.
  26. * <p>
  27. * A cometd server that supports timesync, should respond only if the
  28. * measured accuracy value is greater than accuracy target. The response
  29. * will be an ext field like: <code>{ext:{timesync:{tc:12345567890,ts:1234567900,p:123,a:3},...},...}</code>
  30. * where:<ul>
  31. * <li>tc is the client timestamp of when the message was sent,
  32. * <li>ts is the server timestamp of when the message was received
  33. * <li>p is the poll duration in ms - ie the time the server took before sending the response.
  34. * <li>a is the measured accuracy of the calculated offset and lag sent by the client
  35. * </ul>
  36. *
  37. * On receipt of the response, the client is able to use current time to determine
  38. * the total trip time, from which p is subtracted to determine an approximate
  39. * two way network traversal time. The measured accuracy is used to adjust the assumption
  40. * that the network is symmetric for traversal time, so: <ul>
  41. * <li>lag = (now-tc-p)/2-a
  42. * <li>offset = ts-tc-lag
  43. * </ul>
  44. *
  45. * In order to smooth over any transient fluctuations, the extension keeps a sliding
  46. * average of the offsets received. By default this is over 10 messages, but this can
  47. * be changed with the dojox.cometd.timesync._window element.
  48. */
  49. dojox.cometd.timesync = new function(){
  50. this._window = 10; // The window size for the sliding average of offset samples.
  51. this._lags = []; // The samples used to calculate the average lag.
  52. this._offsets = []; // The samples used to calculate the average offset.
  53. this.lag=0; // The calculated network lag from client to server
  54. this.offset = 0; // The offset in ms between the clients clock and the servers clock.
  55. this.samples = 0; // The number of samples used to calculate the offset. If 0, the offset is not valid.
  56. this.getServerTime = function(){ // return: long
  57. // Summary:
  58. // Calculate the current time on the server
  59. //
  60. return new Date().getTime()+this.offset;
  61. }
  62. this.getServerDate = function(){ // return: Date
  63. // Summary:
  64. // Calculate the current time on the server
  65. //
  66. return new Date(this.getServerTime());
  67. }
  68. this.setTimeout = function(/*function*/call, /*long|Date*/atTimeOrDate){
  69. // Summary:
  70. // Set a timeout function relative to server time
  71. // call:
  72. // the function to call when the timeout occurs
  73. // atTimeOrTime:
  74. // a long timestamp or a Date representing the server time at
  75. // which the timeout should occur.
  76. var ts = (atTimeOrDate instanceof Date) ? atTimeOrDate.getTime() : (0 + atTimeOrDate);
  77. var tc = ts - this.offset;
  78. var interval = tc - new Date().getTime();
  79. if(interval <= 0){
  80. interval = 1;
  81. }
  82. return setTimeout(call,interval);
  83. }
  84. this._in = function(/*Object*/msg){
  85. // Summary:
  86. // Handle incoming messages for the timesync extension.
  87. // description:
  88. // Look for ext:{timesync:{}} field and calculate offset if present.
  89. // msg:
  90. // The incoming bayeux message
  91. var channel = msg.channel;
  92. if(channel && channel.indexOf('/meta/') == 0){
  93. if(msg.ext && msg.ext.timesync){
  94. var sync = msg.ext.timesync;
  95. var now = new Date().getTime();
  96. var l=(now-sync.tc-sync.p)/2-sync.a;
  97. var o=sync.ts-sync.tc-l;
  98. this._lags.push(l);
  99. this._offsets.push(o);
  100. if(this._offsets.length > this._window){
  101. this._offsets.shift();
  102. this._lags.shift();
  103. }
  104. this.samples++;
  105. l=0;
  106. o=0;
  107. for(var i in this._offsets){
  108. l+=this._lags[i];
  109. o+=this._offsets[i];
  110. }
  111. this.offset = parseInt((o / this._offsets.length).toFixed());
  112. this.lag = parseInt((l / this._lags.length).toFixed());
  113. }
  114. }
  115. return msg;
  116. }
  117. this._out = function(msg){
  118. // Summary:
  119. // Handle outgoing messages for the timesync extension.
  120. // description:
  121. // Look for handshake and connect messages and add the ext:{timesync:{}} fields
  122. // msg:
  123. // The outgoing bayeux message
  124. var channel = msg.channel;
  125. if(channel && channel.indexOf('/meta/') == 0){
  126. var now = new Date().getTime();
  127. if(!msg.ext){
  128. msg.ext = {};
  129. }
  130. msg.ext.timesync = {tc:now,l:this.lag,o:this.offset};
  131. }
  132. return msg;
  133. }
  134. };
  135. dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd.timesync, "_in"));
  136. dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd.timesync, "_out"));
  137. }