Guest User

Untitled

a guest
Jan 23rd, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.76 KB | None | 0 0
  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.  
  10. this.io = {
  11. version: '0.6.2',
  12.  
  13. setPath: function(path){
  14. if (window.console && console.error) console.error('io.setPath will be removed. Please set the variable WEB_SOCKET_SWF_LOCATION pointing to WebSocketMain.swf');
  15. this.path = /\/$/.test(path) ? path : path + '/';
  16. WEB_SOCKET_SWF_LOCATION = path + 'lib/vendor/web-socket-js/WebSocketMain.swf';
  17. }
  18. };
  19.  
  20. if ('jQuery' in this) jQuery.io = this.io;
  21.  
  22. if (typeof window != 'undefined'){
  23. // WEB_SOCKET_SWF_LOCATION = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cdn.socket.io/' + this.io.version + '/WebSocketMain.swf';
  24. if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined')
  25. WEB_SOCKET_SWF_LOCATION = '/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf';
  26. }
  27.  
  28. /**
  29. * Socket.IO client
  30. *
  31. * @author Guillermo Rauch <guillermo@learnboost.com>
  32. * @license The MIT license.
  33. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  34. */
  35.  
  36. (function(){
  37.  
  38. var _pageLoaded = false;
  39.  
  40. io.util = {
  41.  
  42. ios: false,
  43.  
  44. load: function(fn){
  45. if (/loaded|complete/.test(document.readyState) || _pageLoaded) return fn();
  46. if ('attachEvent' in window){
  47. window.attachEvent('onload', fn);
  48. } else {
  49. window.addEventListener('load', fn, false);
  50. }
  51. },
  52.  
  53. inherit: function(ctor, superCtor){
  54. // no support for `instanceof` for now
  55. for (var i in superCtor.prototype){
  56. ctor.prototype[i] = superCtor.prototype[i];
  57. }
  58. },
  59.  
  60. indexOf: function(arr, item, from){
  61. for (var l = arr.length, i = (from < 0) ? Math.max(0, l + from) : from || 0; i < l; i++){
  62. if (arr[i] === item) return i;
  63. }
  64. return -1;
  65. },
  66.  
  67. isArray: function(obj){
  68. return Object.prototype.toString.call(obj) === '[object Array]';
  69. },
  70.  
  71. merge: function(target, additional){
  72. for (var i in additional)
  73. if (additional.hasOwnProperty(i))
  74. target[i] = additional[i];
  75. }
  76.  
  77. };
  78.  
  79. io.util.ios = /iphone|ipad/i.test(navigator.userAgent);
  80. io.util.android = /android/i.test(navigator.userAgent);
  81. io.util.opera = /opera/i.test(navigator.userAgent);
  82.  
  83. io.util.load(function(){
  84. _pageLoaded = true;
  85. });
  86.  
  87. })();
  88.  
  89. /**
  90. * Socket.IO client
  91. *
  92. * @author Guillermo Rauch <guillermo@learnboost.com>
  93. * @license The MIT license.
  94. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  95. */
  96.  
  97. // abstract
  98.  
  99. (function(){
  100.  
  101. var frame = '~m~',
  102.  
  103. stringify = function(message){
  104. if (Object.prototype.toString.call(message) == '[object Object]'){
  105. if (!('JSON' in window)){
  106. if ('console' in window && console.error) console.error('Trying to encode as JSON, but JSON.stringify is missing.');
  107. return '{ "$error": "Invalid message" }';
  108. }
  109. return '~j~' + JSON.stringify(message);
  110. } else {
  111. return String(message);
  112. }
  113. };
  114.  
  115. Transport = io.Transport = function(base, options){
  116. this.base = base;
  117. this.options = {
  118. timeout: 15000 // based on heartbeat interval default
  119. };
  120. io.util.merge(this.options, options);
  121. };
  122.  
  123. Transport.prototype.send = function(){
  124. throw new Error('Missing send() implementation');
  125. };
  126.  
  127. Transport.prototype.connect = function(){
  128. throw new Error('Missing connect() implementation');
  129. };
  130.  
  131. Transport.prototype.disconnect = function(){
  132. throw new Error('Missing disconnect() implementation');
  133. };
  134.  
  135. Transport.prototype._encode = function(messages){
  136. var ret = '', message,
  137. messages = io.util.isArray(messages) ? messages : [messages];
  138. for (var i = 0, l = messages.length; i < l; i++){
  139. message = messages[i] === null || messages[i] === undefined ? '' : stringify(messages[i]);
  140. ret += frame + message.length + frame + message;
  141. }
  142. return ret;
  143. };
  144.  
  145. Transport.prototype._decode = function(data){
  146. var messages = [], number, n;
  147. do {
  148. if (data.substr(0, 3) !== frame) return messages;
  149. data = data.substr(3);
  150. number = '', n = '';
  151. for (var i = 0, l = data.length; i < l; i++){
  152. n = Number(data.substr(i, 1));
  153. if (data.substr(i, 1) == n){
  154. number += n;
  155. } else {
  156. data = data.substr(number.length + frame.length);
  157. number = Number(number);
  158. break;
  159. }
  160. }
  161. messages.push(data.substr(0, number)); // here
  162. data = data.substr(number);
  163. } while(data !== '');
  164. return messages;
  165. };
  166.  
  167. Transport.prototype._onData = function(data){
  168. this._setTimeout();
  169. var msgs = this._decode(data);
  170. if (msgs && msgs.length){
  171. for (var i = 0, l = msgs.length; i < l; i++){
  172. this._onMessage(msgs[i]);
  173. }
  174. }
  175. };
  176.  
  177. Transport.prototype._setTimeout = function(){
  178. var self = this;
  179. if (this._timeout) clearTimeout(this._timeout);
  180. this._timeout = setTimeout(function(){
  181. self._onTimeout();
  182. }, this.options.timeout);
  183. };
  184.  
  185. Transport.prototype._onTimeout = function(){
  186. this._onDisconnect();
  187. };
  188.  
  189. Transport.prototype._onMessage = function(message){
  190. if (!this.sessionid){
  191. this.sessionid = message;
  192. this._onConnect();
  193. } else if (message.substr(0, 3) == '~h~'){
  194. this._onHeartbeat(message.substr(3));
  195. } else if (message.substr(0, 3) == '~j~'){
  196. this.base._onMessage(JSON.parse(message.substr(3)));
  197. } else {
  198. this.base._onMessage(message);
  199. }
  200. },
  201.  
  202. Transport.prototype._onHeartbeat = function(heartbeat){
  203. this.send('~h~' + heartbeat); // echo
  204. };
  205.  
  206. Transport.prototype._onConnect = function(){
  207. this.connected = true;
  208. this.connecting = false;
  209. this.base._onConnect();
  210. this._setTimeout();
  211. };
  212.  
  213. Transport.prototype._onDisconnect = function(){
  214. this.connecting = false;
  215. this.connected = false;
  216. this.sessionid = null;
  217. this.base._onDisconnect();
  218. };
  219.  
  220. Transport.prototype._prepareUrl = function(){
  221. return (this.base.options.secure ? 'https' : 'http')
  222. + '://' + this.base.host
  223. + ':' + this.base.options.port
  224. + '/' + this.base.options.resource
  225. + '/' + this.type
  226. + (this.sessionid ? ('/' + this.sessionid) : '/');
  227. };
  228.  
  229. })();
  230. /**
  231. * Socket.IO client
  232. *
  233. * @author Guillermo Rauch <guillermo@learnboost.com>
  234. * @license The MIT license.
  235. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  236. */
  237.  
  238. (function(){
  239.  
  240. var empty = new Function,
  241.  
  242. XMLHttpRequestCORS = (function(){
  243. if (!('XMLHttpRequest' in window)) return false;
  244. // CORS feature detection
  245. var a = new XMLHttpRequest();
  246. return a.withCredentials != undefined;
  247. })(),
  248.  
  249. request = function(xdomain){
  250. if ('XDomainRequest' in window && xdomain) return new XDomainRequest();
  251. if ('XMLHttpRequest' in window && (!xdomain || XMLHttpRequestCORS)) return new XMLHttpRequest();
  252. if (!xdomain){
  253. try {
  254. var a = new ActiveXObject('MSXML2.XMLHTTP');
  255. return a;
  256. } catch(e){}
  257.  
  258. try {
  259. var b = new ActiveXObject('Microsoft.XMLHTTP');
  260. return b;
  261. } catch(e){}
  262. }
  263. return false;
  264. },
  265.  
  266. XHR = io.Transport.XHR = function(){
  267. io.Transport.apply(this, arguments);
  268. this._sendBuffer = [];
  269. };
  270.  
  271. io.util.inherit(XHR, io.Transport);
  272.  
  273. XHR.prototype.connect = function(){
  274. this._get();
  275. return this;
  276. };
  277.  
  278. XHR.prototype._checkSend = function(){
  279. if (!this._posting && this._sendBuffer.length){
  280. var encoded = this._encode(this._sendBuffer);
  281. this._sendBuffer = [];
  282. this._send(encoded);
  283. }
  284. };
  285.  
  286. XHR.prototype.send = function(data){
  287. if (io.util.isArray(data)){
  288. this._sendBuffer.push.apply(this._sendBuffer, data);
  289. } else {
  290. this._sendBuffer.push(data);
  291. }
  292. this._checkSend();
  293. return this;
  294. };
  295.  
  296. XHR.prototype._send = function(data){
  297. var self = this;
  298. this._posting = true;
  299. this._sendXhr = this._request('send', 'POST');
  300. this._sendXhr.onreadystatechange = function(){
  301. var status;
  302. if (self._sendXhr.readyState == 4){
  303. self._sendXhr.onreadystatechange = empty;
  304. try { status = self._sendXhr.status; } catch(e){}
  305. self._posting = false;
  306. if (status == 200){
  307. self._checkSend();
  308. } else {
  309. self._onDisconnect();
  310. }
  311. }
  312. };
  313. this._sendXhr.send('data=' + encodeURIComponent(data));
  314. };
  315.  
  316. XHR.prototype.disconnect = function(){
  317. // send disconnection signal
  318. this._onDisconnect();
  319. return this;
  320. };
  321.  
  322. XHR.prototype._onDisconnect = function(){
  323. if (this._xhr){
  324. this._xhr.onreadystatechange = empty;
  325. try {
  326. this._xhr.abort();
  327. } catch(e){}
  328. this._xhr = null;
  329. }
  330. if (this._sendXhr){
  331. this._sendXhr.onreadystatechange = empty;
  332. try {
  333. this._sendXhr.abort();
  334. } catch(e){}
  335. this._sendXhr = null;
  336. }
  337. this._sendBuffer = [];
  338. io.Transport.prototype._onDisconnect.call(this);
  339. };
  340.  
  341. XHR.prototype._request = function(url, method, multipart){
  342. var req = request(this.base._isXDomain());
  343. if (multipart) req.multipart = true;
  344. req.open(method || 'GET', this._prepareUrl() + (url ? '/' + url : ''));
  345. if (method == 'POST' && 'setRequestHeader' in req){
  346. req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
  347. }
  348. return req;
  349. };
  350.  
  351. XHR.check = function(xdomain){
  352. try {
  353. if (request(xdomain)) return true;
  354. } catch(e){}
  355. return false;
  356. };
  357.  
  358. XHR.xdomainCheck = function(){
  359. return XHR.check(true);
  360. };
  361.  
  362. XHR.request = request;
  363.  
  364. })();
  365.  
  366. /**
  367. * Socket.IO client
  368. *
  369. * @author Guillermo Rauch <guillermo@learnboost.com>
  370. * @license The MIT license.
  371. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  372. */
  373.  
  374. (function(){
  375.  
  376. var WS = io.Transport.websocket = function(){
  377. io.Transport.apply(this, arguments);
  378. };
  379.  
  380. io.util.inherit(WS, io.Transport);
  381.  
  382. WS.prototype.type = 'websocket';
  383.  
  384. WS.prototype.connect = function(){
  385. var self = this;
  386. this.socket = new WebSocket(this._prepareUrl());
  387. this.socket.onmessage = function(ev){ self._onData(ev.data); };
  388. this.socket.onclose = function(ev){ self._onClose(); };
  389. this.socket.onerror = function(e){ self._onError(e); };
  390. return this;
  391. };
  392.  
  393. WS.prototype.send = function(data){
  394. if (this.socket) this.socket.send(this._encode(data));
  395. return this;
  396. };
  397.  
  398. WS.prototype.disconnect = function(){
  399. if (this.socket) this.socket.close();
  400. return this;
  401. };
  402.  
  403. WS.prototype._onClose = function(){
  404. this._onDisconnect();
  405. return this;
  406. };
  407.  
  408. WS.prototype._onError = function(e){
  409. this.base.emit('error', [e]);
  410. };
  411.  
  412. WS.prototype._prepareUrl = function(){
  413. return (this.base.options.secure ? 'wss' : 'ws')
  414. + '://' + this.base.host
  415. + ':' + this.base.options.port
  416. + '/' + this.base.options.resource
  417. + '/' + this.type
  418. + (this.sessionid ? ('/' + this.sessionid) : '');
  419. };
  420.  
  421. WS.check = function(){
  422. // we make sure WebSocket is not confounded with a previously loaded flash WebSocket
  423. return 'WebSocket' in window && WebSocket.prototype && ( WebSocket.prototype.send && !!WebSocket.prototype.send.toString().match(/native/i)) && typeof WebSocket !== "undefined";
  424. };
  425.  
  426. WS.xdomainCheck = function(){
  427. return true;
  428. };
  429.  
  430. })();
  431.  
  432. /**
  433. * Socket.IO client
  434. *
  435. * @author Guillermo Rauch <guillermo@learnboost.com>
  436. * @license The MIT license.
  437. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  438. */
  439.  
  440. (function(){
  441.  
  442. var Flashsocket = io.Transport.flashsocket = function(){
  443. io.Transport.websocket.apply(this, arguments);
  444. };
  445.  
  446. io.util.inherit(Flashsocket, io.Transport.websocket);
  447.  
  448. Flashsocket.prototype.type = 'flashsocket';
  449.  
  450. Flashsocket.prototype.connect = function(){
  451. var self = this, args = arguments;
  452. WebSocket.__addTask(function(){
  453. io.Transport.websocket.prototype.connect.apply(self, args);
  454. });
  455. return this;
  456. };
  457.  
  458. Flashsocket.prototype.send = function(){
  459. var self = this, args = arguments;
  460. WebSocket.__addTask(function(){
  461. io.Transport.websocket.proto
Add Comment
Please, Sign In to add comment