2 * Copyright (c) 2010 the original author or authors.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
19 function bind($, org_cometd)
\r
21 // Remap cometd JSON functions to jquery JSON functions
\r
22 org_cometd.JSON.toJSON = JSON.stringify;
\r
23 org_cometd.JSON.fromJSON = JSON.parse;
\r
25 function _setHeaders(xhr, headers)
\r
29 for (var headerName in headers)
\r
31 if (headerName.toLowerCase() === 'content-type')
\r
35 xhr.setRequestHeader(headerName, headers[headerName]);
\r
40 // Remap toolkit-specific transport calls
\r
41 function LongPollingTransport()
\r
43 var _super = new org_cometd.LongPollingTransport();
\r
44 var that = org_cometd.Transport.derive(_super);
\r
46 that.xhrSend = function(packet)
\r
50 async: packet.sync !== true,
\r
52 contentType: 'application/json;charset=UTF-8',
\r
55 // Has no effect if the request is not cross domain
\r
56 // but if it is, allows cookies to be sent to the server
\r
57 withCredentials: true
\r
59 beforeSend: function(xhr)
\r
61 _setHeaders(xhr, packet.headers);
\r
62 // Returning false will abort the XHR send
\r
65 success: packet.onSuccess,
\r
66 error: function(xhr, reason, exception)
\r
68 packet.onError(reason, exception);
\r
76 function CallbackPollingTransport()
\r
78 var _super = new org_cometd.CallbackPollingTransport();
\r
79 var that = org_cometd.Transport.derive(_super);
\r
81 that.jsonpSend = function(packet)
\r
85 async: packet.sync !== true,
\r
90 // In callback-polling, the content must be sent via the 'message' parameter
\r
91 message: packet.body
\r
93 beforeSend: function(xhr)
\r
95 _setHeaders(xhr, packet.headers);
\r
96 // Returning false will abort the XHR send
\r
99 success: packet.onSuccess,
\r
100 error: function(xhr, reason, exception)
\r
102 packet.onError(reason, exception);
\r
110 $.Cometd = function(name)
\r
112 var cometd = new org_cometd.Cometd(name);
\r
114 // Registration order is important
\r
115 if (org_cometd.WebSocket)
\r
117 cometd.registerTransport('websocket', new org_cometd.WebSocketTransport());
\r
119 cometd.registerTransport('long-polling', new LongPollingTransport());
\r
120 cometd.registerTransport('callback-polling', new CallbackPollingTransport());
\r
125 // The default cometd instance
\r
126 $.cometd = new $.Cometd();
\r
131 if (typeof define === 'function' && define.amd)
\r
133 define(['jquery', 'org/cometd'], bind);
\r
137 bind($, org.cometd);
\r