Xander Uiterlinden | 82ac846 | 2012-06-06 07:03:59 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2010 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | dojo.provide('dojox.cometd'); |
| 18 | dojo.registerModulePath('org','../org'); |
| 19 | dojo.require('org.cometd'); |
| 20 | dojo.require('dojo.io.script'); |
| 21 | |
| 22 | // Remap cometd JSON functions to dojo JSON functions |
| 23 | org.cometd.JSON.toJSON = dojo.toJson; |
| 24 | org.cometd.JSON.fromJSON = dojo.fromJson; |
| 25 | |
| 26 | dojox.Cometd = function(name) |
| 27 | { |
| 28 | var cometd = new org.cometd.Cometd(name); |
| 29 | |
| 30 | function LongPollingTransport() |
| 31 | { |
| 32 | var _super = new org.cometd.LongPollingTransport(); |
| 33 | var that = org.cometd.Transport.derive(_super); |
| 34 | |
| 35 | that.xhrSend = function(packet) |
| 36 | { |
| 37 | var deferred = dojo.rawXhrPost({ |
| 38 | url: packet.url, |
| 39 | sync: packet.sync === true, |
| 40 | contentType: 'application/json;charset=UTF-8', |
| 41 | headers: packet.headers, |
| 42 | postData: packet.body, |
| 43 | withCredentials: true, |
| 44 | handleAs: 'json', |
| 45 | load: packet.onSuccess, |
| 46 | error: function(error) |
| 47 | { |
| 48 | packet.onError(error.message, deferred ? deferred.ioArgs.error : error); |
| 49 | } |
| 50 | }); |
| 51 | return deferred.ioArgs.xhr; |
| 52 | }; |
| 53 | |
| 54 | return that; |
| 55 | } |
| 56 | |
| 57 | function CallbackPollingTransport() |
| 58 | { |
| 59 | var _super = new org.cometd.CallbackPollingTransport(); |
| 60 | var that = org.cometd.Transport.derive(_super); |
| 61 | |
| 62 | that.jsonpSend = function(packet) |
| 63 | { |
| 64 | var deferred = dojo.io.script.get({ |
| 65 | url: packet.url, |
| 66 | sync: packet.sync === true, |
| 67 | callbackParamName: 'jsonp', |
| 68 | content: { |
| 69 | // In callback-polling, the content must be sent via the 'message' parameter |
| 70 | message: packet.body |
| 71 | }, |
| 72 | load: packet.onSuccess, |
| 73 | error: function(error) |
| 74 | { |
| 75 | packet.onError(error.message, deferred ? deferred.ioArgs.error : error); |
| 76 | } |
| 77 | }); |
| 78 | return undefined; |
| 79 | }; |
| 80 | |
| 81 | return that; |
| 82 | } |
| 83 | |
| 84 | // Registration order is important |
| 85 | if (org.cometd.WebSocket) |
| 86 | { |
| 87 | cometd.registerTransport('websocket', new org.cometd.WebSocketTransport()); |
| 88 | } |
| 89 | cometd.registerTransport('long-polling', new LongPollingTransport()); |
| 90 | cometd.registerTransport('callback-polling', new CallbackPollingTransport()); |
| 91 | |
| 92 | return cometd; |
| 93 | }; |
| 94 | |
| 95 | // The default cometd instance |
| 96 | dojox.cometd = new dojox.Cometd(); |
| 97 | |
| 98 | // Create a compatibility API for dojox.cometd instance with |
| 99 | // the original API. |
| 100 | |
| 101 | dojox.cometd._init = dojox.cometd.init; |
| 102 | |
| 103 | dojox.cometd._unsubscribe = dojox.cometd.unsubscribe; |
| 104 | |
| 105 | dojox.cometd.unsubscribe = function(channelOrToken, objOrFunc, funcName) |
| 106 | { |
| 107 | if (typeof channelOrToken === 'string') |
| 108 | { |
| 109 | throw "Deprecated function unsubscribe(string). Use unsubscribe(object) passing as argument the return value of subscribe()"; |
| 110 | } |
| 111 | |
| 112 | dojox.cometd._unsubscribe(channelOrToken); |
| 113 | }; |
| 114 | |
| 115 | dojox.cometd._metaHandshakeEvent = function(event) |
| 116 | { |
| 117 | event.action = "handshake"; |
| 118 | dojo.publish("/cometd/meta", [event]); |
| 119 | }; |
| 120 | |
| 121 | dojox.cometd._metaConnectEvent = function(event) |
| 122 | { |
| 123 | event.action = "connect"; |
| 124 | dojo.publish("/cometd/meta", [event]); |
| 125 | }; |
| 126 | |
| 127 | dojox.cometd.addListener('/meta/handshake', dojox.cometd, dojox.cometd._metaHandshakeEvent); |
| 128 | dojox.cometd.addListener('/meta/connect', dojox.cometd, dojox.cometd._metaConnectEvent); |