PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/socket.io.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 1429 lines | 1018 code | 194 blank | 217 comment | 236 complexity | b1652545bcbdf7fea93ee1b1a308028b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /** Socket.IO 0.6.2 - Built with build.js */
  2. /**
  3. * Socket.IO client
  4. *
  5. * @author Guillermo Rauch <guillermo@learnboost.com>
  6. * @license The MIT license.
  7. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  8. */
  9. var io = this.io = {
  10. version: '0.6.2',
  11. setPath: function(path){
  12. if (window.console && console.error) console.error('io.setPath will be removed. Please set the variable WEB_SOCKET_SWF_LOCATION pointing to WebSocketMain.swf');
  13. this.path = /\/$/.test(path) ? path : path + '/';
  14. WEB_SOCKET_SWF_LOCATION = path + 'lib/vendor/web-socket-js/WebSocketMain.swf';
  15. }
  16. };
  17. if ('jQuery' in this) jQuery.io = this.io;
  18. if (typeof window != 'undefined'){
  19. // WEB_SOCKET_SWF_LOCATION = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cdn.socket.io/' + this.io.version + '/WebSocketMain.swf';
  20. if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined')
  21. WEB_SOCKET_SWF_LOCATION = '/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf';
  22. }
  23. /**
  24. * Socket.IO client
  25. *
  26. * @author Guillermo Rauch <guillermo@learnboost.com>
  27. * @license The MIT license.
  28. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  29. */
  30. (function(){
  31. var io = this.io;
  32. var _pageLoaded = false;
  33. io.util = {
  34. ios: false,
  35. load: function(fn){
  36. if (/loaded|complete/.test(document.readyState) || _pageLoaded) return fn();
  37. if ('attachEvent' in window){
  38. window.attachEvent('onload', fn);
  39. } else {
  40. window.addEventListener('load', fn, false);
  41. }
  42. },
  43. inherit: function(ctor, superCtor){
  44. // no support for `instanceof` for now
  45. for (var i in superCtor.prototype){
  46. ctor.prototype[i] = superCtor.prototype[i];
  47. }
  48. },
  49. indexOf: function(arr, item, from){
  50. for (var l = arr.length, i = (from < 0) ? Math.max(0, l + from) : from || 0; i < l; i++){
  51. if (arr[i] === item) return i;
  52. }
  53. return -1;
  54. },
  55. isArray: function(obj){
  56. return Object.prototype.toString.call(obj) === '[object Array]';
  57. },
  58. merge: function(target, additional){
  59. for (var i in additional)
  60. if (additional.hasOwnProperty(i))
  61. target[i] = additional[i];
  62. }
  63. };
  64. io.util.ios = /iphone|ipad/i.test(navigator.userAgent);
  65. io.util.android = /android/i.test(navigator.userAgent);
  66. io.util.opera = /opera/i.test(navigator.userAgent);
  67. io.util.load(function(){
  68. _pageLoaded = true;
  69. });
  70. })();
  71. /**
  72. * Socket.IO client
  73. *
  74. * @author Guillermo Rauch <guillermo@learnboost.com>
  75. * @license The MIT license.
  76. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  77. */
  78. // abstract
  79. (function(){
  80. var io = this.io;
  81. var frame = '~m~',
  82. stringify = function(message){
  83. if (Object.prototype.toString.call(message) == '[object Object]'){
  84. if (!('JSON' in window)){
  85. if ('console' in window && console.error) console.error('Trying to encode as JSON, but JSON.stringify is missing.');
  86. return '{ "$error": "Invalid message" }';
  87. }
  88. return '~j~' + JSON.stringify(message);
  89. } else {
  90. return String(message);
  91. }
  92. };
  93. Transport = io.Transport = function(base, options){
  94. this.base = base;
  95. this.options = {
  96. timeout: 15000 // based on heartbeat interval default
  97. };
  98. io.util.merge(this.options, options);
  99. };
  100. Transport.prototype.send = function(){
  101. throw new Error('Missing send() implementation');
  102. };
  103. Transport.prototype.connect = function(){
  104. throw new Error('Missing connect() implementation');
  105. };
  106. Transport.prototype.disconnect = function(){
  107. throw new Error('Missing disconnect() implementation');
  108. };
  109. Transport.prototype._encode = function(messages){
  110. var ret = '', message,
  111. messages = io.util.isArray(messages) ? messages : [messages];
  112. for (var i = 0, l = messages.length; i < l; i++){
  113. message = messages[i] === null || messages[i] === undefined ? '' : stringify(messages[i]);
  114. ret += frame + message.length + frame + message;
  115. }
  116. return ret;
  117. };
  118. Transport.prototype._decode = function(data){
  119. var messages = [], number, n;
  120. do {
  121. if (data.substr(0, 3) !== frame) return messages;
  122. data = data.substr(3);
  123. number = '', n = '';
  124. for (var i = 0, l = data.length; i < l; i++){
  125. n = Number(data.substr(i, 1));
  126. if (data.substr(i, 1) == n){
  127. number += n;
  128. } else {
  129. data = data.substr(number.length + frame.length);
  130. number = Number(number);
  131. break;
  132. }
  133. }
  134. messages.push(data.substr(0, number)); // here
  135. data = data.substr(number);
  136. } while(data !== '');
  137. return messages;
  138. };
  139. Transport.prototype._onData = function(data){
  140. this._setTimeout();
  141. var msgs = this._decode(data);
  142. if (msgs && msgs.length){
  143. for (var i = 0, l = msgs.length; i < l; i++){
  144. this._onMessage(msgs[i]);
  145. }
  146. }
  147. };
  148. Transport.prototype._setTimeout = function(){
  149. var self = this;
  150. if (this._timeout) clearTimeout(this._timeout);
  151. this._timeout = setTimeout(function(){
  152. self._onTimeout();
  153. }, this.options.timeout);
  154. };
  155. Transport.prototype._onTimeout = function(){
  156. this._onDisconnect();
  157. };
  158. Transport.prototype._onMessage = function(message){
  159. if (!this.sessionid){
  160. this.sessionid = message;
  161. this._onConnect();
  162. } else if (message.substr(0, 3) == '~h~'){
  163. this._onHeartbeat(message.substr(3));
  164. } else if (message.substr(0, 3) == '~j~'){
  165. this.base._onMessage(JSON.parse(message.substr(3)));
  166. } else {
  167. this.base._onMessage(message);
  168. }
  169. },
  170. Transport.prototype._onHeartbeat = function(heartbeat){
  171. this.send('~h~' + heartbeat); // echo
  172. };
  173. Transport.prototype._onConnect = function(){
  174. this.connected = true;
  175. this.connecting = false;
  176. this.base._onConnect();
  177. this._setTimeout();
  178. };
  179. Transport.prototype._onDisconnect = function(){
  180. this.connecting = false;
  181. this.connected = false;
  182. this.sessionid = null;
  183. this.base._onDisconnect();
  184. };
  185. Transport.prototype._prepareUrl = function(){
  186. return (this.base.options.secure ? 'https' : 'http')
  187. + '://' + this.base.host
  188. + ':' + this.base.options.port
  189. + '/' + this.base.options.resource
  190. + '/' + this.type
  191. + (this.sessionid ? ('/' + this.sessionid) : '/');
  192. };
  193. })();
  194. /**
  195. * Socket.IO client
  196. *
  197. * @author Guillermo Rauch <guillermo@learnboost.com>
  198. * @license The MIT license.
  199. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  200. */
  201. (function(){
  202. var io = this.io;
  203. var empty = new Function,
  204. XMLHttpRequestCORS = (function(){
  205. if (!('XMLHttpRequest' in window)) return false;
  206. // CORS feature detection
  207. var a = new XMLHttpRequest();
  208. return a.withCredentials != undefined;
  209. })(),
  210. request = function(xdomain){
  211. if ('XDomainRequest' in window && xdomain) return new XDomainRequest();
  212. if ('XMLHttpRequest' in window && (!xdomain || XMLHttpRequestCORS)) return new XMLHttpRequest();
  213. if (!xdomain){
  214. try {
  215. var a = new ActiveXObject('MSXML2.XMLHTTP');
  216. return a;
  217. } catch(e){}
  218. try {
  219. var b = new ActiveXObject('Microsoft.XMLHTTP');
  220. return b;
  221. } catch(e){}
  222. }
  223. return false;
  224. },
  225. XHR = io.Transport.XHR = function(){
  226. io.Transport.apply(this, arguments);
  227. this._sendBuffer = [];
  228. };
  229. io.util.inherit(XHR, io.Transport);
  230. XHR.prototype.connect = function(){
  231. this._get();
  232. return this;
  233. };
  234. XHR.prototype._checkSend = function(){
  235. if (!this._posting && this._sendBuffer.length){
  236. var encoded = this._encode(this._sendBuffer);
  237. this._sendBuffer = [];
  238. this._send(encoded);
  239. }
  240. };
  241. XHR.prototype.send = function(data){
  242. if (io.util.isArray(data)){
  243. this._sendBuffer.push.apply(this._sendBuffer, data);
  244. } else {
  245. this._sendBuffer.push(data);
  246. }
  247. this._checkSend();
  248. return this;
  249. };
  250. XHR.prototype._send = function(data){
  251. var self = this;
  252. this._posting = true;
  253. this._sendXhr = this._request('send', 'POST');
  254. this._sendXhr.onreadystatechange = function(){
  255. var status;
  256. if (self._sendXhr.readyState == 4){
  257. self._sendXhr.onreadystatechange = empty;
  258. try { status = self._sendXhr.status; } catch(e){}
  259. self._posting = false;
  260. if (status == 200){
  261. self._checkSend();
  262. } else {
  263. self._onDisconnect();
  264. }
  265. }
  266. };
  267. this._sendXhr.send('data=' + encodeURIComponent(data));
  268. };
  269. XHR.prototype.disconnect = function(){
  270. // send disconnection signal
  271. this._onDisconnect();
  272. return this;
  273. };
  274. XHR.prototype._onDisconnect = function(){
  275. if (this._xhr){
  276. this._xhr.onreadystatechange = empty;
  277. try {
  278. this._xhr.abort();
  279. } catch(e){}
  280. this._xhr = null;
  281. }
  282. if (this._sendXhr){
  283. this._sendXhr.onreadystatechange = empty;
  284. try {
  285. this._sendXhr.abort();
  286. } catch(e){}
  287. this._sendXhr = null;
  288. }
  289. this._sendBuffer = [];
  290. io.Transport.prototype._onDisconnect.call(this);
  291. };
  292. XHR.prototype._request = function(url, method, multipart){
  293. var req = request(this.base._isXDomain());
  294. if (multipart) req.multipart = true;
  295. req.open(method || 'GET', this._prepareUrl() + (url ? '/' + url : ''));
  296. if (method == 'POST' && 'setRequestHeader' in req){
  297. req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
  298. }
  299. return req;
  300. };
  301. XHR.check = function(xdomain){
  302. try {
  303. if (request(xdomain)) return true;
  304. } catch(e){}
  305. return false;
  306. };
  307. XHR.xdomainCheck = function(){
  308. return XHR.check(true);
  309. };
  310. XHR.request = request;
  311. })();
  312. /**
  313. * Socket.IO client
  314. *
  315. * @author Guillermo Rauch <guillermo@learnboost.com>
  316. * @license The MIT license.
  317. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  318. */
  319. (function(){
  320. var io = this.io;
  321. var WS = io.Transport.websocket = function(){
  322. io.Transport.apply(this, arguments);
  323. };
  324. io.util.inherit(WS, io.Transport);
  325. WS.prototype.type = 'websocket';
  326. WS.prototype.connect = function(){
  327. var self = this;
  328. this.socket = new WebSocket(this._prepareUrl());
  329. this.socket.onmessage = function(ev){ self._onData(ev.data); };
  330. this.socket.onclose = function(ev){ self._onClose(); };
  331. this.socket.onerror = function(e){ self._onError(e); };
  332. return this;
  333. };
  334. WS.prototype.send = function(data){
  335. if (this.socket) this.socket.send(this._encode(data));
  336. return this;
  337. };
  338. WS.prototype.disconnect = function(){
  339. if (this.socket) this.socket.close();
  340. return this;
  341. };
  342. WS.prototype._onClose = function(){
  343. this._onDisconnect();
  344. return this;
  345. };
  346. WS.prototype._onError = function(e){
  347. this.base.emit('error', [e]);
  348. };
  349. WS.prototype._prepareUrl = function(){
  350. return (this.base.options.secure ? 'wss' : 'ws')
  351. + '://' + this.base.host
  352. + ':' + this.base.options.port
  353. + '/' + this.base.options.resource
  354. + '/' + this.type
  355. + (this.sessionid ? ('/' + this.sessionid) : '');
  356. };
  357. WS.check = function(){
  358. // we make sure WebSocket is not confounded with a previously loaded flash WebSocket
  359. return 'WebSocket' in window && WebSocket.prototype && ( WebSocket.prototype.send && !!WebSocket.prototype.send.toString().match(/native/i)) && typeof WebSocket !== "undefined";
  360. };
  361. WS.xdomainCheck = function(){
  362. return true;
  363. };
  364. })();
  365. /**
  366. * Socket.IO client
  367. *
  368. * @author Guillermo Rauch <guillermo@learnboost.com>
  369. * @license The MIT license.
  370. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  371. */
  372. (function(){
  373. var io = this.io;
  374. var Flashsocket = io.Transport.flashsocket = function(){
  375. io.Transport.websocket.apply(this, arguments);
  376. };
  377. io.util.inherit(Flashsocket, io.Transport.websocket);
  378. Flashsocket.prototype.type = 'flashsocket';
  379. Flashsocket.prototype.connect = function(){
  380. var self = this, args = arguments;
  381. WebSocket.__addTask(function(){
  382. io.Transport.websocket.prototype.connect.apply(self, args);
  383. });
  384. return this;
  385. };
  386. Flashsocket.prototype.send = function(){
  387. var self = this, args = arguments;
  388. WebSocket.__addTask(function(){
  389. io.Transport.websocket.prototype.send.apply(self, args);
  390. });
  391. return this;
  392. };
  393. Flashsocket.check = function(){
  394. if (typeof WebSocket == 'undefined' || !('__addTask' in WebSocket)) return false;
  395. if (io.util.opera) return false; // opera is buggy with this transport
  396. if ('navigator' in window && 'plugins' in navigator && navigator.plugins['Shockwave Flash']){
  397. return !!navigator.plugins['Shockwave Flash'].description;
  398. }
  399. if ('ActiveXObject' in window) {
  400. try {
  401. return !!new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
  402. } catch (e) {}
  403. }
  404. return false;
  405. };
  406. Flashsocket.xdomainCheck = function(){
  407. return true;
  408. };
  409. })();
  410. /**
  411. * Socket.IO client
  412. *
  413. * @author Guillermo Rauch <guillermo@learnboost.com>
  414. * @license The MIT license.
  415. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  416. */
  417. (function(){
  418. var io = this.io;
  419. var HTMLFile = io.Transport.htmlfile = function(){
  420. io.Transport.XHR.apply(this, arguments);
  421. };
  422. io.util.inherit(HTMLFile, io.Transport.XHR);
  423. HTMLFile.prototype.type = 'htmlfile';
  424. HTMLFile.prototype._get = function(){
  425. var self = this;
  426. this._open();
  427. window.attachEvent('onunload', function(){ self._destroy(); });
  428. };
  429. HTMLFile.prototype._open = function(){
  430. this._doc = new ActiveXObject('htmlfile');
  431. this._doc.open();
  432. this._doc.write('<html></html>');
  433. this._doc.parentWindow.s = this;
  434. this._doc.close();
  435. var _iframeC = this._doc.createElement('div');
  436. this._doc.body.appendChild(_iframeC);
  437. this._iframe = this._doc.createElement('iframe');
  438. _iframeC.appendChild(this._iframe);
  439. this._iframe.src = this._prepareUrl() + '/' + (+ new Date);
  440. };
  441. HTMLFile.prototype._ = function(data, doc){
  442. this._onData(data);
  443. var script = doc.getElementsByTagName('script')[0];
  444. script.parentNode.removeChild(script);
  445. };
  446. HTMLFile.prototype._destroy = function(){
  447. if (this._iframe){
  448. this._iframe.src = 'about:blank';
  449. this._doc = null;
  450. CollectGarbage();
  451. }
  452. };
  453. HTMLFile.prototype.disconnect = function(){
  454. this._destroy();
  455. return io.Transport.XHR.prototype.disconnect.call(this);
  456. };
  457. HTMLFile.check = function(){
  458. if ('ActiveXObject' in window){
  459. try {
  460. var a = new ActiveXObject('htmlfile');
  461. return a && io.Transport.XHR.check();
  462. } catch(e){}
  463. }
  464. return false;
  465. };
  466. HTMLFile.xdomainCheck = function(){
  467. // we can probably do handling for sub-domains, we should test that it's cross domain but a subdomain here
  468. return false;
  469. };
  470. })();
  471. /**
  472. * Socket.IO client
  473. *
  474. * @author Guillermo Rauch <guillermo@learnboost.com>
  475. * @license The MIT license.
  476. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  477. */
  478. (function(){
  479. var io = this.io;
  480. var XHRMultipart = io.Transport['xhr-multipart'] = function(){
  481. io.Transport.XHR.apply(this, arguments);
  482. };
  483. io.util.inherit(XHRMultipart, io.Transport.XHR);
  484. XHRMultipart.prototype.type = 'xhr-multipart';
  485. XHRMultipart.prototype._get = function(){
  486. var self = this;
  487. this._xhr = this._request('', 'GET', true);
  488. this._xhr.onreadystatechange = function(){
  489. if (self._xhr.readyState == 4) self._onData(self._xhr.responseText);
  490. };
  491. this._xhr.send(null);
  492. };
  493. XHRMultipart.check = function(){
  494. return 'XMLHttpRequest' in window && 'prototype' in XMLHttpRequest && 'multipart' in XMLHttpRequest.prototype;
  495. };
  496. XHRMultipart.xdomainCheck = function(){
  497. return true;
  498. };
  499. })();
  500. /**
  501. * Socket.IO client
  502. *
  503. * @author Guillermo Rauch <guillermo@learnboost.com>
  504. * @license The MIT license.
  505. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  506. */
  507. (function(){
  508. var io = this.io;
  509. var empty = new Function(),
  510. XHRPolling = io.Transport['xhr-polling'] = function(){
  511. io.Transport.XHR.apply(this, arguments);
  512. };
  513. io.util.inherit(XHRPolling, io.Transport.XHR);
  514. XHRPolling.prototype.type = 'xhr-polling';
  515. XHRPolling.prototype.connect = function(){
  516. if (io.util.ios || io.util.android){
  517. var self = this;
  518. io.util.load(function(){
  519. setTimeout(function(){
  520. io.Transport.XHR.prototype.connect.call(self);
  521. }, 10);
  522. });
  523. } else {
  524. io.Transport.XHR.prototype.connect.call(this);
  525. }
  526. };
  527. XHRPolling.prototype._get = function(){
  528. var self = this;
  529. this._xhr = this._request(+ new Date, 'GET');
  530. this._xhr.onreadystatechange = function(){
  531. var status;
  532. if (self._xhr.readyState == 4){
  533. self._xhr.onreadystatechange = empty;
  534. try { status = self._xhr.status; } catch(e){}
  535. if (status == 200){
  536. self._onData(self._xhr.responseText);
  537. self._get();
  538. } else {
  539. self._onDisconnect();
  540. }
  541. }
  542. };
  543. this._xhr.send(null);
  544. };
  545. XHRPolling.check = function(){
  546. return io.Transport.XHR.check();
  547. };
  548. XHRPolling.xdomainCheck = function(){
  549. return io.Transport.XHR.xdomainCheck();
  550. };
  551. })();
  552. /**
  553. * Socket.IO client
  554. *
  555. * @author Guillermo Rauch <guillermo@learnboost.com>
  556. * @license The MIT license.
  557. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  558. */
  559. (function(){
  560. var io = this.io;
  561. io.JSONP = [];
  562. JSONPPolling = io.Transport['jsonp-polling'] = function(){
  563. io.Transport.XHR.apply(this, arguments);
  564. this._insertAt = document.getElementsByTagName('script')[0];
  565. this._index = io.JSONP.length;
  566. io.JSONP.push(this);
  567. };
  568. io.util.inherit(JSONPPolling, io.Transport['xhr-polling']);
  569. JSONPPolling.prototype.type = 'jsonp-polling';
  570. JSONPPolling.prototype._send = function(data){
  571. var self = this;
  572. if (!('_form' in this)){
  573. var form = document.createElement('FORM'),
  574. area = document.createElement('TEXTAREA'),
  575. id = this._iframeId = 'socket_io_iframe_' + this._index,
  576. iframe;
  577. form.style.position = 'absolute';
  578. form.style.top = '-1000px';
  579. form.style.left = '-1000px';
  580. form.target = id;
  581. form.method = 'POST';
  582. form.action = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
  583. area.name = 'data';
  584. form.appendChild(area);
  585. this._insertAt.parentNode.insertBefore(form, this._insertAt);
  586. document.body.appendChild(form);
  587. this._form = form;
  588. this._area = area;
  589. }
  590. function complete(){
  591. initIframe();
  592. self._posting = false;
  593. self._checkSend();
  594. };
  595. function initIframe(){
  596. if (self._iframe){
  597. self._form.removeChild(self._iframe);
  598. }
  599. try {
  600. // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
  601. iframe = document.createElement('<iframe name="'+ self._iframeId +'">');
  602. } catch(e){
  603. iframe = document.createElement('iframe');
  604. iframe.name = self._iframeId;
  605. }
  606. iframe.id = self._iframeId;
  607. self._form.appendChild(iframe);
  608. self._iframe = iframe;
  609. };
  610. initIframe();
  611. this._posting = true;
  612. this._area.value = data;
  613. try {
  614. this._form.submit();
  615. } catch(e){}
  616. if (this._iframe.attachEvent){
  617. iframe.onreadystatechange = function(){
  618. if (self._iframe.readyState == 'complete') complete();
  619. };
  620. } else {
  621. this._iframe.onload = complete;
  622. }
  623. };
  624. JSONPPolling.prototype._get = function(){
  625. var self = this,
  626. script = document.createElement('SCRIPT');
  627. if (this._script){
  628. this._script.parentNode.removeChild(this._script);
  629. this._script = null;
  630. }
  631. script.async = true;
  632. script.src = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
  633. script.onerror = function(){
  634. self._onDisconnect();
  635. };
  636. this._insertAt.parentNode.insertBefore(script, this._insertAt);
  637. this._script = script;
  638. };
  639. JSONPPolling.prototype._ = function(){
  640. this._onData.apply(this, arguments);
  641. this._get();
  642. return this;
  643. };
  644. JSONPPolling.check = function(){
  645. return true;
  646. };
  647. JSONPPolling.xdomainCheck = function(){
  648. return true;
  649. };
  650. })();
  651. /**
  652. * Socket.IO client
  653. *
  654. * @author Guillermo Rauch <guillermo@learnboost.com>
  655. * @license The MIT license.
  656. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  657. */
  658. (function(){
  659. var io = this.io;
  660. var Socket = io.Socket = function(host, options){
  661. this.host = host || document.domain;
  662. this.options = {
  663. secure: false,
  664. document: document,
  665. port: document.location.port || 80,
  666. resource: 'socket.io',
  667. transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling'],
  668. transportOptions: {
  669. 'xhr-polling': {
  670. timeout: 25000 // based on polling duration default
  671. },
  672. 'jsonp-polling': {
  673. timeout: 25000
  674. }
  675. },
  676. connectTimeout: 5000,
  677. reconnect: true,
  678. reconnectionDelay: 500,
  679. maxReconnectionAttempts: 10,
  680. tryTransportsOnConnectTimeout: true,
  681. rememberTransport: true
  682. };
  683. io.util.merge(this.options, options);
  684. this.connected = false;
  685. this.connecting = false;
  686. this._events = {};
  687. this.transport = this.getTransport();
  688. if (!this.transport && 'console' in window) console.error('No transport available');
  689. };
  690. Socket.prototype.getTransport = function(override){
  691. var transports = override || this.options.transports, match;
  692. if (this.options.rememberTransport && !override){
  693. match = this.options.document.cookie.match('(?:^|;)\\s*socketio=([^;]*)');
  694. if (match){
  695. this._rememberedTransport = true;
  696. transports = [decodeURIComponent(match[1])];
  697. }
  698. }
  699. for (var i = 0, transport; transport = transports[i]; i++){
  700. if (io.Transport[transport]
  701. && io.Transport[transport].check()
  702. && (!this._isXDomain() || io.Transport[transport].xdomainCheck())){
  703. return new io.Transport[transport](this, this.options.transportOptions[transport] || {});
  704. }
  705. }
  706. return null;
  707. };
  708. Socket.prototype.connect = function(){
  709. if (this.transport && !this.connected){
  710. if (this.connecting) this.disconnect(true);
  711. this.connecting = true;
  712. this.emit('connecting', [this.transport.type]);
  713. this.transport.connect();
  714. if (this.options.connectTimeout){
  715. var self = this;
  716. this.connectTimeoutTimer = setTimeout(function(){
  717. if (!self.connected){
  718. self.disconnect(true);
  719. if (self.options.tryTransportsOnConnectTimeout && !self._rememberedTransport){
  720. if(!self._remainingTransports) self._remainingTransports = self.options.transports.slice(0);
  721. var transports = self._remainingTransports;
  722. while(transports.length > 0 && transports.splice(0,1)[0] != self.transport.type){}
  723. if(transports.length){
  724. self.transport = self.getTransport(transports);
  725. self.connect();
  726. }
  727. }
  728. if(!self._remainingTransports || self._remainingTransports.length == 0) self.emit('connect_failed');
  729. }
  730. if(self._remainingTransports && self._remainingTransports.length == 0) delete self._remainingTransports;
  731. }, this.options.connectTimeout);
  732. }
  733. }
  734. return this;
  735. };
  736. Socket.prototype.send = function(data){
  737. if (!this.transport || !this.transport.connected) return this._queue(data);
  738. this.transport.send(data);
  739. return this;
  740. };
  741. Socket.prototype.disconnect = function(reconnect){
  742. if (this.connectTimeoutTimer) clearTimeout(this.connectTimeoutTimer);
  743. if (!reconnect) this.options.reconnect = false;
  744. this.transport.disconnect();
  745. return this;
  746. };
  747. Socket.prototype.on = function(name, fn){
  748. if (!(name in this._events)) this._events[name] = [];
  749. this._events[name].push(fn);
  750. return this;
  751. };
  752. Socket.prototype.emit = function(name, args){
  753. if (name in this._events){
  754. var events = this._events[name].concat();
  755. for (var i = 0, ii = events.length; i < ii; i++)
  756. events[i].apply(this, args === undefined ? [] : args);
  757. }
  758. return this;
  759. };
  760. Socket.prototype.removeEvent = function(name, fn){
  761. if (name in this._events){
  762. for (var a = 0, l = this._events[name].length; a < l; a++)
  763. if (this._events[name][a] == fn) this._events[name].splice(a, 1);
  764. }
  765. return this;
  766. };
  767. Socket.prototype._queue = function(message){
  768. if (!('_queueStack' in this)) this._queueStack = [];
  769. this._queueStack.push(message);
  770. return this;
  771. };
  772. Socket.prototype._doQueue = function(){
  773. if (!('_queueStack' in this) || !this._queueStack.length) return this;
  774. this.transport.send(this._queueStack);
  775. this._queueStack = [];
  776. return this;
  777. };
  778. Socket.prototype._isXDomain = function(){
  779. var locPort = window.location.port || 80;
  780. return this.host !== document.domain || this.options.port != locPort;
  781. };
  782. Socket.prototype._onConnect = function(){
  783. this.connected = true;
  784. this.connecting = false;
  785. this._doQueue();
  786. if (this.options.rememberTransport) this.options.document.cookie = 'socketio=' + encodeURIComponent(this.transport.type);
  787. this.emit('connect');
  788. };
  789. Socket.prototype._onMessage = function(data){
  790. this.emit('message', [data]);
  791. };
  792. Socket.prototype._onDisconnect = function(){
  793. var wasConnected = this.connected;
  794. this.connected = false;
  795. this.connecting = false;
  796. this._queueStack = [];
  797. if (wasConnected){
  798. this.emit('disconnect');
  799. if (this.options.reconnect && !this.reconnecting) this._onReconnect();
  800. }
  801. };
  802. Socket.prototype._onReconnect = function(){
  803. this.reconnecting = true;
  804. this.reconnectionAttempts = 0;
  805. this.reconnectionDelay = this.options.reconnectionDelay;
  806. var self = this
  807. , tryTransportsOnConnectTimeout = this.options.tryTransportsOnConnectTimeout
  808. , rememberTransport = this.options.rememberTransport;
  809. function reset(){
  810. if(self.connected) self.emit('reconnect',[self.transport.type,self.reconnectionAttempts]);
  811. self.removeEvent('connect_failed', maybeReconnect).removeEvent('connect', maybeReconnect);
  812. delete self.reconnecting;
  813. delete self.reconnectionAttempts;
  814. delete self.reconnectionDelay;
  815. delete self.reconnectionTimer;
  816. delete self.redoTransports;
  817. self.options.tryTransportsOnConnectTimeout = tryTransportsOnConnectTimeout;
  818. self.options.rememberTransport = rememberTransport;
  819. return;
  820. };
  821. function maybeReconnect(){
  822. if (!self.reconnecting) return;
  823. if (!self.connected){
  824. if (self.connecting && self.reconnecting) return self.reconnectionTimer = setTimeout(maybeReconnect, 1000);
  825. if (self.reconnectionAttempts++ >= self.options.maxReconnectionAttempts){
  826. if (!self.redoTransports){
  827. self.on('connect_failed', maybeReconnect);
  828. self.options.tryTransportsOnConnectTimeout = true;
  829. self.transport = self.getTransport(self.options.transports); // overwrite with all enabled transports
  830. self.redoTransports = true;
  831. self.connect();
  832. } else {
  833. self.emit('reconnect_failed');
  834. reset();
  835. }
  836. } else {
  837. self.reconnectionDelay *= 2; // exponential backoff
  838. self.connect();
  839. self.emit('reconnecting', [self.reconnectionDelay,self.reconnectionAttempts]);
  840. self.reconnectionTimer = setTimeout(maybeReconnect, self.reconnectionDelay);
  841. }
  842. } else {
  843. reset();
  844. }
  845. };
  846. this.options.tryTransportsOnConnectTimeout = false;
  847. this.reconnectionTimer = setTimeout(maybeReconnect, this.reconnectionDelay);
  848. this.on('connect', maybeReconnect);
  849. };
  850. Socket.prototype.fire = Socket.prototype.emit;
  851. Socket.prototype.addListener = Socket.prototype.addEvent = Socket.prototype.addEventListener = Socket.prototype.on;
  852. Socket.prototype.removeListener = Socket.prototype.removeEventListener = Socket.prototype.removeEvent;
  853. })();
  854. /* SWFObject v2.2 <http://code.google.com/p/swfobject/>
  855. is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
  856. */
  857. 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}}}}();
  858. // Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
  859. // License: New BSD License
  860. // Reference: http://dev.w3.org/html5/websockets/
  861. // Reference: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
  862. (function() {
  863. if (window.WebSocket) return;
  864. var console = window.console;
  865. if (!console || !console.log || !console.error) {
  866. console = {log: function(){ }, error: function(){ }};
  867. }
  868. if (!swfobject.hasFlashPlayerVersion("10.0.0")) {
  869. console.error("Flash Player >= 10.0.0 is required.");
  870. return;
  871. }
  872. if (location.protocol == "file:") {
  873. console.error(
  874. "WARNING: web-socket-js doesn't work in file:///... URL " +
  875. "unless you set Flash Security Settings properly. " +
  876. "Open the page via Web server i.e. http://...");
  877. }
  878. /**
  879. * This class represents a faux web socket.
  880. * @param {string} url
  881. * @param {string} protocol
  882. * @param {string} proxyHost
  883. * @param {int} proxyPort
  884. * @param {string} headers
  885. */
  886. WebSocket = function(url, protocol, proxyHost, proxyPort, headers) {
  887. var self = this;
  888. self.__id = WebSocket.__nextId++;
  889. WebSocket.__instances[self.__id] = self;
  890. self.readyState = WebSocket.CONNECTING;
  891. self.bufferedAmount = 0;
  892. // Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc.
  893. // Otherwise, when onopen fires immediately, onopen is called before it is set.
  894. setTimeout(function() {
  895. WebSocket.__addTask(function() {
  896. WebSocket.__flash.create(
  897. self.__id, url, protocol, proxyHost || null, proxyPort || 0, headers || null);
  898. });
  899. }, 0);
  900. };
  901. /**
  902. * Send data to the web socket.
  903. * @param {string} data The data to send to the socket.
  904. * @return {boolean} True for success, false for failure.
  905. */
  906. WebSocket.prototype.send = function(data) {
  907. if (this.readyState == WebSocket.CONNECTING) {
  908. throw "INVALID_STATE_ERR: Web Socket connection has not been established";
  909. }
  910. // We use encodeURIComponent() here, because FABridge doesn't work if
  911. // the argument includes some characters. We don't use escape() here
  912. // because of this:
  913. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions
  914. // But it looks decodeURIComponent(encodeURIComponent(s)) doesn't
  915. // preserve all Unicode characters either e.g. "\uffff" in Firefox.
  916. // Note by wtritch: Hopefully this will not be necessary using ExternalInterface. Will require
  917. // additional testing.
  918. var result = WebSocket.__flash.send(this.__id, encodeURIComponent(data));
  919. if (result < 0) { // success
  920. return true;
  921. } else {
  922. this.bufferedAmount += result;
  923. return false;
  924. }
  925. };
  926. /**
  927. * Close this web socket gracefully.
  928. */
  929. WebSocket.prototype.close = function() {
  930. if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
  931. return;
  932. }
  933. this.readyState = WebSocket.CLOSING;
  934. WebSocket.__flash.close(this.__id);
  935. };
  936. /**
  937. * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
  938. *
  939. * @param {string} type
  940. * @param {function} listener
  941. * @param {boolean} useCapture !NB Not implemented yet
  942. * @return void
  943. */
  944. WebSocket.prototype.addEventListener = function(type, listener, useCapture) {
  945. if (!('__events' in this)) {
  946. this.__events = {};
  947. }
  948. if (!(type in this.__events)) {
  949. this.__events[type] = [];
  950. if ('function' == typeof this['on' + type]) {
  951. this.__events[type].defaultHandler = this['on' + type];
  952. this['on' + type] = this.__createEventHandler(this, type);
  953. }
  954. }
  955. this.__events[type].push(listener);
  956. };
  957. /**
  958. * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
  959. *
  960. * @param {string} type
  961. * @param {function} listener
  962. * @param {boolean} useCapture NB! Not implemented yet
  963. * @return void
  964. */
  965. WebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
  966. if (!('__events' in this)) {
  967. this.__events = {};
  968. }
  969. if (!(type in this.__events)) return;
  970. for (var i = this.__events.length; i > -1; --i) {
  971. if (listener === this.__events[type][i]) {
  972. this.__events[type].splice(i, 1);
  973. break;
  974. }
  975. }
  976. };
  977. /**
  978. * Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
  979. *
  980. * @param {WebSocketEvent} event
  981. * @return void
  982. */
  983. WebSocket.prototype.dispatchEvent = function(event) {
  984. if (!('__events' in this)) throw 'UNSPECIFIED_EVENT_TYPE_ERR';
  985. if (!(event.type in this.__events)) throw 'UNSPECIFIED_EVENT_TYPE_ERR';
  986. for (var i = 0, l = this.__events[event.type].length; i < l; ++ i) {
  987. this.__events[event.type][i](event);
  988. if (event.cancelBubble) break;
  989. }
  990. if (false !== event.returnValue &&
  991. 'function' == typeof this.__events[event.type].defaultHandler)
  992. {
  993. this.__events[event.type].defaultHandler(event);
  994. }
  995. };
  996. /**
  997. * Handle an event from flash. Do any websocket-specific
  998. * handling before passing the event off to the event handlers.
  999. * @param {Object} event
  1000. */
  1001. WebSocket.prototype.__handleEvent = function(event) {
  1002. if ("readyState" in event) {
  1003. this.readyState = event.readyState;
  1004. }
  1005. try {
  1006. if (event.type == "open") {
  1007. this.onopen && this.onopen();
  1008. } else if (event.type == "close") {
  1009. this.onclose && this.onclose();
  1010. } else if (event.type == "error") {
  1011. this.onerror && this.onerror(event);
  1012. } else if (event.type == "message") {
  1013. if (this.onmessage) {
  1014. var data = decodeURIComponent(event.message);
  1015. var e;
  1016. if (window.MessageEvent && !window.opera) {
  1017. e = document.createEvent("MessageEvent");
  1018. e.initMessageEvent("message", false, false, data, null, null, window, null);
  1019. } else {
  1020. // IE and Opera, the latter one truncates the data parameter after any 0x00 bytes.
  1021. e = {data: data};
  1022. }
  1023. this.onmessage(e);
  1024. }
  1025. } else {
  1026. throw "unknown event type: " + event.type;
  1027. }
  1028. } catch (e) {
  1029. console.error(e.toString());
  1030. }
  1031. };
  1032. /**
  1033. * @param {object} object
  1034. * @param {string} type
  1035. */
  1036. WebSocket.prototype.__createEventHandler = function(object, type) {
  1037. return function(data) {
  1038. var event = new WebSocketEvent();
  1039. event.initEvent(type, true, true);
  1040. event.target = event.currentTarget = object;
  1041. for (var key in data) {
  1042. event[key] = data[key];
  1043. }
  1044. object.dispatchEvent(event, arguments);
  1045. };
  1046. };
  1047. /**
  1048. * Define the WebSocket readyState enumeration.
  1049. */
  1050. WebSocket.CONNECTING = 0;
  1051. WebSocket.OPEN = 1;
  1052. WebSocket.CLOSING = 2;
  1053. WebSocket.CLOSED = 3;
  1054. WebSocket.__flash = null;
  1055. WebSocket.__instances = {};
  1056. WebSocket.__tasks = [];
  1057. WebSocket.__nextId = 0;
  1058. /**
  1059. * Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
  1060. */
  1061. WebSocket.__initialize = function() {
  1062. if (WebSocket.__flash) return;
  1063. if (WebSocket.__swfLocation) {
  1064. // For backword compatibility.
  1065. window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
  1066. }
  1067. if (!window.WEB_SOCKET_SWF_LOCATION) {
  1068. console.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
  1069. return;
  1070. }
  1071. var container = document.createElement("div");
  1072. container.id = "webSocketContainer";
  1073. // Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
  1074. // Flash from loading at least in IE. So we move it out of the screen at (-100, -100).
  1075. // But this even doesn't work with Flash Lite (e.g. in Droid Incredible). So with Flash
  1076. // Lite, we put it at (0, 0). This shows 1x1 box visible at left-top corner but this is
  1077. // the best we can do as far as we know now.
  1078. container.style.position = "absolute";
  1079. if (WebSocket.__isFlashLite()) {
  1080. container.style.left = "0px";
  1081. container.style.top = "0px";
  1082. } else {
  1083. container.style.left = "-100px";
  1084. container.style.top = "-100px";
  1085. }
  1086. var holder = document.createElement("div");
  1087. holder.id = "webSocketFlash";
  1088. container.appendChild(holder);
  1089. document.body.appendChild(container);
  1090. // See this article for hasPriority:
  1091. // http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-7ffd.html
  1092. swfobject.embedSWF(
  1093. WEB_SOCKET_SWF_LOCATION,
  1094. "webSocketFlash",
  1095. "1" /* width */,
  1096. "1" /* height */,
  1097. "10.0.0" /* SWF version */,
  1098. null,
  1099. null,
  1100. {hasPriority: true, swliveconnect : true, allowScriptAccess: "always"},
  1101. null,
  1102. function(e) {
  1103. if (!e.success) {
  1104. console.error("[WebSocket] swfobject.embedSWF failed");
  1105. }
  1106. });
  1107. };
  1108. /**
  1109. * Load a new flash security policy file.
  1110. * @param {string} url
  1111. */
  1112. WebSocket.loadFlashPolicyFile = function(url){
  1113. WebSocket.__addTask(function() {
  1114. WebSocket.__flash.loadManualPolicyFile(url);
  1115. });
  1116. };
  1117. /**
  1118. * Called by flash to notify js that it's fully loaded and ready
  1119. * for communication.
  1120. */
  1121. WebSocket.__onFlashInitialized = function() {
  1122. // We need to set a timeout here to avoid round-trip calls
  1123. // to flash during the initialization process.
  1124. setTimeout(function() {
  1125. WebSocket.__flash = document.getElementById("webSocketFlash");
  1126. WebSocket.__flash.setCallerUrl(location.href);
  1127. WebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
  1128. for (var i = 0; i < WebSocket.__tasks.length; ++i) {
  1129. WebSocket.__tasks[i]();
  1130. }
  1131. WebSocket.__tasks = [];
  1132. }, 0);
  1133. };
  1134. /**
  1135. * Called by flash to dispatch an event to a web socket.
  1136. * @param {object} eventObj A web socket event dispatched from flash.
  1137. */
  1138. WebSocket.__onFlashEvent = function() {
  1139. setTimeout(function() {
  1140. // Gets events using receiveEvents() instead of getting it from event object
  1141. // of Flash event. This is to make sure to keep message order.
  1142. // It seems sometimes Flash events don't arrive in the same order as they are sent.
  1143. var events = WebSocket.__flash.receiveEvents();
  1144. for (var i = 0; i < events.length; ++i) {
  1145. WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
  1146. }
  1147. }, 0);
  1148. return true;
  1149. };
  1150. // called from Flash
  1151. WebSocket.__log = function(message) {
  1152. console.log(decodeURIComponent(message));
  1153. };
  1154. // called from Flash
  1155. WebSocket.__error = function(message) {
  1156. console.error(decodeURIComponent(message));
  1157. };
  1158. WebSocket.__addTask = function(task) {
  1159. if (WebSocket.__flash) {
  1160. task();
  1161. } else {
  1162. WebSocket.__tasks.push(task);
  1163. }
  1164. };
  1165. /**
  1166. * Test if the browser is running flash lite.
  1167. * @return {boolean} True if flash lite is running, false otherwise.
  1168. */
  1169. WebSocket.__isFlashLite = function() {
  1170. if (!window.navigator || !window.navigator.mimeTypes) {
  1171. return false;
  1172. }
  1173. var mimeType = window.navigator.mimeTypes["application/x-shockwave-flash"];
  1174. if (!mimeType || !mimeType.enabledPlugin || !mimeType.enabledPlugin.filename) {
  1175. return false;
  1176. }
  1177. return mimeType.enabledPlugin.filename.match(/flashlite/i) ? true : false;
  1178. };
  1179. /**
  1180. * Basic implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-interface">DOM 2 EventInterface</a>}
  1181. *
  1182. * @class
  1183. * @constructor
  1184. */
  1185. function WebSocketEvent(){}
  1186. /**
  1187. *
  1188. * @type boolean
  1189. */
  1190. WebSocketEvent.prototype.cancelable = true;
  1191. /**
  1192. *
  1193. * @type boolean
  1194. */
  1195. WebSocketEvent.prototype.cancelBubble = false;
  1196. /**
  1197. *
  1198. * @return void
  1199. */
  1200. WebSocketEvent.prototype.preventDefault = function() {
  1201. if (this.cancelable) {
  1202. this.returnValue = false;
  1203. }
  1204. };
  1205. /**
  1206. *
  1207. * @return void
  1208. */
  1209. WebSocketEvent.prototype.stopPropagation = function() {
  1210. this.cancelBubble = true;
  1211. };
  1212. /**
  1213. *
  1214. * @param {string} eventTypeArg
  1215. * @param {boolean} canBubbleArg
  1216. * @param {boolean} cancelableArg
  1217. * @return void
  1218. */
  1219. WebSocketEvent.prototype.initEvent = function(eventTypeArg, canBubbleArg, cancelableArg) {
  1220. this.type = eventTypeArg;
  1221. this.cancelable = cancelableArg;
  1222. this.timeStamp = new Date();
  1223. };
  1224. if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
  1225. if (window.addEventListener) {
  1226. window.addEventListener("load", function(){
  1227. WebSocket.__initialize();
  1228. }, false);
  1229. } else {
  1230. window.attachEvent("onload", function(){
  1231. WebSocket.__initialize();
  1232. });
  1233. }
  1234. }
  1235. })();