1 | dojo.provide("dojox.cometd.longPollTransportJsonEncoded"); |
---|
2 | dojo.require("dojox.cometd._base"); |
---|
3 | |
---|
4 | dojox.cometd.longPollTransportJsonEncoded = new function(){ |
---|
5 | // This is an alternative implementation to that provided in logPollTransportFormEncoded.js |
---|
6 | // that sends messages as text/json rather than form encoding them. |
---|
7 | |
---|
8 | this._connectionType="long-polling"; |
---|
9 | this._cometd=null; |
---|
10 | |
---|
11 | this.check = function(types, version, xdomain){ |
---|
12 | return ((!xdomain)&&(dojo.indexOf(types, "long-polling") >= 0)); |
---|
13 | } |
---|
14 | |
---|
15 | this.tunnelInit = function(){ |
---|
16 | var message = { |
---|
17 | channel: "/meta/connect", |
---|
18 | clientId: this._cometd.clientId, |
---|
19 | connectionType: this._connectionType, |
---|
20 | id: ""+this._cometd.messageId++ |
---|
21 | }; |
---|
22 | message=this._cometd._extendOut(message); |
---|
23 | this.openTunnelWith([message]); |
---|
24 | } |
---|
25 | |
---|
26 | this.tunnelCollapse = function(){ |
---|
27 | // TODO handle transport specific advice |
---|
28 | |
---|
29 | if(!this._cometd._initialized){ return; } |
---|
30 | |
---|
31 | if(this._cometd._advice && this._cometd._advice["reconnect"]=="none"){ |
---|
32 | return; |
---|
33 | } |
---|
34 | if (this._cometd._status=="connected") { |
---|
35 | setTimeout(dojo.hitch(this,function(){this._connect();}),this._cometd._interval()); |
---|
36 | }else{ |
---|
37 | setTimeout(dojo.hitch(this._cometd,function(){this.init(this.url,this._props);}),this._cometd._interval()); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | this._connect = function(){ |
---|
42 | if(!this._cometd._initialized){ return; } |
---|
43 | if(this._cometd._polling) { |
---|
44 | return; |
---|
45 | } |
---|
46 | |
---|
47 | if((this._cometd._advice) && (this._cometd._advice["reconnect"]=="handshake")){ |
---|
48 | this._cometd._status="unconnected"; |
---|
49 | this._initialized = false; |
---|
50 | this._cometd.init(this._cometd.url,this._cometd._props); |
---|
51 | }else if(this._cometd._status=="connected"){ |
---|
52 | var message={ |
---|
53 | channel: "/meta/connect", |
---|
54 | connectionType: this._connectionType, |
---|
55 | clientId: this._cometd.clientId, |
---|
56 | id: ""+this._cometd.messageId++ |
---|
57 | }; |
---|
58 | if (this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){ |
---|
59 | message.advice={timeout:(this._cometd.connectTimeout-this._cometd.expectedNetworkDelay)}; |
---|
60 | } |
---|
61 | message=this._cometd._extendOut(message); |
---|
62 | this.openTunnelWith([message]); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | this.deliver = function(message){ |
---|
67 | // Nothing to do |
---|
68 | } |
---|
69 | |
---|
70 | this.openTunnelWith = function(messages, url){ |
---|
71 | this._cometd._polling = true; |
---|
72 | var post = { |
---|
73 | url: (url||this._cometd.url), |
---|
74 | postData: dojo.toJson(messages), |
---|
75 | contentType: "text/json;charset=UTF-8", |
---|
76 | handleAs: this._cometd.handleAs, |
---|
77 | load: dojo.hitch(this, function(data){ |
---|
78 | this._cometd._polling=false; |
---|
79 | this._cometd.deliver(data); |
---|
80 | this._cometd._backon(); |
---|
81 | this.tunnelCollapse(); |
---|
82 | }), |
---|
83 | error: dojo.hitch(this, function(err){ |
---|
84 | this._cometd._polling=false; |
---|
85 | var metaMsg = { |
---|
86 | failure: true, |
---|
87 | error: err, |
---|
88 | advice: this._cometd._advice |
---|
89 | }; |
---|
90 | this._cometd._publishMeta("connect",false, metaMsg); |
---|
91 | this._cometd._backoff(); |
---|
92 | this.tunnelCollapse(); |
---|
93 | }) |
---|
94 | }; |
---|
95 | |
---|
96 | var connectTimeout=this._cometd._connectTimeout(); |
---|
97 | if (connectTimeout>0) { |
---|
98 | post.timeout=connectTimeout; |
---|
99 | } |
---|
100 | |
---|
101 | this._poll = dojo.rawXhrPost(post); |
---|
102 | } |
---|
103 | |
---|
104 | this.sendMessages = function(messages){ |
---|
105 | for(var i=0; i<messages.length; i++){ |
---|
106 | messages[i].clientId = this._cometd.clientId; |
---|
107 | messages[i].id = ""+this._cometd.messageId++; |
---|
108 | messages[i]=this._cometd._extendOut(messages[i]); |
---|
109 | } |
---|
110 | return dojo.rawXhrPost({ |
---|
111 | url: this._cometd.url||dojo.config["cometdRoot"], |
---|
112 | handleAs: this._cometd.handleAs, |
---|
113 | load: dojo.hitch(this._cometd, "deliver"), |
---|
114 | postData: dojo.toJson(messages), |
---|
115 | contentType: "text/json;charset=UTF-8", |
---|
116 | error: dojo.hitch(this, function(err){ |
---|
117 | this._cometd._publishMeta("publish",false,{messages:messages}); |
---|
118 | }), |
---|
119 | timeout: this._cometd.expectedNetworkDelay |
---|
120 | }); |
---|
121 | } |
---|
122 | |
---|
123 | this.startup = function(handshakeData){ |
---|
124 | if(this._cometd._status=="connected"){ return; } |
---|
125 | this.tunnelInit(); |
---|
126 | } |
---|
127 | |
---|
128 | this.disconnect = function(){ |
---|
129 | var message = { |
---|
130 | channel: "/meta/disconnect", |
---|
131 | clientId: this._cometd.clientId, |
---|
132 | id: "" + this._cometd.messageId++ |
---|
133 | }; |
---|
134 | message = this._cometd._extendOut(message); |
---|
135 | dojo.rawXhrPost({ |
---|
136 | url: this._cometd.url || dojo.config["cometdRoot"], |
---|
137 | handleAs: this._cometd.handleAs, |
---|
138 | postData: dojo.toJson([message]), |
---|
139 | contentType: "text/json;charset=UTF-8" |
---|
140 | }); |
---|
141 | } |
---|
142 | |
---|
143 | this.cancelConnect = function(){ |
---|
144 | if(this._poll){ |
---|
145 | this._poll.cancel(); |
---|
146 | this._cometd._polling=false; |
---|
147 | this._cometd._publishMeta("connect",false,{cancel:true}); |
---|
148 | this._cometd._backoff(); |
---|
149 | this.disconnect(); |
---|
150 | this.tunnelCollapse(); |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | dojox.cometd.longPollTransport = dojox.cometd.longPollTransportJsonEncoded; |
---|
156 | |
---|
157 | dojox.cometd.connectionTypes.register("long-polling", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded); |
---|
158 | dojox.cometd.connectionTypes.register("long-polling-json-encoded", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded); |
---|
159 | |
---|