1 | dojo.provide("dojox.cometd.callbackPollTransport"); |
---|
2 | dojo.require("dojox.cometd._base"); |
---|
3 | dojo.require("dojox.cometd.longPollTransport"); |
---|
4 | dojo.require("dojo.io.script"); |
---|
5 | |
---|
6 | dojox.cometd.callbackPollTransport = new function(){ |
---|
7 | |
---|
8 | this._connectionType = "callback-polling"; |
---|
9 | this._cometd = null; |
---|
10 | |
---|
11 | this.check = function(types, version, xdomain){ |
---|
12 | // we handle x-domain! |
---|
13 | return (dojo.indexOf(types, "callback-polling") >= 0); |
---|
14 | } |
---|
15 | |
---|
16 | this.tunnelInit = function(){ |
---|
17 | var message = { |
---|
18 | channel: "/meta/connect", |
---|
19 | clientId: this._cometd.clientId, |
---|
20 | connectionType: this._connectionType, |
---|
21 | id: "" + this._cometd.messageId++ |
---|
22 | }; |
---|
23 | message = this._cometd._extendOut(message); |
---|
24 | this.openTunnelWith([message]); |
---|
25 | } |
---|
26 | |
---|
27 | this.tunnelCollapse = dojox.cometd.longPollTransport.tunnelCollapse; |
---|
28 | this._connect = dojox.cometd.longPollTransport._connect; |
---|
29 | this.deliver = dojox.cometd.longPollTransport.deliver; |
---|
30 | |
---|
31 | this.openTunnelWith = function(content, url){ |
---|
32 | this._cometd._polling = true; |
---|
33 | var script = { |
---|
34 | load: dojo.hitch(this, function(data){ |
---|
35 | this._cometd._polling=false; |
---|
36 | this._cometd.deliver(data); |
---|
37 | this._cometd._backon(); |
---|
38 | this.tunnelCollapse(); |
---|
39 | }), |
---|
40 | error: dojo.hitch(this, function(err){ |
---|
41 | this._cometd._polling = false; |
---|
42 | this._cometd._publishMeta("connect",false); |
---|
43 | this._cometd._backoff(); |
---|
44 | this.tunnelCollapse(); |
---|
45 | }), |
---|
46 | url: (url || this._cometd.url), |
---|
47 | content: { message: dojo.toJson(content) }, |
---|
48 | callbackParamName: "jsonp" |
---|
49 | }; |
---|
50 | var connectTimeout = this._cometd._connectTimeout(); |
---|
51 | if(connectTimeout > 0){ |
---|
52 | script.timeout=connectTimeout; |
---|
53 | } |
---|
54 | dojo.io.script.get(script); |
---|
55 | } |
---|
56 | |
---|
57 | this.sendMessages = function(/*array*/ messages){ |
---|
58 | for(var i = 0; i < messages.length; i++){ |
---|
59 | messages[i].clientId = this._cometd.clientId; |
---|
60 | messages[i].id = ""+this._cometd.messageId++; |
---|
61 | messages[i]=this._cometd._extendOut(messages[i]); |
---|
62 | } |
---|
63 | |
---|
64 | var bindArgs = { |
---|
65 | url: this._cometd.url || dojo.config["cometdRoot"], |
---|
66 | load: dojo.hitch(this._cometd, "deliver"), |
---|
67 | callbackParamName: "jsonp", |
---|
68 | content: { message: dojo.toJson( messages ) }, |
---|
69 | error: dojo.hitch(this, function(err){ |
---|
70 | this._cometd._publishMeta("publish",false,{messages:messages}); |
---|
71 | }), |
---|
72 | timeout: this._cometd.expectedNetworkDelay |
---|
73 | }; |
---|
74 | return dojo.io.script.get(bindArgs); |
---|
75 | } |
---|
76 | |
---|
77 | this.startup = function(handshakeData){ |
---|
78 | if(this._cometd._connected){ return; } |
---|
79 | this.tunnelInit(); |
---|
80 | } |
---|
81 | |
---|
82 | // FIXME: what is this supposed to do? ;) |
---|
83 | this.disconnect = dojox.cometd.longPollTransport.disconnect; |
---|
84 | this.disconnect = function(){ |
---|
85 | var message = { |
---|
86 | channel: "/meta/disconnect", |
---|
87 | clientId: this._cometd.clientId, |
---|
88 | id: "" + this._cometd.messageId++ |
---|
89 | }; |
---|
90 | message = this._cometd._extendOut(message); |
---|
91 | dojo.io.script.get({ |
---|
92 | url: this._cometd.url || dojo.config["cometdRoot"], |
---|
93 | callbackParamName: "jsonp", |
---|
94 | content: { message: dojo.toJson([message]) } |
---|
95 | }); |
---|
96 | } |
---|
97 | |
---|
98 | this.cancelConnect = function(){} |
---|
99 | } |
---|
100 | |
---|
101 | dojox.cometd.connectionTypes.register("callback-polling", dojox.cometd.callbackPollTransport.check, dojox.cometd.callbackPollTransport); |
---|
102 | |
---|