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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
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
*/
dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.cometd.longPollTransportJsonEncoded"],
["require", "dojox.cometd._base"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"] = true;
dojo.provide("dojox.cometd.longPollTransportJsonEncoded");
dojo.require("dojox.cometd._base");
dojox.cometd.longPollTransportJsonEncoded = new function(){
// This is an alternative implementation to that provided in logPollTransportFormEncoded.js
// that sends messages as text/json rather than form encoding them.
this._connectionType="long-polling";
this._cometd=null;
this.check = function(types, version, xdomain){
return ((!xdomain)&&(dojo.indexOf(types, "long-polling") >= 0));
}
this.tunnelInit = function(){
var message = {
channel: "/meta/connect",
clientId: this._cometd.clientId,
connectionType: this._connectionType,
id: ""+this._cometd.messageId++
};
message=this._cometd._extendOut(message);
this.openTunnelWith([message]);
}
this.tunnelCollapse = function(){
// TODO handle transport specific advice
if(!this._cometd._initialized){ return; }
if(this._cometd._advice && this._cometd._advice["reconnect"]=="none"){
return;
}
if (this._cometd._status=="connected") {
setTimeout(dojo.hitch(this,function(){this._connect();}),this._cometd._interval());
}else{
setTimeout(dojo.hitch(this._cometd,function(){this.init(this.url,this._props);}),this._cometd._interval());
}
}
this._connect = function(){
if(!this._cometd._initialized){ return; }
if(this._cometd._polling) {
return;
}
if((this._cometd._advice) && (this._cometd._advice["reconnect"]=="handshake")){
this._cometd._status="unconnected";
this._initialized = false;
this._cometd.init(this._cometd.url,this._cometd._props);
}else if(this._cometd._status=="connected"){
var message={
channel: "/meta/connect",
connectionType: this._connectionType,
clientId: this._cometd.clientId,
id: ""+this._cometd.messageId++
};
if (this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){
message.advice={timeout:(this._cometd.connectTimeout-this._cometd.expectedNetworkDelay)};
}
message=this._cometd._extendOut(message);
this.openTunnelWith([message]);
}
}
this.deliver = function(message){
// Nothing to do
}
this.openTunnelWith = function(messages, url){
this._cometd._polling = true;
var post = {
url: (url||this._cometd.url),
postData: dojo.toJson(messages),
contentType: "text/json;charset=UTF-8",
handleAs: this._cometd.handleAs,
load: dojo.hitch(this, function(data){
this._cometd._polling=false;
this._cometd.deliver(data);
this._cometd._backon();
this.tunnelCollapse();
}),
error: dojo.hitch(this, function(err){
this._cometd._polling=false;
var metaMsg = {
failure: true,
error: err,
advice: this._cometd._advice
};
this._cometd._publishMeta("connect",false, metaMsg);
this._cometd._backoff();
this.tunnelCollapse();
})
};
var connectTimeout=this._cometd._connectTimeout();
if (connectTimeout>0) {
post.timeout=connectTimeout;
}
this._poll = dojo.rawXhrPost(post);
}
this.sendMessages = function(messages){
for(var i=0; i<messages.length; i++){
messages[i].clientId = this._cometd.clientId;
messages[i].id = ""+this._cometd.messageId++;
messages[i]=this._cometd._extendOut(messages[i]);
}
return dojo.rawXhrPost({
url: this._cometd.url||dojo.config["cometdRoot"],
handleAs: this._cometd.handleAs,
load: dojo.hitch(this._cometd, "deliver"),
postData: dojo.toJson(messages),
contentType: "text/json;charset=UTF-8",
error: dojo.hitch(this, function(err){
this._cometd._publishMeta("publish",false,{messages:messages});
}),
timeout: this._cometd.expectedNetworkDelay
});
}
this.startup = function(handshakeData){
if(this._cometd._status=="connected"){ return; }
this.tunnelInit();
}
this.disconnect = function(){
var message = {
channel: "/meta/disconnect",
clientId: this._cometd.clientId,
id: "" + this._cometd.messageId++
};
message = this._cometd._extendOut(message);
dojo.rawXhrPost({
url: this._cometd.url || dojo.config["cometdRoot"],
handleAs: this._cometd.handleAs,
postData: dojo.toJson([message]),
contentType: "text/json;charset=UTF-8"
});
}
this.cancelConnect = function(){
if(this._poll){
this._poll.cancel();
this._cometd._polling=false;
this._cometd._publishMeta("connect",false,{cancel:true});
this._cometd._backoff();
this.disconnect();
this.tunnelCollapse();
}
}
}
dojox.cometd.longPollTransport = dojox.cometd.longPollTransportJsonEncoded;
dojox.cometd.connectionTypes.register("long-polling", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
dojox.cometd.connectionTypes.register("long-polling-json-encoded", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
}
}};});
|