1 | dojo.provide("dojox.cometd.ack"); |
---|
2 | dojo.require("dojox.cometd._base"); |
---|
3 | |
---|
4 | /* |
---|
5 | * This file provides the dojox cometd ack extension which |
---|
6 | * acknowledges the messages received in /meta/connect responses. |
---|
7 | * Each meta/connect is sent with the id of the last successful meta/connect |
---|
8 | * received. The server uses this information to manage a queue of unacknowleged |
---|
9 | * messages. |
---|
10 | * |
---|
11 | * To use, add dojo.require("dojox.cometd.ack"); and if the handshake will be sent |
---|
12 | * with ext:{ack:true}. If the server supports the same extension, then the |
---|
13 | * mechanism will be initialized. The dojox.cometd.ackEnabled field may also be |
---|
14 | * used to optionally enable/disable the extension before init of cometd. |
---|
15 | * |
---|
16 | */ |
---|
17 | dojox.cometd._ack = new function(){ |
---|
18 | var supportAcks = false; |
---|
19 | var lastAck = -1; |
---|
20 | |
---|
21 | this._in = function(msg){ |
---|
22 | if (msg.channel == "/meta/handshake") { |
---|
23 | supportAcks = msg.ext && msg.ext.ack; |
---|
24 | } else if (supportAcks && msg.channel == "/meta/connect" && msg.ext && msg.ext.ack && msg.successful) { |
---|
25 | var ackId = parseInt(msg.ext.ack); |
---|
26 | lastAck = ackId; |
---|
27 | } |
---|
28 | return msg; |
---|
29 | } |
---|
30 | |
---|
31 | this._out = function(msg){ |
---|
32 | |
---|
33 | if (msg.channel == "/meta/handshake") { |
---|
34 | if (!msg.ext) |
---|
35 | msg.ext = {}; |
---|
36 | msg.ext.ack = dojox.cometd.ackEnabled; |
---|
37 | lastAck = -1; |
---|
38 | } |
---|
39 | if (supportAcks && msg.channel == "/meta/connect") { |
---|
40 | if (!msg.ext) |
---|
41 | msg.ext = {}; |
---|
42 | msg.ext.ack = lastAck; |
---|
43 | } |
---|
44 | return msg; |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd._ack, "_in")); |
---|
49 | dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd._ack, "_out")); |
---|
50 | dojox.cometd.ackEnabled = true; |
---|