summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/cometd/ack.js
blob: 395c1dc56fe0538eeaed819957a7734672bdbbd5 (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
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.cometd.ack"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.cometd.ack"] = true;
dojo.provide("dojox.cometd.ack");
dojo.require("dojox.cometd._base");

/*
 * This file provides the dojox cometd ack extension which
 * acknowledges the messages received in /meta/connect responses.
 * Each meta/connect is sent with the id of the last successful meta/connect
 * received.  The server uses this information to manage a queue of unacknowleged
 * messages.
 *
 * To use, add dojo.require("dojox.cometd.ack"); and if the handshake will be sent
 * with ext:{ack:true}.  If the server supports the same extension, then the
 * mechanism will be initialized.  The dojox.cometd.ackEnabled field may also be
 * used to optionally enable/disable the extension before init of cometd.
 *
 */
dojox.cometd._ack = new function(){
	var supportAcks = false;
	var lastAck = -1;
	
	this._in = function(msg){
		if (msg.channel == "/meta/handshake") {
			supportAcks = msg.ext && msg.ext.ack;
		} else if (supportAcks && msg.channel == "/meta/connect" && msg.ext && msg.ext.ack && msg.successful) {
			var ackId = parseInt(msg.ext.ack);
			lastAck = ackId;
		}
		return msg;
	}
	
	this._out = function(msg){
	
		if (msg.channel == "/meta/handshake") {
			if (!msg.ext)
				msg.ext = {};
			msg.ext.ack = dojox.cometd.ackEnabled;
			lastAck = -1;
		}
		if (supportAcks && msg.channel == "/meta/connect") {
			if (!msg.ext)
				msg.ext = {};
			msg.ext.ack = lastAck;
		}
		return msg;
	}
};

dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd._ack, "_in"));
dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd._ack, "_out"));
dojox.cometd.ackEnabled = true;

}