torquebox-stomp 2.3.2 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,244 +1,911 @@
1
- // (c) 2010 Jeff Mesnil -- http://jmesnil.net/
2
- // stilts-stomp-client.js 0.1.29
1
+ // Stilts stomp-client.js 0.1.38
2
+ // Some parts (c) 2010 Jeff Mesnil -- http://jmesnil.net/
3
+ // 0.1.38
3
4
 
4
- (function(window) {
5
+ var Stomp = {
5
6
 
6
- var Stomp = {};
7
+ Headers: {
8
+ HOST : 'host',
9
+ CONTENT_LENGTH : 'content-length',
10
+ CONTENT_TYPE : 'content-type',
11
+ ACCEPT_VERSION : 'accept-version',
12
+ VERSION : 'version'
13
+ },
14
+
15
+ Transport: {
16
+
17
+ },
18
+
19
+ Transports: [],
7
20
 
8
- Stomp.Headers = {
9
- HOST : 'host',
10
- CONTENT_LENGTH : 'content-length',
11
- CONTENT_TYPE : 'content-type',
12
- ACCEPT_VERSION : 'accept-version',
13
- VERSION : 'version'
14
- };
21
+ unmarshal: function(data) {
22
+ var divider = data.search(/\n\n/);
23
+ var headerLines = data.substring(0, divider).split('\n');
24
+ var command = headerLines.shift(), headers = {}, body = '';
25
+
26
+ // Parse headers
27
+ var line = idx = null;
28
+ for ( var i = 0; i < headerLines.length; i++) {
29
+ line = '' + headerLines[i];
30
+ idx = line.indexOf(':');
31
+ headers[line.substring(0, idx).trim()] = line.substring(idx + 1).trim();
32
+ }
33
+ try {
34
+ if (headers[Stomp.Headers.CONTENT_LENGTH]) {
35
+ var len = parseInt( headers[Stomp.Headers.CONTENT_LENGTH] );
36
+ var start = divider + 2;
37
+ body = (''+ data).substring(start, start+len);
38
+ } else {
39
+ // Parse body, stopping at the first \0 found.
40
+ var chr = null;
41
+ for ( var i = divider + 2; i < data.length; i++) {
42
+ chr = data.charAt(i);
43
+ if (chr === '\0') {
44
+ break;
45
+ }
46
+ body += chr;
47
+ }
48
+ }
49
+ return Stomp.frame(command, headers, body);
50
+ } catch (err) {
51
+ return Stomp.frame('ERROR', headers, "Error parsing frame: " + err.description);
52
+ }
53
+ },
15
54
 
16
- Stomp.Versions = {
17
- VERSION_1_0 : "1.0",
18
- VERSION_1_1 : "1.1",
19
-
20
- supportedVersions : function() {
21
- return "1.0,1.1";
22
- }
23
- };
24
-
25
- Stomp.frame = function(command, headers, body) {
26
- return {
27
- command : command,
28
- headers : headers,
29
- body : body,
30
- toString : function() {
31
- var out = command + '\n';
32
- if (headers) {
33
- for (header in headers) {
34
- if (headers.hasOwnProperty(header)) {
35
- out = out + header + ':' + headers[header] + '\n';
36
- }
37
- }
38
- }
39
- if (body) {
40
- out = out + 'content-length:' + body.length + '\n';
41
- }
42
- out = out + '\n';
43
- if (body) {
44
- out = out + body;
45
- }
46
- return out;
47
- }
48
- }
49
- };
50
-
51
- trim = function(str) {
52
- return ("" + str).replace(/^\s+/g, '').replace(/\s+$/g, '');
53
- };
54
-
55
- Stomp.unmarshal = function(data) {
56
- debug( "unmarshal: " + data );
57
- var divider = data.search(/\n\n/);
58
- var headerLines = data.substring(0, divider).split('\n');
59
- var command = headerLines.shift(), headers = {}, body = '';
60
-
61
- debug( headerLines );
62
-
63
- // Parse headers
64
- var line = idx = null;
65
- for ( var i = 0; i < headerLines.length; i++) {
66
- line = '' + headerLines[i];
67
- idx = line.indexOf(':');
68
- headers[trim(line.substring(0, idx))] = trim(line
69
- .substring(idx + 1));
70
- }
71
- try {
72
- if (headers[Stomp.Headers.CONTENT_LENGTH]) {
73
- var len = parseInt( headers[Stomp.Headers.CONTENT_LENGTH] );
74
- var start = divider + 2;
75
- body = (''+ data).substring(start, start+len);
76
- } else {
77
- // Parse body, stopping at the first \0 found.
78
- var chr = null;
79
- for ( var i = divider + 2; i < data.length; i++) {
80
- chr = data.charAt(i);
81
- if (chr === '\0') {
82
- break;
83
- }
84
- body += chr;
85
- }
86
- }
87
- return Stomp.frame(command, headers, body);
88
- } catch (err) {
89
- debug( err );
90
- return Stomp.frame('ERROR', headers, "Error parsing frame: "
91
- + err.description);
92
- }
93
- };
94
-
95
- Stomp.marshal = function(command, headers, body) {
96
- return Stomp.frame(command, headers, body).toString() + '\0';
97
- };
98
-
99
- Stomp.client = function(url) {
100
-
101
- var that, ws, login, passcode;
102
- var counter = 0; // used to index subscribers
103
- // subscription callbacks indexed by subscriber's ID
104
- var subscriptions = {};
105
-
106
- debug = function(str) {
107
- if (that.debug) {
108
- that.debug(str);
109
- }
110
- };
111
-
112
- onmessage = function(evt) {
113
- debug('<<< ' + evt.data);
114
- var frame = Stomp.unmarshal(evt.data);
115
- if (frame.command == "CONNECTED") {
116
- var version = frame.headers[Stomp.Headers.VERSION];
117
- that.version = version;
118
- if (that.connectCallback) {
119
- that.connectCallback(frame);
120
- }
121
- } else if (frame.command == "MESSAGE") {
122
- var subscription_id = '' + frame.headers.subscription;
123
- var onreceive = subscriptions['' + frame.headers.subscription];
124
- if (onreceive) {
125
- onreceive(frame);
126
- }
127
- } else if (frame.command == "RECEIPT" && that.onreceipt) {
128
- that.onreceipt(frame);
129
- } else if (frame.command == "ERROR" && that.onerror) {
130
- that.onerror(frame);
131
- }
132
- };
133
-
134
- transmit = function(command, headers, body) {
135
- var out = Stomp.marshal(command, headers, body);
136
- debug(">>> " + out);
137
- ws.send(out);
138
- };
139
-
140
- that = {};
141
-
142
- that.connect = function(login_, passcode_, connectCallback, errorCallback) {
143
- debug("Opening Web Socket...");
144
- var wsClass = null;
145
- if ( typeof WebSocket != 'undefined' ) {
146
- wsClass = WebSocket;
147
- } else if ( typeof MozWebSocket != 'undefined' ) {
148
- wsClass = MozWebSocket;
149
- }
150
-
151
- ws = new wsClass(url);
152
- ws.onmessage = onmessage;
153
- ws.onclose = function() {
154
- var msg = "Whoops! Lost connection to " + url;
155
- debug(msg);
156
- if (errorCallback) {
157
- errorCallback(msg);
158
- }
159
- };
160
- ws.onopen = function() {
161
- debug('Web Socket Opened...');
162
- var headers = {
163
- login : login,
164
- passcode : passcode
165
- };
166
- headers[Stomp.Headers.ACCEPT_VERSION] = Stomp.Versions.supportedVersions();
167
- transmit("CONNECT", headers);
168
- };
169
- login = login_;
170
- passcode = passcode_;
171
- that.connectCallback = connectCallback;
172
- };
173
-
174
- that.disconnect = function(disconnectCallback) {
175
- transmit("DISCONNECT");
176
- ws.close();
177
- if (disconnectCallback) {
178
- disconnectCallback();
179
- }
180
- };
181
-
182
- that.waitForDisconnect = function() {
183
- ws.waitForClosedState();
184
- };
185
-
186
- that.send = function(destination, headers, body) {
187
- var headers = headers || {};
188
- headers.destination = destination;
189
- transmit("SEND", headers, body);
190
- };
191
-
192
- that.subscribe = function(destination, callback, headers) {
193
- var headers = headers || {};
194
- var subscription_id = "sub-" + counter++;
195
- headers.destination = destination;
196
- headers.id = subscription_id;
197
- debug( "SUBSCRIBE---> " + subscription_id + " == " + callback );
198
- subscriptions['' + subscription_id] = callback;
199
- debug( subscriptions['' + subscription_id] );
200
- transmit("SUBSCRIBE", headers);
201
- return subscription_id;
202
- };
203
-
204
- that.unsubscribe = function(id, headers) {
205
- var headers = headers || {};
206
- headers.id = id;
207
- delete subscriptions[id];
208
- transmit("UNSUBSCRIBE", headers);
209
- };
210
-
211
- that.begin = function(transaction, headers) {
212
- var headers = headers || {};
213
- headers.transaction = transaction;
214
- transmit("BEGIN", headers);
215
- };
216
-
217
- that.commit = function(transaction, headers) {
218
- var headers = headers || {};
219
- headers.transaction = transaction;
220
- transmit("COMMIT", headers);
221
- };
222
-
223
- that.abort = function(transaction, headers) {
224
- var headers = headers || {};
225
- headers.transaction = transaction;
226
- transmit("ABORT", headers);
227
- };
228
-
229
- that.ack = function(message_id, headers) {
230
- var headers = headers || {};
231
- headers["message-id"] = message_id;
232
- transmit("ACK", headers);
233
- };
234
-
235
- that.nack = function(message_id, headers) {
236
- // TODO: Add nack functionality.
237
- }
55
+ marshal: function(command, headers, body) {
56
+ var frame = Stomp.frame(command, headers, body);
57
+ return frame.toString() + '\0';
58
+ },
59
+
60
+ frame: function(command, headers, body) {
61
+ return {
62
+ command : command,
63
+ headers : headers,
64
+ body : body,
65
+ toString : function() {
66
+ var out = command + '\n';
67
+ if (headers) {
68
+ for (header in headers) {
69
+ if (headers.hasOwnProperty(header)) {
70
+ out = out + header + ':' + headers[header] + '\n';
71
+ }
72
+ }
73
+ }
74
+ if (body) {
75
+ out = out + 'content-length:' + body.length + '\n';
76
+ }
77
+ out = out + '\n';
78
+ if (body) {
79
+ out = out + body;
80
+ }
81
+ return out;
82
+ }
83
+ }
84
+ },
85
+
86
+ };
87
+
88
+
89
+ Stomp.Client = function(host, port, secure) {
90
+ this._host = host || Stomp.DEFAULT_HOST;
91
+ this._port = port || Stomp.DEFAULT_PORT || 8675;
92
+ this._secure = secure || Stomp.DEFAULT_SECURE_FLAG || false;
93
+ }
94
+
95
+ Stomp.Client.prototype = {
96
+
97
+ Versions: {
98
+ VERSION_1_0 : "1.0",
99
+ VERSION_1_1 : "1.1",
100
+
101
+ },
102
+
103
+ supportedVersions : function() {
104
+ return "1.0,1.1";
105
+ },
106
+
107
+ connect: function(callback) {
108
+ if ( arguments.length == 1 ) {
109
+ this._connectCallback = arguments[0];
110
+ }
111
+ if ( arguments.length == 2 ) {
112
+ this._connectCallback = arguments[0];
113
+ this._errorCallback = arguments[1];
114
+ }
115
+ if ( arguments.length == 3 ) {
116
+ this._login = arguments[0];
117
+ this._passcode = arguments[1];
118
+ this._connectCallback = arguments[2];
119
+ }
120
+ if ( arguments.length == 4 ) {
121
+ this._login = arguments[0];
122
+ this._passcode = arguments[1];
123
+ this._connectCallback = arguments[2];
124
+ this._errorCallback = arguments[3];
125
+ }
126
+
127
+ this._connectTransport(callback);
128
+
129
+ },
130
+
131
+ _connectTransport: function(callback) {
132
+ var transports = [];
133
+ for ( i = 0 ; i < Stomp.Transports.length ; ++i ) {
134
+ var t = new Stomp.Transports[i]( this._host, this._port, this._secure );
135
+ t.client = this;
136
+ transports.push( t );
137
+ }
138
+
139
+ this._buildConnector( transports, 0, callback )();
140
+ },
141
+
142
+
143
+ _buildConnector: function(transports, i, callback) {
144
+ var client = this;
145
+ if ( i+1 < transports.length ) {
146
+ return function() {
147
+ var fallback = client._buildConnector( transports, i+1, callback );
148
+ try {
149
+ transports[i].connect( function() {
150
+ client._transport = transports[i];
151
+ callback();
152
+ }, fallback );
153
+ } catch (err) {
154
+ fallback();
155
+ }
156
+ };
157
+ } else if ( i < transports.length ) {
158
+ return function() {
159
+ var fallback = client.connectionFailed.bind( this );
160
+ try {
161
+ transports[i].connect( function() {
162
+ client._transport = transports[i];
163
+ callback();
164
+ }, client.connectionFailed.bind( this ) );
165
+ } catch(err) {
166
+ fallback();
167
+ }
168
+ };
169
+ } else {
170
+ return function() {
171
+ client.connectionFailed(this);
172
+ };
173
+ }
174
+ },
175
+
176
+ connectionFailed: function() {
177
+ console.log( "unable to connect" );
178
+ },
179
+
180
+ disconnect: function(disconnectCallback) {
181
+ this._transmit("DISCONNECT");
182
+ this._transport.close();
183
+ if (disconnectCallback) {
184
+ disconnectCallback();
185
+ }
186
+ },
187
+
188
+ waitForDisconnect: function() {
189
+ this._transport.waitForClosedState();
190
+ },
191
+
192
+ send: function(destination, headers, body) {
193
+ var headers = headers || {};
194
+ headers.destination = destination;
195
+ this._transmit("SEND", headers, body);
196
+ },
197
+
198
+ subscribe: function(destination, callback, headers) {
199
+ var headers = headers || {};
200
+ var subscription_id = "sub-" + this._counter++;
201
+ headers.destination = destination;
202
+ headers.id = subscription_id;
203
+ this._subscriptions['' + subscription_id] = callback;
204
+ this._transmit("SUBSCRIBE", headers);
205
+ return subscription_id;
206
+ },
207
+
208
+ unsubscribe: function(id, headers) {
209
+ var headers = headers || {};
210
+ headers.id = id;
211
+ delete this._subscriptions[id];
212
+ this._transmit("UNSUBSCRIBE", headers);
213
+ },
214
+
215
+ begin: function(transaction, headers) {
216
+ var headers = headers || {};
217
+ headers.transaction = transaction;
218
+ this._transmit("BEGIN", headers);
219
+ },
220
+
221
+ commit: function(transaction, headers) {
222
+ var headers = headers || {};
223
+ headers.transaction = transaction;
224
+ this._transmit("COMMIT", headers);
225
+ },
226
+
227
+ abort: function(transaction, headers) {
228
+ var headers = headers || {};
229
+ headers.transaction = transaction;
230
+ this._transmit("ABORT", headers);
231
+ },
232
+
233
+ ack: function(message_id, headers) {
234
+ var headers = headers || {};
235
+ headers["message-id"] = message_id;
236
+ this._transmit("ACK", headers);
237
+ },
238
238
 
239
- return that;
240
- };
239
+ nack: function(message_id, headers) {
240
+ // TODO: Add nack functionality.
241
+ },
242
+
243
+ // ----------------------------------------
244
+ processMessage: function(message) {
245
+ if (message.command == "MESSAGE") {
246
+ var subId = message.headers['subscription'];
247
+ var callback = this._subscriptions[ subId ];
248
+ callback(message);
249
+ } else if (message.command == "RECEIPT" && this.onreceipt) {
250
+ this.onreceipt(message);
251
+ } else if (message.command == "ERROR" && this.onerror) {
252
+ this.onerror(message);
253
+ }
254
+ },
255
+ // ----------------------------------------
256
+
257
+ _login: undefined,
258
+ _passcode: undefined,
259
+ _connectCallback: undefined,
260
+ _errorCallback: undefined,
261
+ _webSocketEnabled: true,
262
+ _longPollEnabled: true,
263
+
264
+ _transport: undefined,
265
+ _subscriptions: {},
266
+ _counter: 0,
267
+
268
+ _transmit: function(command, headers, body) {
269
+ this._transport.transmit(command, headers, body);
270
+ },
271
+
272
+ }
273
+ Stomp.Transport.WebSocket = function(host, port, secure) {
274
+ this._host = host;
275
+ this._port = port;
276
+ this._secure = secure;
277
+ }
278
+
279
+ Stomp.Transport.WebSocket.prototype = {
280
+ _ws: undefined,
281
+ _state: 'unconnected',
282
+
283
+ transmit: function(command, headers, body) {
284
+ var out = Stomp.marshal(command, headers, body);
285
+ this._ws.send(out);
286
+ },
287
+
288
+ connect: function(callback, errorCallback) {
289
+ var wsClass = null;
290
+ if ( typeof WebSocket != 'undefined' ) {
291
+ wsClass = WebSocket;
292
+ } else if ( typeof MozWebSocket != 'undefined' ) {
293
+ wsClass = MozWebSocket;
294
+ } else {
295
+ return;
296
+ }
297
+
298
+ this._connectCallback = callback;
299
+ this._ws = new wsClass( this._url() );
300
+ this._ws.onopen = this._issueConnect.bind(this);
301
+ this._ws.onmessage = this._handleMessage.bind(this);
302
+ this._ws.onerror = errorCallback;
303
+ },
304
+
305
+ close: function() {
306
+ this._ws.close();
307
+ },
308
+
309
+ send: function(data) {
310
+ this._ws.send(data);
311
+ },
312
+
313
+ _issueConnect: function() {
314
+ var headers = {};
315
+ this._ws.onerror = this.client.onerror;
316
+ if ( this._login ) {
317
+ headers['login'] = this._login;
318
+ }
319
+ if ( this._passcode ) {
320
+ headers['passcode'] = this._passcode;
321
+ }
322
+ console.debug( this.client );
323
+ headers[Stomp.Headers.ACCEPT_VERSION] = this.client.supportedVersions();
324
+ this.transmit( "CONNECT", headers)
325
+ },
326
+
327
+ _handleMessage: function(evt) {
328
+ var frame = Stomp.unmarshal(evt.data);
329
+ if (frame.command == "CONNECTED") {
330
+ this._version = frame.headers[Stomp.Headers.VERSION];
331
+ if (this._connectCallback) {
332
+ this._connectCallback(frame);
333
+ }
334
+ } else {
335
+ this.client.processMessage( frame );
336
+ }
337
+ },
338
+
339
+ _url: function() {
340
+ if ( this._secure ) {
341
+ return "wss://" + this._host + ":" + this._port + "/";
342
+ }
343
+ return "ws://" + this._host + ":" + this._port + "/";
344
+ },
345
+ }
346
+
347
+
348
+ Stomp.Transports.push( Stomp.Transport.WebSocket );
349
+
350
+
351
+
352
+ // ---------------------------------------------------------------
353
+ // Third-party Libraries
354
+ // ---------------------------------------------------------------
355
+
356
+ /* SWFObject v2.2 <http://code.google.com/p/swfobject/>
357
+ is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
358
+ */
359
+ var swfobject=function(){var D="undefined",r="object",S="Shockwave Flash",W="ShockwaveFlash.ShockwaveFlash",q="application/x-shockwave-flash",R="SWFObjectExprInst",x="onreadystatechange",O=window,j=document,t=navigator,T=false,U=[h],o=[],N=[],I=[],l,Q,E,B,J=false,a=false,n,G,m=true,M=function(){var aa=typeof j.getElementById!=D&&typeof j.getElementsByTagName!=D&&typeof j.createElement!=D,ah=t.userAgent.toLowerCase(),Y=t.platform.toLowerCase(),ae=Y?/win/.test(Y):/win/.test(ah),ac=Y?/mac/.test(Y):/mac/.test(ah),af=/webkit/.test(ah)?parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,X=!+"\v1",ag=[0,0,0],ab=null;if(typeof t.plugins!=D&&typeof t.plugins[S]==r){ab=t.plugins[S].description;if(ab&&!(typeof t.mimeTypes!=D&&t.mimeTypes[q]&&!t.mimeTypes[q].enabledPlugin)){T=true;X=false;ab=ab.replace(/^.*\s+(\S+\s+\S+$)/,"$1");ag[0]=parseInt(ab.replace(/^(.*)\..*$/,"$1"),10);ag[1]=parseInt(ab.replace(/^.*\.(.*)\s.*$/,"$1"),10);ag[2]=/[a-zA-Z]/.test(ab)?parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/,"$1"),10):0}}else{if(typeof O.ActiveXObject!=D){try{var ad=new ActiveXObject(W);if(ad){ab=ad.GetVariable("$version");if(ab){X=true;ab=ab.split(" ")[1].split(",");ag=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}}catch(Z){}}}return{w3:aa,pv:ag,wk:af,ie:X,win:ae,mac:ac}}(),k=function(){if(!M.w3){return}if((typeof j.readyState!=D&&j.readyState=="complete")||(typeof j.readyState==D&&(j.getElementsByTagName("body")[0]||j.body))){f()}if(!J){if(typeof j.addEventListener!=D){j.addEventListener("DOMContentLoaded",f,false)}if(M.ie&&M.win){j.attachEvent(x,function(){if(j.readyState=="complete"){j.detachEvent(x,arguments.callee);f()}});if(O==top){(function(){if(J){return}try{j.documentElement.doScroll("left")}catch(X){setTimeout(arguments.callee,0);return}f()})()}}if(M.wk){(function(){if(J){return}if(!/loaded|complete/.test(j.readyState)){setTimeout(arguments.callee,0);return}f()})()}s(f)}}();function f(){if(J){return}try{var Z=j.getElementsByTagName("body")[0].appendChild(C("span"));Z.parentNode.removeChild(Z)}catch(aa){return}J=true;var X=U.length;for(var Y=0;Y<X;Y++){U[Y]()}}function K(X){if(J){X()}else{U[U.length]=X}}function s(Y){if(typeof O.addEventListener!=D){O.addEventListener("load",Y,false)}else{if(typeof j.addEventListener!=D){j.addEventListener("load",Y,false)}else{if(typeof O.attachEvent!=D){i(O,"onload",Y)}else{if(typeof O.onload=="function"){var X=O.onload;O.onload=function(){X();Y()}}else{O.onload=Y}}}}}function h(){if(T){V()}else{H()}}function V(){var X=j.getElementsByTagName("body")[0];var aa=C(r);aa.setAttribute("type",q);var Z=X.appendChild(aa);if(Z){var Y=0;(function(){if(typeof Z.GetVariable!=D){var ab=Z.GetVariable("$version");if(ab){ab=ab.split(" ")[1].split(",");M.pv=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}else{if(Y<10){Y++;setTimeout(arguments.callee,10);return}}X.removeChild(aa);Z=null;H()})()}else{H()}}function H(){var ag=o.length;if(ag>0){for(var af=0;af<ag;af++){var Y=o[af].id;var ab=o[af].callbackFn;var aa={success:false,id:Y};if(M.pv[0]>0){var ae=c(Y);if(ae){if(F(o[af].swfVersion)&&!(M.wk&&M.wk<312)){w(Y,true);if(ab){aa.success=true;aa.ref=z(Y);ab(aa)}}else{if(o[af].expressInstall&&A()){var ai={};ai.data=o[af].expressInstall;ai.width=ae.getAttribute("width")||"0";ai.height=ae.getAttribute("height")||"0";if(ae.getAttribute("class")){ai.styleclass=ae.getAttribute("class")}if(ae.getAttribute("align")){ai.align=ae.getAttribute("align")}var ah={};var X=ae.getElementsByTagName("param");var ac=X.length;for(var ad=0;ad<ac;ad++){if(X[ad].getAttribute("name").toLowerCase()!="movie"){ah[X[ad].getAttribute("name")]=X[ad].getAttribute("value")}}P(ai,ah,Y,ab)}else{p(ae);if(ab){ab(aa)}}}}}else{w(Y,true);if(ab){var Z=z(Y);if(Z&&typeof Z.SetVariable!=D){aa.success=true;aa.ref=Z}ab(aa)}}}}}function z(aa){var X=null;var Y=c(aa);if(Y&&Y.nodeName=="OBJECT"){if(typeof Y.SetVariable!=D){X=Y}else{var Z=Y.getElementsByTagName(r)[0];if(Z){X=Z}}}return X}function A(){return !a&&F("6.0.65")&&(M.win||M.mac)&&!(M.wk&&M.wk<312)}function P(aa,ab,X,Z){a=true;E=Z||null;B={success:false,id:X};var ae=c(X);if(ae){if(ae.nodeName=="OBJECT"){l=g(ae);Q=null}else{l=ae;Q=X}aa.id=R;if(typeof aa.width==D||(!/%$/.test(aa.width)&&parseInt(aa.width,10)<310)){aa.width="310"}if(typeof aa.height==D||(!/%$/.test(aa.height)&&parseInt(aa.height,10)<137)){aa.height="137"}j.title=j.title.slice(0,47)+" - Flash Player Installation";var ad=M.ie&&M.win?"ActiveX":"PlugIn",ac="MMredirectURL="+O.location.toString().replace(/&/g,"%26")+"&MMplayerType="+ad+"&MMdoctitle="+j.title;if(typeof ab.flashvars!=D){ab.flashvars+="&"+ac}else{ab.flashvars=ac}if(M.ie&&M.win&&ae.readyState!=4){var Y=C("div");X+="SWFObjectNew";Y.setAttribute("id",X);ae.parentNode.insertBefore(Y,ae);ae.style.display="none";(function(){if(ae.readyState==4){ae.parentNode.removeChild(ae)}else{setTimeout(arguments.callee,10)}})()}u(aa,ab,X)}}function p(Y){if(M.ie&&M.win&&Y.readyState!=4){var X=C("div");Y.parentNode.insertBefore(X,Y);X.parentNode.replaceChild(g(Y),X);Y.style.display="none";(function(){if(Y.readyState==4){Y.parentNode.removeChild(Y)}else{setTimeout(arguments.callee,10)}})()}else{Y.parentNode.replaceChild(g(Y),Y)}}function g(ab){var aa=C("div");if(M.win&&M.ie){aa.innerHTML=ab.innerHTML}else{var Y=ab.getElementsByTagName(r)[0];if(Y){var ad=Y.childNodes;if(ad){var X=ad.length;for(var Z=0;Z<X;Z++){if(!(ad[Z].nodeType==1&&ad[Z].nodeName=="PARAM")&&!(ad[Z].nodeType==8)){aa.appendChild(ad[Z].cloneNode(true))}}}}}return aa}function u(ai,ag,Y){var X,aa=c(Y);if(M.wk&&M.wk<312){return X}if(aa){if(typeof ai.id==D){ai.id=Y}if(M.ie&&M.win){var ah="";for(var ae in ai){if(ai[ae]!=Object.prototype[ae]){if(ae.toLowerCase()=="data"){ag.movie=ai[ae]}else{if(ae.toLowerCase()=="styleclass"){ah+=' class="'+ai[ae]+'"'}else{if(ae.toLowerCase()!="classid"){ah+=" "+ae+'="'+ai[ae]+'"'}}}}}var af="";for(var ad in ag){if(ag[ad]!=Object.prototype[ad]){af+='<param name="'+ad+'" value="'+ag[ad]+'" />'}}aa.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+ah+">"+af+"</object>";N[N.length]=ai.id;X=c(ai.id)}else{var Z=C(r);Z.setAttribute("type",q);for(var ac in ai){if(ai[ac]!=Object.prototype[ac]){if(ac.toLowerCase()=="styleclass"){Z.setAttribute("class",ai[ac])}else{if(ac.toLowerCase()!="classid"){Z.setAttribute(ac,ai[ac])}}}}for(var ab in ag){if(ag[ab]!=Object.prototype[ab]&&ab.toLowerCase()!="movie"){e(Z,ab,ag[ab])}}aa.parentNode.replaceChild(Z,aa);X=Z}}return X}function e(Z,X,Y){var aa=C("param");aa.setAttribute("name",X);aa.setAttribute("value",Y);Z.appendChild(aa)}function y(Y){var X=c(Y);if(X&&X.nodeName=="OBJECT"){if(M.ie&&M.win){X.style.display="none";(function(){if(X.readyState==4){b(Y)}else{setTimeout(arguments.callee,10)}})()}else{X.parentNode.removeChild(X)}}}function b(Z){var Y=c(Z);if(Y){for(var X in Y){if(typeof Y[X]=="function"){Y[X]=null}}Y.parentNode.removeChild(Y)}}function c(Z){var X=null;try{X=j.getElementById(Z)}catch(Y){}return X}function C(X){return j.createElement(X)}function i(Z,X,Y){Z.attachEvent(X,Y);I[I.length]=[Z,X,Y]}function F(Z){var Y=M.pv,X=Z.split(".");X[0]=parseInt(X[0],10);X[1]=parseInt(X[1],10)||0;X[2]=parseInt(X[2],10)||0;return(Y[0]>X[0]||(Y[0]==X[0]&&Y[1]>X[1])||(Y[0]==X[0]&&Y[1]==X[1]&&Y[2]>=X[2]))?true:false}function v(ac,Y,ad,ab){if(M.ie&&M.mac){return}var aa=j.getElementsByTagName("head")[0];if(!aa){return}var X=(ad&&typeof ad=="string")?ad:"screen";if(ab){n=null;G=null}if(!n||G!=X){var Z=C("style");Z.setAttribute("type","text/css");Z.setAttribute("media",X);n=aa.appendChild(Z);if(M.ie&&M.win&&typeof j.styleSheets!=D&&j.styleSheets.length>0){n=j.styleSheets[j.styleSheets.length-1]}G=X}if(M.ie&&M.win){if(n&&typeof n.addRule==r){n.addRule(ac,Y)}}else{if(n&&typeof j.createTextNode!=D){n.appendChild(j.createTextNode(ac+" {"+Y+"}"))}}}function w(Z,X){if(!m){return}var Y=X?"visible":"hidden";if(J&&c(Z)){c(Z).style.visibility=Y}else{v("#"+Z,"visibility:"+Y)}}function L(Y){var Z=/[\\\"<>\.;]/;var X=Z.exec(Y)!=null;return X&&typeof encodeURIComponent!=D?encodeURIComponent(Y):Y}var d=function(){if(M.ie&&M.win){window.attachEvent("onunload",function(){var ac=I.length;for(var ab=0;ab<ac;ab++){I[ab][0].detachEvent(I[ab][1],I[ab][2])}var Z=N.length;for(var aa=0;aa<Z;aa++){y(N[aa])}for(var Y in M){M[Y]=null}M=null;for(var X in swfobject){swfobject[X]=null}swfobject=null})}}();return{registerObject:function(ab,X,aa,Z){if(M.w3&&ab&&X){var Y={};Y.id=ab;Y.swfVersion=X;Y.expressInstall=aa;Y.callbackFn=Z;o[o.length]=Y;w(ab,false)}else{if(Z){Z({success:false,id:ab})}}},getObjectById:function(X){if(M.w3){return z(X)}},embedSWF:function(ab,ah,ae,ag,Y,aa,Z,ad,af,ac){var X={success:false,id:ah};if(M.w3&&!(M.wk&&M.wk<312)&&ab&&ah&&ae&&ag&&Y){w(ah,false);K(function(){ae+="";ag+="";var aj={};if(af&&typeof af===r){for(var al in af){aj[al]=af[al]}}aj.data=ab;aj.width=ae;aj.height=ag;var am={};if(ad&&typeof ad===r){for(var ak in ad){am[ak]=ad[ak]}}if(Z&&typeof Z===r){for(var ai in Z){if(typeof am.flashvars!=D){am.flashvars+="&"+ai+"="+Z[ai]}else{am.flashvars=ai+"="+Z[ai]}}}if(F(Y)){var an=u(aj,am,ah);if(aj.id==ah){w(ah,true)}X.success=true;X.ref=an}else{if(aa&&A()){aj.data=aa;P(aj,am,ah,ac);return}else{w(ah,true)}}if(ac){ac(X)}})}else{if(ac){ac(X)}}},switchOffAutoHideShow:function(){m=false},ua:M,getFlashPlayerVersion:function(){return{major:M.pv[0],minor:M.pv[1],release:M.pv[2]}},hasFlashPlayerVersion:F,createSWF:function(Z,Y,X){if(M.w3){return u(Z,Y,X)}else{return undefined}},showExpressInstall:function(Z,aa,X,Y){if(M.w3&&A()){P(Z,aa,X,Y)}},removeSWF:function(X){if(M.w3){y(X)}},createCSS:function(aa,Z,Y,X){if(M.w3){v(aa,Z,Y,X)}},addDomLoadEvent:K,addLoadEvent:s,getQueryParamValue:function(aa){var Z=j.location.search||j.location.hash;if(Z){if(/\?/.test(Z)){Z=Z.split("?")[1]}if(aa==null){return L(Z)}var Y=Z.split("&");for(var X=0;X<Y.length;X++){if(Y[X].substring(0,Y[X].indexOf("="))==aa){return L(Y[X].substring((Y[X].indexOf("=")+1)))}}}return""},expressInstallCallback:function(){if(a){var X=c(R);if(X&&l){X.parentNode.replaceChild(l,X);if(Q){w(Q,true);if(M.ie&&M.win){l.style.display="block"}}if(E){E(B)}}a=false}}}}();
360
+
361
+ // Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
362
+ // License: New BSD License
363
+ // Reference: http://dev.w3.org/html5/websockets/
364
+ // Reference: http://tools.ietf.org/html/rfc6455
365
+
366
+ WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
367
+
368
+ (function() {
369
+
370
+ if (window.WEB_SOCKET_FORCE_FLASH) {
371
+ // Keeps going.
372
+ } else if (window.WebSocket) {
373
+ return;
374
+ } else if (window.MozWebSocket) {
375
+ // Firefox.
376
+ window.WebSocket = MozWebSocket;
377
+ return;
378
+ }
379
+
380
+ var logger;
381
+ if (window.WEB_SOCKET_LOGGER) {
382
+ logger = WEB_SOCKET_LOGGER;
383
+ } else if (window.console && window.console.log && window.console.error) {
384
+ // In some environment, console is defined but console.log or console.error is missing.
385
+ logger = window.console;
386
+ } else {
387
+ logger = {log: function(){ }, error: function(){ }};
388
+ }
389
+
390
+ // swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
391
+ if (swfobject.getFlashPlayerVersion().major < 10) {
392
+ logger.error("Flash Player >= 10.0.0 is required.");
393
+ return;
394
+ }
395
+ if (location.protocol == "file:") {
396
+ logger.error(
397
+ "WARNING: web-socket-js doesn't work in file:///... URL " +
398
+ "unless you set Flash Security Settings properly. " +
399
+ "Open the page via Web server i.e. http://...");
400
+ }
401
+
402
+ /**
403
+ * Our own implementation of WebSocket class using Flash.
404
+ * @param {string} url
405
+ * @param {array or string} protocols
406
+ * @param {string} proxyHost
407
+ * @param {int} proxyPort
408
+ * @param {string} headers
409
+ */
410
+ window.WebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
411
+ var self = this;
412
+ self.__id = WebSocket.__nextId++;
413
+ WebSocket.__instances[self.__id] = self;
414
+ self.readyState = WebSocket.CONNECTING;
415
+ self.bufferedAmount = 0;
416
+ self.__events = {};
417
+ if (!protocols) {
418
+ protocols = [];
419
+ } else if (typeof protocols == "string") {
420
+ protocols = [protocols];
421
+ }
422
+ // Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc.
423
+ // Otherwise, when onopen fires immediately, onopen is called before it is set.
424
+ self.__createTask = setTimeout(function() {
425
+ WebSocket.__addTask(function() {
426
+ self.__createTask = null;
427
+ WebSocket.__flash.create(
428
+ self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null);
429
+ });
430
+ }, 0);
431
+ };
432
+
433
+ /**
434
+ * Send data to the web socket.
435
+ * @param {string} data The data to send to the socket.
436
+ * @return {boolean} True for success, false for failure.
437
+ */
438
+ WebSocket.prototype.send = function(data) {
439
+ if (this.readyState == WebSocket.CONNECTING) {
440
+ throw "INVALID_STATE_ERR: Web Socket connection has not been established";
441
+ }
442
+ // We use encodeURIComponent() here, because FABridge doesn't work if
443
+ // the argument includes some characters. We don't use escape() here
444
+ // because of this:
445
+ // https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions
446
+ // But it looks decodeURIComponent(encodeURIComponent(s)) doesn't
447
+ // preserve all Unicode characters either e.g. "\uffff" in Firefox.
448
+ // Note by wtritch: Hopefully this will not be necessary using ExternalInterface. Will require
449
+ // additional testing.
450
+ var result = WebSocket.__flash.send(this.__id, encodeURIComponent(data));
451
+ if (result < 0) { // success
452
+ return true;
453
+ } else {
454
+ this.bufferedAmount += result;
455
+ return false;
456
+ }
457
+ };
458
+
459
+ /**
460
+ * Close this web socket gracefully.
461
+ */
462
+ WebSocket.prototype.close = function() {
463
+ if (this.__createTask) {
464
+ clearTimeout(this.__createTask);
465
+ this.__createTask = null;
466
+ this.readyState = WebSocket.CLOSED;
467
+ return;
468
+ }
469
+ if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
470
+ return;
471
+ }
472
+ this.readyState = WebSocket.CLOSING;
473
+ WebSocket.__flash.close(this.__id);
474
+ };
475
+
476
+ /**
477
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
478
+ *
479
+ * @param {string} type
480
+ * @param {function} listener
481
+ * @param {boolean} useCapture
482
+ * @return void
483
+ */
484
+ WebSocket.prototype.addEventListener = function(type, listener, useCapture) {
485
+ if (!(type in this.__events)) {
486
+ this.__events[type] = [];
487
+ }
488
+ this.__events[type].push(listener);
489
+ };
490
+
491
+ /**
492
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
493
+ *
494
+ * @param {string} type
495
+ * @param {function} listener
496
+ * @param {boolean} useCapture
497
+ * @return void
498
+ */
499
+ WebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
500
+ if (!(type in this.__events)) return;
501
+ var events = this.__events[type];
502
+ for (var i = events.length - 1; i >= 0; --i) {
503
+ if (events[i] === listener) {
504
+ events.splice(i, 1);
505
+ break;
506
+ }
507
+ }
508
+ };
509
+
510
+ /**
511
+ * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
512
+ *
513
+ * @param {Event} event
514
+ * @return void
515
+ */
516
+ WebSocket.prototype.dispatchEvent = function(event) {
517
+ var events = this.__events[event.type] || [];
518
+ for (var i = 0; i < events.length; ++i) {
519
+ events[i](event);
520
+ }
521
+ var handler = this["on" + event.type];
522
+ if (handler) handler.apply(this, [event]);
523
+ };
524
+
525
+ /**
526
+ * Handles an event from Flash.
527
+ * @param {Object} flashEvent
528
+ */
529
+ WebSocket.prototype.__handleEvent = function(flashEvent) {
530
+
531
+ if ("readyState" in flashEvent) {
532
+ this.readyState = flashEvent.readyState;
533
+ }
534
+ if ("protocol" in flashEvent) {
535
+ this.protocol = flashEvent.protocol;
536
+ }
537
+
538
+ var jsEvent;
539
+ if (flashEvent.type == "open" || flashEvent.type == "error") {
540
+ jsEvent = this.__createSimpleEvent(flashEvent.type);
541
+ } else if (flashEvent.type == "close") {
542
+ jsEvent = this.__createSimpleEvent("close");
543
+ jsEvent.wasClean = flashEvent.wasClean ? true : false;
544
+ jsEvent.code = flashEvent.code;
545
+ jsEvent.reason = flashEvent.reason;
546
+ } else if (flashEvent.type == "message") {
547
+ var data = decodeURIComponent(flashEvent.message);
548
+ jsEvent = this.__createMessageEvent("message", data);
549
+ } else {
550
+ throw "unknown event type: " + flashEvent.type;
551
+ }
552
+
553
+ this.dispatchEvent(jsEvent);
554
+
555
+ };
556
+
557
+ WebSocket.prototype.__createSimpleEvent = function(type) {
558
+ if (document.createEvent && window.Event) {
559
+ var event = document.createEvent("Event");
560
+ event.initEvent(type, false, false);
561
+ return event;
562
+ } else {
563
+ return {type: type, bubbles: false, cancelable: false};
564
+ }
565
+ };
566
+
567
+ WebSocket.prototype.__createMessageEvent = function(type, data) {
568
+ if (document.createEvent && window.MessageEvent && !window.opera) {
569
+ var event = document.createEvent("MessageEvent");
570
+ event.initMessageEvent("message", false, false, data, null, null, window, null);
571
+ return event;
572
+ } else {
573
+ // IE and Opera, the latter one truncates the data parameter after any 0x00 bytes.
574
+ return {type: type, data: data, bubbles: false, cancelable: false};
575
+ }
576
+ };
577
+
578
+ /**
579
+ * Define the WebSocket readyState enumeration.
580
+ */
581
+ WebSocket.CONNECTING = 0;
582
+ WebSocket.OPEN = 1;
583
+ WebSocket.CLOSING = 2;
584
+ WebSocket.CLOSED = 3;
585
+
586
+ // Field to check implementation of WebSocket.
587
+ WebSocket.__isFlashImplementation = true;
588
+ WebSocket.__initialized = false;
589
+ WebSocket.__flash = null;
590
+ WebSocket.__instances = {};
591
+ WebSocket.__tasks = [];
592
+ WebSocket.__nextId = 0;
593
+
594
+ /**
595
+ * Load a new flash security policy file.
596
+ * @param {string} url
597
+ */
598
+ WebSocket.loadFlashPolicyFile = function(url){
599
+ WebSocket.__addTask(function() {
600
+ WebSocket.__flash.loadManualPolicyFile(url);
601
+ });
602
+ };
603
+
604
+ /**
605
+ * Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
606
+ */
607
+ WebSocket.__initialize = function() {
608
+
609
+ if (WebSocket.__initialized) return;
610
+ WebSocket.__initialized = true;
611
+
612
+ if (WebSocket.__swfLocation) {
613
+ // For backword compatibility.
614
+ window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
615
+ }
616
+ if (!window.WEB_SOCKET_SWF_LOCATION) {
617
+ logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
618
+ return;
619
+ }
620
+ if (!window.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR &&
621
+ !WEB_SOCKET_SWF_LOCATION.match(/(^|\/)WebSocketMainInsecure\.swf(\?.*)?$/) &&
622
+ WEB_SOCKET_SWF_LOCATION.match(/^\w+:\/\/([^\/]+)/)) {
623
+ var swfHost = RegExp.$1;
624
+ if (location.host != swfHost) {
625
+ logger.error(
626
+ "[WebSocket] You must host HTML and WebSocketMain.swf in the same host " +
627
+ "('" + location.host + "' != '" + swfHost + "'). " +
628
+ "See also 'How to host HTML file and SWF file in different domains' section " +
629
+ "in README.md. If you use WebSocketMainInsecure.swf, you can suppress this message " +
630
+ "by WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;");
631
+ }
632
+ }
633
+ var container = document.createElement("div");
634
+ container.id = "webSocketContainer";
635
+ // Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
636
+ // Flash from loading at least in IE. So we move it out of the screen at (-100, -100).
637
+ // But this even doesn't work with Flash Lite (e.g. in Droid Incredible). So with Flash
638
+ // Lite, we put it at (0, 0). This shows 1x1 box visible at left-top corner but this is
639
+ // the best we can do as far as we know now.
640
+ container.style.position = "absolute";
641
+ if (WebSocket.__isFlashLite()) {
642
+ container.style.left = "0px";
643
+ container.style.top = "0px";
644
+ } else {
645
+ container.style.left = "-100px";
646
+ container.style.top = "-100px";
647
+ }
648
+ var holder = document.createElement("div");
649
+ holder.id = "webSocketFlash";
650
+ container.appendChild(holder);
651
+ document.body.appendChild(container);
652
+ // See this article for hasPriority:
653
+ // http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-7ffd.html
654
+ swfobject.embedSWF(
655
+ WEB_SOCKET_SWF_LOCATION,
656
+ "webSocketFlash",
657
+ "1" /* width */,
658
+ "1" /* height */,
659
+ "10.0.0" /* SWF version */,
660
+ null,
661
+ null,
662
+ {hasPriority: true, swliveconnect : true, allowScriptAccess: "always"},
663
+ null,
664
+ function(e) {
665
+ if (!e.success) {
666
+ logger.error("[WebSocket] swfobject.embedSWF failed");
667
+ }
668
+ }
669
+ );
670
+
671
+ };
672
+
673
+ /**
674
+ * Called by Flash to notify JS that it's fully loaded and ready
675
+ * for communication.
676
+ */
677
+ WebSocket.__onFlashInitialized = function() {
678
+ // We need to set a timeout here to avoid round-trip calls
679
+ // to flash during the initialization process.
680
+ setTimeout(function() {
681
+ WebSocket.__flash = document.getElementById("webSocketFlash");
682
+ WebSocket.__flash.setCallerUrl(location.href);
683
+ WebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
684
+ for (var i = 0; i < WebSocket.__tasks.length; ++i) {
685
+ WebSocket.__tasks[i]();
686
+ }
687
+ WebSocket.__tasks = [];
688
+ }, 0);
689
+ };
690
+
691
+ /**
692
+ * Called by Flash to notify WebSockets events are fired.
693
+ */
694
+ WebSocket.__onFlashEvent = function() {
695
+ setTimeout(function() {
696
+ try {
697
+ // Gets events using receiveEvents() instead of getting it from event object
698
+ // of Flash event. This is to make sure to keep message order.
699
+ // It seems sometimes Flash events don't arrive in the same order as they are sent.
700
+ var events = WebSocket.__flash.receiveEvents();
701
+ for (var i = 0; i < events.length; ++i) {
702
+ WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
703
+ }
704
+ } catch (e) {
705
+ logger.error(e);
706
+ }
707
+ }, 0);
708
+ return true;
709
+ };
710
+
711
+ // Called by Flash.
712
+ WebSocket.__log = function(message) {
713
+ logger.log(decodeURIComponent(message));
714
+ };
715
+
716
+ // Called by Flash.
717
+ WebSocket.__error = function(message) {
718
+ logger.error(decodeURIComponent(message));
719
+ };
720
+
721
+ WebSocket.__addTask = function(task) {
722
+ if (WebSocket.__flash) {
723
+ task();
724
+ } else {
725
+ WebSocket.__tasks.push(task);
726
+ }
727
+ };
728
+
729
+ /**
730
+ * Test if the browser is running flash lite.
731
+ * @return {boolean} True if flash lite is running, false otherwise.
732
+ */
733
+ WebSocket.__isFlashLite = function() {
734
+ if (!window.navigator || !window.navigator.mimeTypes) {
735
+ return false;
736
+ }
737
+ var mimeType = window.navigator.mimeTypes["application/x-shockwave-flash"];
738
+ if (!mimeType || !mimeType.enabledPlugin || !mimeType.enabledPlugin.filename) {
739
+ return false;
740
+ }
741
+ return mimeType.enabledPlugin.filename.match(/flashlite/i) ? true : false;
742
+ };
743
+
744
+ if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
745
+ // NOTE:
746
+ // This fires immediately if web_socket.js is dynamically loaded after
747
+ // the document is loaded.
748
+ swfobject.addDomLoadEvent(function() {
749
+ WebSocket.__initialize();
750
+ });
751
+ }
752
+
753
+ })();
754
+
755
+ // ----------------------------------------
756
+ // HTTP Transport
757
+ // ----------------------------------------
758
+
759
+ Stomp.Transport.HTTP = function(host, port, secure) {
760
+ this._host = host;
761
+ this._port = port;
762
+ this._secure = secure;
763
+ }
764
+
765
+ Stomp.Transport.HTTP.prototype = {
766
+
767
+ _receiverRequest: undefined,
768
+ _disconnectReceiver: false,
769
+
770
+ connect: function(callback, errorCallback) {
771
+ var headers = {};
772
+ if ( this._login ) {
773
+ headers.login = this._login;
774
+ }
775
+ if ( this._passcode ) {
776
+ headers.passcode = this._passcode;
777
+ }
778
+
779
+ headers[Stomp.Headers.ACCEPT_VERSION] = this.client.supportedVersions();
780
+
781
+ var transport = this;
782
+
783
+ var request = new XMLHttpRequest();
784
+ request.open( "POST", this._url(), true );
785
+ request.withCredentials = true;
786
+
787
+ var timeoutHandle = setTimeout( function() {
788
+ if ( request.readyState != 0 && request.readyState != 4 ) {
789
+ request.abort();
790
+ errorCallack();
791
+ }
792
+ }, 5000 );
793
+
794
+ request.onerror = errorCallback;
795
+
796
+ request.onload = function() {
797
+ clearTimeout( timeoutHandle );
798
+ transport.connectMessageReceiver();
799
+ callback();
800
+ }
801
+ request.setRequestHeader("Content-type","text/stomp");
802
+
803
+ var data = Stomp.marshal("CONNECT", headers);
804
+ request.send(data);
805
+ },
806
+
807
+ connectMessageReceiver: function() {
808
+ var transport = this;
809
+ try {
810
+ this._eventSource = new EventSource( this._url(), { withCredentials: true } );
811
+ this._eventSource.withCredentials = true;
812
+ this._eventSource.onerror = function() {
813
+ transport._eventSource.close();
814
+ transport._eventSource = undefined;
815
+ transport.connectLongPoll();
816
+ };
817
+ this._eventSource.onopen = function() {
818
+ transport._eventSource.onerror = undefined;
819
+ };
820
+ this._eventSource.onmessage = function(e) {
821
+ var message = Stomp.unmarshal( e.data );
822
+ transport.client.processMessage( message );
823
+ };
824
+ } catch (err) {
825
+ console.debug( err );
826
+ this._eventSource.close();
827
+ this._eventSource = undefined;
828
+ this.connectLongPoll();
829
+ }
830
+ },
831
+
832
+ connectLongPoll: function() {
833
+ var transport = this;
834
+
835
+ var request = new XMLHttpRequest();
836
+ request.open( "GET", this._url(), true );
837
+ request.onload = function() {
838
+ var message = Stomp.unmarshal( request.response );
839
+ transport.client.processMessage( message );
840
+ }
841
+
842
+ request.onloadend = function() {
843
+ if ( transport._disconnectReceiver ) {
844
+ return;
845
+ }
846
+ transport.connectLongPoll();
847
+ }
848
+
849
+ setTimeout( function() {
850
+ if ( request.readyState != 0 && request.readyState != 4 ) {
851
+ request.abort();
852
+ }
853
+ }, 10000 );
854
+
855
+ request.setRequestHeader("Accept","text/stomp-poll");
856
+ request.withCredentials = true;
857
+ request.send();
858
+ this._receiverRequest = request;
859
+ },
860
+
861
+ disconnectMessageReceiver: function() {
862
+ this._disconnectReceiver = true;
863
+ if ( this._eventSource ) {
864
+ this._eventSource.close();
865
+ this._eventSource = undefined;
866
+ }
867
+ if ( this._receiverRequest ) {
868
+ this._receiverRequest.abort();
869
+ this._receiverRequest = undefined;
870
+ }
871
+ },
872
+
873
+ close: function() {
874
+ this.disconnectMessageReceiver();
875
+ },
241
876
 
242
- window.Stomp = Stomp;
877
+ transmit: function(command, headers, body, callbacks, timeoutMs) {
878
+ var data = Stomp.marshal(command, headers, body);
879
+ this.send(data, callbacks, timeoutMs);
880
+ },
881
+
882
+ send: function(data, callbacks) {
883
+ callbacks = callbacks || {};
884
+ var request = new XMLHttpRequest();
885
+ request.open( "POST", this._url(), true );
886
+ request.withCredentials = true;
887
+ console.debug( request );
888
+ if ( callbacks['load'] ) {
889
+ request.onload = function() {
890
+ callbacks['load'](request);
891
+ }
892
+ }
893
+ if ( callbacks['error'] ) {
894
+ requrest.onerror = function() {
895
+ callbacks['error'](request);
896
+ }
897
+ }
898
+ request.setRequestHeader("Content-type","text/stomp");
899
+ request.send(data);
900
+ },
901
+
902
+ _url: function() {
903
+ if ( this._secure ) {
904
+ return "https://" + this._host + ":" + this._port + "/";
905
+ }
906
+ return "http://" + this._host + ":" + this._port + "/";
907
+ },
908
+
909
+ };
243
910
 
244
- })(window);
911
+ Stomp.Transports.push( Stomp.Transport.HTTP );