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

/libs/engine.io-client/1.4.3/engine.io.js

https://gitlab.com/mba811/static
JavaScript | 3872 lines | 2169 code | 672 blank | 1031 comment | 551 complexity | 37bbbe99db35ea7420f80666147d49cf MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.eio=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. module.exports = _dereq_('./lib/');
  3. },{"./lib/":2}],2:[function(_dereq_,module,exports){
  4. module.exports = _dereq_('./socket');
  5. /**
  6. * Exports parser
  7. *
  8. * @api public
  9. *
  10. */
  11. module.exports.parser = _dereq_('engine.io-parser');
  12. },{"./socket":3,"engine.io-parser":17}],3:[function(_dereq_,module,exports){
  13. (function (global){
  14. /**
  15. * Module dependencies.
  16. */
  17. var transports = _dereq_('./transports');
  18. var Emitter = _dereq_('component-emitter');
  19. var debug = _dereq_('debug')('engine.io-client:socket');
  20. var index = _dereq_('indexof');
  21. var parser = _dereq_('engine.io-parser');
  22. var parseuri = _dereq_('parseuri');
  23. var parsejson = _dereq_('parsejson');
  24. var parseqs = _dereq_('parseqs');
  25. /**
  26. * Module exports.
  27. */
  28. module.exports = Socket;
  29. /**
  30. * Noop function.
  31. *
  32. * @api private
  33. */
  34. function noop(){}
  35. /**
  36. * Socket constructor.
  37. *
  38. * @param {String|Object} uri or options
  39. * @param {Object} options
  40. * @api public
  41. */
  42. function Socket(uri, opts){
  43. if (!(this instanceof Socket)) return new Socket(uri, opts);
  44. opts = opts || {};
  45. if (uri && 'object' == typeof uri) {
  46. opts = uri;
  47. uri = null;
  48. }
  49. if (uri) {
  50. uri = parseuri(uri);
  51. opts.host = uri.host;
  52. opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
  53. opts.port = uri.port;
  54. if (uri.query) opts.query = uri.query;
  55. }
  56. this.secure = null != opts.secure ? opts.secure :
  57. (global.location && 'https:' == location.protocol);
  58. if (opts.host) {
  59. var pieces = opts.host.split(':');
  60. opts.hostname = pieces.shift();
  61. if (pieces.length) opts.port = pieces.pop();
  62. }
  63. this.agent = opts.agent || false;
  64. this.hostname = opts.hostname ||
  65. (global.location ? location.hostname : 'localhost');
  66. this.port = opts.port || (global.location && location.port ?
  67. location.port :
  68. (this.secure ? 443 : 80));
  69. this.query = opts.query || {};
  70. if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
  71. this.upgrade = false !== opts.upgrade;
  72. this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  73. this.forceJSONP = !!opts.forceJSONP;
  74. this.jsonp = false !== opts.jsonp;
  75. this.forceBase64 = !!opts.forceBase64;
  76. this.enablesXDR = !!opts.enablesXDR;
  77. this.timestampParam = opts.timestampParam || 't';
  78. this.timestampRequests = opts.timestampRequests;
  79. this.transports = opts.transports || ['polling', 'websocket'];
  80. this.readyState = '';
  81. this.writeBuffer = [];
  82. this.callbackBuffer = [];
  83. this.policyPort = opts.policyPort || 843;
  84. this.rememberUpgrade = opts.rememberUpgrade || false;
  85. this.open();
  86. this.binaryType = null;
  87. this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
  88. }
  89. Socket.priorWebsocketSuccess = false;
  90. /**
  91. * Mix in `Emitter`.
  92. */
  93. Emitter(Socket.prototype);
  94. /**
  95. * Protocol version.
  96. *
  97. * @api public
  98. */
  99. Socket.protocol = parser.protocol; // this is an int
  100. /**
  101. * Expose deps for legacy compatibility
  102. * and standalone browser access.
  103. */
  104. Socket.Socket = Socket;
  105. Socket.Transport = _dereq_('./transport');
  106. Socket.transports = _dereq_('./transports');
  107. Socket.parser = _dereq_('engine.io-parser');
  108. /**
  109. * Creates transport of the given type.
  110. *
  111. * @param {String} transport name
  112. * @return {Transport}
  113. * @api private
  114. */
  115. Socket.prototype.createTransport = function (name) {
  116. debug('creating transport "%s"', name);
  117. var query = clone(this.query);
  118. // append engine.io protocol identifier
  119. query.EIO = parser.protocol;
  120. // transport name
  121. query.transport = name;
  122. // session id if we already have one
  123. if (this.id) query.sid = this.id;
  124. var transport = new transports[name]({
  125. agent: this.agent,
  126. hostname: this.hostname,
  127. port: this.port,
  128. secure: this.secure,
  129. path: this.path,
  130. query: query,
  131. forceJSONP: this.forceJSONP,
  132. jsonp: this.jsonp,
  133. forceBase64: this.forceBase64,
  134. enablesXDR: this.enablesXDR,
  135. timestampRequests: this.timestampRequests,
  136. timestampParam: this.timestampParam,
  137. policyPort: this.policyPort,
  138. socket: this
  139. });
  140. return transport;
  141. };
  142. function clone (obj) {
  143. var o = {};
  144. for (var i in obj) {
  145. if (obj.hasOwnProperty(i)) {
  146. o[i] = obj[i];
  147. }
  148. }
  149. return o;
  150. }
  151. /**
  152. * Initializes transport to use and starts probe.
  153. *
  154. * @api private
  155. */
  156. Socket.prototype.open = function () {
  157. var transport;
  158. if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) {
  159. transport = 'websocket';
  160. } else if (0 == this.transports.length) {
  161. // Emit error on next tick so it can be listened to
  162. var self = this;
  163. setTimeout(function() {
  164. self.emit('error', 'No transports available');
  165. }, 0);
  166. return;
  167. } else {
  168. transport = this.transports[0];
  169. }
  170. this.readyState = 'opening';
  171. // Retry with the next transport if the transport is disabled (jsonp: false)
  172. var transport;
  173. try {
  174. transport = this.createTransport(transport);
  175. } catch (e) {
  176. this.transports.shift();
  177. this.open();
  178. return;
  179. }
  180. transport.open();
  181. this.setTransport(transport);
  182. };
  183. /**
  184. * Sets the current transport. Disables the existing one (if any).
  185. *
  186. * @api private
  187. */
  188. Socket.prototype.setTransport = function(transport){
  189. debug('setting transport %s', transport.name);
  190. var self = this;
  191. if (this.transport) {
  192. debug('clearing existing transport %s', this.transport.name);
  193. this.transport.removeAllListeners();
  194. }
  195. // set up transport
  196. this.transport = transport;
  197. // set up transport listeners
  198. transport
  199. .on('drain', function(){
  200. self.onDrain();
  201. })
  202. .on('packet', function(packet){
  203. self.onPacket(packet);
  204. })
  205. .on('error', function(e){
  206. self.onError(e);
  207. })
  208. .on('close', function(){
  209. self.onClose('transport close');
  210. });
  211. };
  212. /**
  213. * Probes a transport.
  214. *
  215. * @param {String} transport name
  216. * @api private
  217. */
  218. Socket.prototype.probe = function (name) {
  219. debug('probing transport "%s"', name);
  220. var transport = this.createTransport(name, { probe: 1 })
  221. , failed = false
  222. , self = this;
  223. Socket.priorWebsocketSuccess = false;
  224. function onTransportOpen(){
  225. if (self.onlyBinaryUpgrades) {
  226. var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary;
  227. failed = failed || upgradeLosesBinary;
  228. }
  229. if (failed) return;
  230. debug('probe transport "%s" opened', name);
  231. transport.send([{ type: 'ping', data: 'probe' }]);
  232. transport.once('packet', function (msg) {
  233. if (failed) return;
  234. if ('pong' == msg.type && 'probe' == msg.data) {
  235. debug('probe transport "%s" pong', name);
  236. self.upgrading = true;
  237. self.emit('upgrading', transport);
  238. if (!transport) return;
  239. Socket.priorWebsocketSuccess = 'websocket' == transport.name;
  240. debug('pausing current transport "%s"', self.transport.name);
  241. self.transport.pause(function () {
  242. if (failed) return;
  243. if ('closed' == self.readyState) return;
  244. debug('changing transport and sending upgrade packet');
  245. cleanup();
  246. self.setTransport(transport);
  247. transport.send([{ type: 'upgrade' }]);
  248. self.emit('upgrade', transport);
  249. transport = null;
  250. self.upgrading = false;
  251. self.flush();
  252. });
  253. } else {
  254. debug('probe transport "%s" failed', name);
  255. var err = new Error('probe error');
  256. err.transport = transport.name;
  257. self.emit('upgradeError', err);
  258. }
  259. });
  260. }
  261. function freezeTransport() {
  262. if (failed) return;
  263. // Any callback called by transport should be ignored since now
  264. failed = true;
  265. cleanup();
  266. transport.close();
  267. transport = null;
  268. }
  269. //Handle any error that happens while probing
  270. function onerror(err) {
  271. var error = new Error('probe error: ' + err);
  272. error.transport = transport.name;
  273. freezeTransport();
  274. debug('probe transport "%s" failed because of error: %s', name, err);
  275. self.emit('upgradeError', error);
  276. }
  277. function onTransportClose(){
  278. onerror("transport closed");
  279. }
  280. //When the socket is closed while we're probing
  281. function onclose(){
  282. onerror("socket closed");
  283. }
  284. //When the socket is upgraded while we're probing
  285. function onupgrade(to){
  286. if (transport && to.name != transport.name) {
  287. debug('"%s" works - aborting "%s"', to.name, transport.name);
  288. freezeTransport();
  289. }
  290. }
  291. //Remove all listeners on the transport and on self
  292. function cleanup(){
  293. transport.removeListener('open', onTransportOpen);
  294. transport.removeListener('error', onerror);
  295. transport.removeListener('close', onTransportClose);
  296. self.removeListener('close', onclose);
  297. self.removeListener('upgrading', onupgrade);
  298. }
  299. transport.once('open', onTransportOpen);
  300. transport.once('error', onerror);
  301. transport.once('close', onTransportClose);
  302. this.once('close', onclose);
  303. this.once('upgrading', onupgrade);
  304. transport.open();
  305. };
  306. /**
  307. * Called when connection is deemed open.
  308. *
  309. * @api public
  310. */
  311. Socket.prototype.onOpen = function () {
  312. debug('socket open');
  313. this.readyState = 'open';
  314. Socket.priorWebsocketSuccess = 'websocket' == this.transport.name;
  315. this.emit('open');
  316. this.flush();
  317. // we check for `readyState` in case an `open`
  318. // listener already closed the socket
  319. if ('open' == this.readyState && this.upgrade && this.transport.pause) {
  320. debug('starting upgrade probes');
  321. for (var i = 0, l = this.upgrades.length; i < l; i++) {
  322. this.probe(this.upgrades[i]);
  323. }
  324. }
  325. };
  326. /**
  327. * Handles a packet.
  328. *
  329. * @api private
  330. */
  331. Socket.prototype.onPacket = function (packet) {
  332. if ('opening' == this.readyState || 'open' == this.readyState) {
  333. debug('socket receive: type "%s", data "%s"', packet.type, packet.data);
  334. this.emit('packet', packet);
  335. // Socket is live - any packet counts
  336. this.emit('heartbeat');
  337. switch (packet.type) {
  338. case 'open':
  339. this.onHandshake(parsejson(packet.data));
  340. break;
  341. case 'pong':
  342. this.setPing();
  343. break;
  344. case 'error':
  345. var err = new Error('server error');
  346. err.code = packet.data;
  347. this.emit('error', err);
  348. break;
  349. case 'message':
  350. this.emit('data', packet.data);
  351. this.emit('message', packet.data);
  352. break;
  353. }
  354. } else {
  355. debug('packet received with socket readyState "%s"', this.readyState);
  356. }
  357. };
  358. /**
  359. * Called upon handshake completion.
  360. *
  361. * @param {Object} handshake obj
  362. * @api private
  363. */
  364. Socket.prototype.onHandshake = function (data) {
  365. this.emit('handshake', data);
  366. this.id = data.sid;
  367. this.transport.query.sid = data.sid;
  368. this.upgrades = this.filterUpgrades(data.upgrades);
  369. this.pingInterval = data.pingInterval;
  370. this.pingTimeout = data.pingTimeout;
  371. this.onOpen();
  372. // In case open handler closes socket
  373. if ('closed' == this.readyState) return;
  374. this.setPing();
  375. // Prolong liveness of socket on heartbeat
  376. this.removeListener('heartbeat', this.onHeartbeat);
  377. this.on('heartbeat', this.onHeartbeat);
  378. };
  379. /**
  380. * Resets ping timeout.
  381. *
  382. * @api private
  383. */
  384. Socket.prototype.onHeartbeat = function (timeout) {
  385. clearTimeout(this.pingTimeoutTimer);
  386. var self = this;
  387. self.pingTimeoutTimer = setTimeout(function () {
  388. if ('closed' == self.readyState) return;
  389. self.onClose('ping timeout');
  390. }, timeout || (self.pingInterval + self.pingTimeout));
  391. };
  392. /**
  393. * Pings server every `this.pingInterval` and expects response
  394. * within `this.pingTimeout` or closes connection.
  395. *
  396. * @api private
  397. */
  398. Socket.prototype.setPing = function () {
  399. var self = this;
  400. clearTimeout(self.pingIntervalTimer);
  401. self.pingIntervalTimer = setTimeout(function () {
  402. debug('writing ping packet - expecting pong within %sms', self.pingTimeout);
  403. self.ping();
  404. self.onHeartbeat(self.pingTimeout);
  405. }, self.pingInterval);
  406. };
  407. /**
  408. * Sends a ping packet.
  409. *
  410. * @api public
  411. */
  412. Socket.prototype.ping = function () {
  413. this.sendPacket('ping');
  414. };
  415. /**
  416. * Called on `drain` event
  417. *
  418. * @api private
  419. */
  420. Socket.prototype.onDrain = function() {
  421. for (var i = 0; i < this.prevBufferLen; i++) {
  422. if (this.callbackBuffer[i]) {
  423. this.callbackBuffer[i]();
  424. }
  425. }
  426. this.writeBuffer.splice(0, this.prevBufferLen);
  427. this.callbackBuffer.splice(0, this.prevBufferLen);
  428. // setting prevBufferLen = 0 is very important
  429. // for example, when upgrading, upgrade packet is sent over,
  430. // and a nonzero prevBufferLen could cause problems on `drain`
  431. this.prevBufferLen = 0;
  432. if (this.writeBuffer.length == 0) {
  433. this.emit('drain');
  434. } else {
  435. this.flush();
  436. }
  437. };
  438. /**
  439. * Flush write buffers.
  440. *
  441. * @api private
  442. */
  443. Socket.prototype.flush = function () {
  444. if ('closed' != this.readyState && this.transport.writable &&
  445. !this.upgrading && this.writeBuffer.length) {
  446. debug('flushing %d packets in socket', this.writeBuffer.length);
  447. this.transport.send(this.writeBuffer);
  448. // keep track of current length of writeBuffer
  449. // splice writeBuffer and callbackBuffer on `drain`
  450. this.prevBufferLen = this.writeBuffer.length;
  451. this.emit('flush');
  452. }
  453. };
  454. /**
  455. * Sends a message.
  456. *
  457. * @param {String} message.
  458. * @param {Function} callback function.
  459. * @return {Socket} for chaining.
  460. * @api public
  461. */
  462. Socket.prototype.write =
  463. Socket.prototype.send = function (msg, fn) {
  464. this.sendPacket('message', msg, fn);
  465. return this;
  466. };
  467. /**
  468. * Sends a packet.
  469. *
  470. * @param {String} packet type.
  471. * @param {String} data.
  472. * @param {Function} callback function.
  473. * @api private
  474. */
  475. Socket.prototype.sendPacket = function (type, data, fn) {
  476. if ('closing' == this.readyState || 'closed' == this.readyState) {
  477. return;
  478. }
  479. var packet = { type: type, data: data };
  480. this.emit('packetCreate', packet);
  481. this.writeBuffer.push(packet);
  482. this.callbackBuffer.push(fn);
  483. this.flush();
  484. };
  485. /**
  486. * Closes the connection.
  487. *
  488. * @api private
  489. */
  490. Socket.prototype.close = function () {
  491. if ('opening' == this.readyState || 'open' == this.readyState) {
  492. this.readyState = 'closing';
  493. var self = this;
  494. function close() {
  495. self.onClose('forced close');
  496. debug('socket closing - telling transport to close');
  497. self.transport.close();
  498. }
  499. function cleanupAndClose() {
  500. self.removeListener('upgrade', cleanupAndClose);
  501. self.removeListener('upgradeError', cleanupAndClose);
  502. close();
  503. }
  504. function waitForUpgrade() {
  505. // wait for upgrade to finish since we can't send packets while pausing a transport
  506. self.once('upgrade', cleanupAndClose);
  507. self.once('upgradeError', cleanupAndClose);
  508. }
  509. if (this.writeBuffer.length) {
  510. this.once('drain', function() {
  511. if (this.upgrading) {
  512. waitForUpgrade();
  513. } else {
  514. close();
  515. }
  516. });
  517. } else if (this.upgrading) {
  518. waitForUpgrade();
  519. } else {
  520. close();
  521. }
  522. }
  523. return this;
  524. };
  525. /**
  526. * Called upon transport error
  527. *
  528. * @api private
  529. */
  530. Socket.prototype.onError = function (err) {
  531. debug('socket error %j', err);
  532. Socket.priorWebsocketSuccess = false;
  533. this.emit('error', err);
  534. this.onClose('transport error', err);
  535. };
  536. /**
  537. * Called upon transport close.
  538. *
  539. * @api private
  540. */
  541. Socket.prototype.onClose = function (reason, desc) {
  542. if ('opening' == this.readyState || 'open' == this.readyState || 'closing' == this.readyState) {
  543. debug('socket close with reason: "%s"', reason);
  544. var self = this;
  545. // clear timers
  546. clearTimeout(this.pingIntervalTimer);
  547. clearTimeout(this.pingTimeoutTimer);
  548. // clean buffers in next tick, so developers can still
  549. // grab the buffers on `close` event
  550. setTimeout(function() {
  551. self.writeBuffer = [];
  552. self.callbackBuffer = [];
  553. self.prevBufferLen = 0;
  554. }, 0);
  555. // stop event from firing again for transport
  556. this.transport.removeAllListeners('close');
  557. // ensure transport won't stay open
  558. this.transport.close();
  559. // ignore further transport communication
  560. this.transport.removeAllListeners();
  561. // set ready state
  562. this.readyState = 'closed';
  563. // clear session id
  564. this.id = null;
  565. // emit close event
  566. this.emit('close', reason, desc);
  567. }
  568. };
  569. /**
  570. * Filters upgrades, returning only those matching client transports.
  571. *
  572. * @param {Array} server upgrades
  573. * @api private
  574. *
  575. */
  576. Socket.prototype.filterUpgrades = function (upgrades) {
  577. var filteredUpgrades = [];
  578. for (var i = 0, j = upgrades.length; i<j; i++) {
  579. if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]);
  580. }
  581. return filteredUpgrades;
  582. };
  583. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  584. },{"./transport":4,"./transports":5,"component-emitter":12,"debug":14,"engine.io-parser":17,"indexof":25,"parsejson":26,"parseqs":27,"parseuri":28}],4:[function(_dereq_,module,exports){
  585. /**
  586. * Module dependencies.
  587. */
  588. var parser = _dereq_('engine.io-parser');
  589. var Emitter = _dereq_('component-emitter');
  590. /**
  591. * Module exports.
  592. */
  593. module.exports = Transport;
  594. /**
  595. * Transport abstract constructor.
  596. *
  597. * @param {Object} options.
  598. * @api private
  599. */
  600. function Transport (opts) {
  601. this.path = opts.path;
  602. this.hostname = opts.hostname;
  603. this.port = opts.port;
  604. this.secure = opts.secure;
  605. this.query = opts.query;
  606. this.timestampParam = opts.timestampParam;
  607. this.timestampRequests = opts.timestampRequests;
  608. this.readyState = '';
  609. this.agent = opts.agent || false;
  610. this.socket = opts.socket;
  611. this.enablesXDR = opts.enablesXDR;
  612. }
  613. /**
  614. * Mix in `Emitter`.
  615. */
  616. Emitter(Transport.prototype);
  617. /**
  618. * A counter used to prevent collisions in the timestamps used
  619. * for cache busting.
  620. */
  621. Transport.timestamps = 0;
  622. /**
  623. * Emits an error.
  624. *
  625. * @param {String} str
  626. * @return {Transport} for chaining
  627. * @api public
  628. */
  629. Transport.prototype.onError = function (msg, desc) {
  630. var err = new Error(msg);
  631. err.type = 'TransportError';
  632. err.description = desc;
  633. this.emit('error', err);
  634. return this;
  635. };
  636. /**
  637. * Opens the transport.
  638. *
  639. * @api public
  640. */
  641. Transport.prototype.open = function () {
  642. if ('closed' == this.readyState || '' == this.readyState) {
  643. this.readyState = 'opening';
  644. this.doOpen();
  645. }
  646. return this;
  647. };
  648. /**
  649. * Closes the transport.
  650. *
  651. * @api private
  652. */
  653. Transport.prototype.close = function () {
  654. if ('opening' == this.readyState || 'open' == this.readyState) {
  655. this.doClose();
  656. this.onClose();
  657. }
  658. return this;
  659. };
  660. /**
  661. * Sends multiple packets.
  662. *
  663. * @param {Array} packets
  664. * @api private
  665. */
  666. Transport.prototype.send = function(packets){
  667. if ('open' == this.readyState) {
  668. this.write(packets);
  669. } else {
  670. throw new Error('Transport not open');
  671. }
  672. };
  673. /**
  674. * Called upon open
  675. *
  676. * @api private
  677. */
  678. Transport.prototype.onOpen = function () {
  679. this.readyState = 'open';
  680. this.writable = true;
  681. this.emit('open');
  682. };
  683. /**
  684. * Called with data.
  685. *
  686. * @param {String} data
  687. * @api private
  688. */
  689. Transport.prototype.onData = function(data){
  690. var packet = parser.decodePacket(data, this.socket.binaryType);
  691. this.onPacket(packet);
  692. };
  693. /**
  694. * Called with a decoded packet.
  695. */
  696. Transport.prototype.onPacket = function (packet) {
  697. this.emit('packet', packet);
  698. };
  699. /**
  700. * Called upon close.
  701. *
  702. * @api private
  703. */
  704. Transport.prototype.onClose = function () {
  705. this.readyState = 'closed';
  706. this.emit('close');
  707. };
  708. },{"component-emitter":12,"engine.io-parser":17}],5:[function(_dereq_,module,exports){
  709. (function (global){
  710. /**
  711. * Module dependencies
  712. */
  713. var XMLHttpRequest = _dereq_('xmlhttprequest');
  714. var XHR = _dereq_('./polling-xhr');
  715. var JSONP = _dereq_('./polling-jsonp');
  716. var websocket = _dereq_('./websocket');
  717. /**
  718. * Export transports.
  719. */
  720. exports.polling = polling;
  721. exports.websocket = websocket;
  722. /**
  723. * Polling transport polymorphic constructor.
  724. * Decides on xhr vs jsonp based on feature detection.
  725. *
  726. * @api private
  727. */
  728. function polling(opts){
  729. var xhr;
  730. var xd = false;
  731. var xs = false;
  732. var jsonp = false !== opts.jsonp;
  733. if (global.location) {
  734. var isSSL = 'https:' == location.protocol;
  735. var port = location.port;
  736. // some user agents have empty `location.port`
  737. if (!port) {
  738. port = isSSL ? 443 : 80;
  739. }
  740. xd = opts.hostname != location.hostname || port != opts.port;
  741. xs = opts.secure != isSSL;
  742. }
  743. opts.xdomain = xd;
  744. opts.xscheme = xs;
  745. xhr = new XMLHttpRequest(opts);
  746. if ('open' in xhr && !opts.forceJSONP) {
  747. return new XHR(opts);
  748. } else {
  749. if (!jsonp) throw new Error('JSONP disabled');
  750. return new JSONP(opts);
  751. }
  752. }
  753. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  754. },{"./polling-jsonp":6,"./polling-xhr":7,"./websocket":9,"xmlhttprequest":10}],6:[function(_dereq_,module,exports){
  755. (function (global){
  756. /**
  757. * Module requirements.
  758. */
  759. var Polling = _dereq_('./polling');
  760. var inherit = _dereq_('component-inherit');
  761. /**
  762. * Module exports.
  763. */
  764. module.exports = JSONPPolling;
  765. /**
  766. * Cached regular expressions.
  767. */
  768. var rNewline = /\n/g;
  769. var rEscapedNewline = /\\n/g;
  770. /**
  771. * Global JSONP callbacks.
  772. */
  773. var callbacks;
  774. /**
  775. * Callbacks count.
  776. */
  777. var index = 0;
  778. /**
  779. * Noop.
  780. */
  781. function empty () { }
  782. /**
  783. * JSONP Polling constructor.
  784. *
  785. * @param {Object} opts.
  786. * @api public
  787. */
  788. function JSONPPolling (opts) {
  789. Polling.call(this, opts);
  790. this.query = this.query || {};
  791. // define global callbacks array if not present
  792. // we do this here (lazily) to avoid unneeded global pollution
  793. if (!callbacks) {
  794. // we need to consider multiple engines in the same page
  795. if (!global.___eio) global.___eio = [];
  796. callbacks = global.___eio;
  797. }
  798. // callback identifier
  799. this.index = callbacks.length;
  800. // add callback to jsonp global
  801. var self = this;
  802. callbacks.push(function (msg) {
  803. self.onData(msg);
  804. });
  805. // append to query string
  806. this.query.j = this.index;
  807. // prevent spurious errors from being emitted when the window is unloaded
  808. if (global.document && global.addEventListener) {
  809. global.addEventListener('beforeunload', function () {
  810. if (self.script) self.script.onerror = empty;
  811. }, false);
  812. }
  813. }
  814. /**
  815. * Inherits from Polling.
  816. */
  817. inherit(JSONPPolling, Polling);
  818. /*
  819. * JSONP only supports binary as base64 encoded strings
  820. */
  821. JSONPPolling.prototype.supportsBinary = false;
  822. /**
  823. * Closes the socket.
  824. *
  825. * @api private
  826. */
  827. JSONPPolling.prototype.doClose = function () {
  828. if (this.script) {
  829. this.script.parentNode.removeChild(this.script);
  830. this.script = null;
  831. }
  832. if (this.form) {
  833. this.form.parentNode.removeChild(this.form);
  834. this.form = null;
  835. this.iframe = null;
  836. }
  837. Polling.prototype.doClose.call(this);
  838. };
  839. /**
  840. * Starts a poll cycle.
  841. *
  842. * @api private
  843. */
  844. JSONPPolling.prototype.doPoll = function () {
  845. var self = this;
  846. var script = document.createElement('script');
  847. if (this.script) {
  848. this.script.parentNode.removeChild(this.script);
  849. this.script = null;
  850. }
  851. script.async = true;
  852. script.src = this.uri();
  853. script.onerror = function(e){
  854. self.onError('jsonp poll error',e);
  855. };
  856. var insertAt = document.getElementsByTagName('script')[0];
  857. insertAt.parentNode.insertBefore(script, insertAt);
  858. this.script = script;
  859. var isUAgecko = 'undefined' != typeof navigator && /gecko/i.test(navigator.userAgent);
  860. if (isUAgecko) {
  861. setTimeout(function () {
  862. var iframe = document.createElement('iframe');
  863. document.body.appendChild(iframe);
  864. document.body.removeChild(iframe);
  865. }, 100);
  866. }
  867. };
  868. /**
  869. * Writes with a hidden iframe.
  870. *
  871. * @param {String} data to send
  872. * @param {Function} called upon flush.
  873. * @api private
  874. */
  875. JSONPPolling.prototype.doWrite = function (data, fn) {
  876. var self = this;
  877. if (!this.form) {
  878. var form = document.createElement('form');
  879. var area = document.createElement('textarea');
  880. var id = this.iframeId = 'eio_iframe_' + this.index;
  881. var iframe;
  882. form.className = 'socketio';
  883. form.style.position = 'absolute';
  884. form.style.top = '-1000px';
  885. form.style.left = '-1000px';
  886. form.target = id;
  887. form.method = 'POST';
  888. form.setAttribute('accept-charset', 'utf-8');
  889. area.name = 'd';
  890. form.appendChild(area);
  891. document.body.appendChild(form);
  892. this.form = form;
  893. this.area = area;
  894. }
  895. this.form.action = this.uri();
  896. function complete () {
  897. initIframe();
  898. fn();
  899. }
  900. function initIframe () {
  901. if (self.iframe) {
  902. try {
  903. self.form.removeChild(self.iframe);
  904. } catch (e) {
  905. self.onError('jsonp polling iframe removal error', e);
  906. }
  907. }
  908. try {
  909. // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
  910. var html = '<iframe src="javascript:0" name="'+ self.iframeId +'">';
  911. iframe = document.createElement(html);
  912. } catch (e) {
  913. iframe = document.createElement('iframe');
  914. iframe.name = self.iframeId;
  915. iframe.src = 'javascript:0';
  916. }
  917. iframe.id = self.iframeId;
  918. self.form.appendChild(iframe);
  919. self.iframe = iframe;
  920. }
  921. initIframe();
  922. // escape \n to prevent it from being converted into \r\n by some UAs
  923. // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
  924. data = data.replace(rEscapedNewline, '\\\n');
  925. this.area.value = data.replace(rNewline, '\\n');
  926. try {
  927. this.form.submit();
  928. } catch(e) {}
  929. if (this.iframe.attachEvent) {
  930. this.iframe.onreadystatechange = function(){
  931. if (self.iframe.readyState == 'complete') {
  932. complete();
  933. }
  934. };
  935. } else {
  936. this.iframe.onload = complete;
  937. }
  938. };
  939. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  940. },{"./polling":8,"component-inherit":13}],7:[function(_dereq_,module,exports){
  941. (function (global){
  942. /**
  943. * Module requirements.
  944. */
  945. var XMLHttpRequest = _dereq_('xmlhttprequest');
  946. var Polling = _dereq_('./polling');
  947. var Emitter = _dereq_('component-emitter');
  948. var inherit = _dereq_('component-inherit');
  949. var debug = _dereq_('debug')('engine.io-client:polling-xhr');
  950. /**
  951. * Module exports.
  952. */
  953. module.exports = XHR;
  954. module.exports.Request = Request;
  955. /**
  956. * Empty function
  957. */
  958. function empty(){}
  959. /**
  960. * XHR Polling constructor.
  961. *
  962. * @param {Object} opts
  963. * @api public
  964. */
  965. function XHR(opts){
  966. Polling.call(this, opts);
  967. if (global.location) {
  968. var isSSL = 'https:' == location.protocol;
  969. var port = location.port;
  970. // some user agents have empty `location.port`
  971. if (!port) {
  972. port = isSSL ? 443 : 80;
  973. }
  974. this.xd = opts.hostname != global.location.hostname ||
  975. port != opts.port;
  976. this.xs = opts.secure != isSSL;
  977. }
  978. }
  979. /**
  980. * Inherits from Polling.
  981. */
  982. inherit(XHR, Polling);
  983. /**
  984. * XHR supports binary
  985. */
  986. XHR.prototype.supportsBinary = true;
  987. /**
  988. * Creates a request.
  989. *
  990. * @param {String} method
  991. * @api private
  992. */
  993. XHR.prototype.request = function(opts){
  994. opts = opts || {};
  995. opts.uri = this.uri();
  996. opts.xd = this.xd;
  997. opts.xs = this.xs;
  998. opts.agent = this.agent || false;
  999. opts.supportsBinary = this.supportsBinary;
  1000. opts.enablesXDR = this.enablesXDR;
  1001. return new Request(opts);
  1002. };
  1003. /**
  1004. * Sends data.
  1005. *
  1006. * @param {String} data to send.
  1007. * @param {Function} called upon flush.
  1008. * @api private
  1009. */
  1010. XHR.prototype.doWrite = function(data, fn){
  1011. var isBinary = typeof data !== 'string' && data !== undefined;
  1012. var req = this.request({ method: 'POST', data: data, isBinary: isBinary });
  1013. var self = this;
  1014. req.on('success', fn);
  1015. req.on('error', function(err){
  1016. self.onError('xhr post error', err);
  1017. });
  1018. this.sendXhr = req;
  1019. };
  1020. /**
  1021. * Starts a poll cycle.
  1022. *
  1023. * @api private
  1024. */
  1025. XHR.prototype.doPoll = function(){
  1026. debug('xhr poll');
  1027. var req = this.request();
  1028. var self = this;
  1029. req.on('data', function(data){
  1030. self.onData(data);
  1031. });
  1032. req.on('error', function(err){
  1033. self.onError('xhr poll error', err);
  1034. });
  1035. this.pollXhr = req;
  1036. };
  1037. /**
  1038. * Request constructor
  1039. *
  1040. * @param {Object} options
  1041. * @api public
  1042. */
  1043. function Request(opts){
  1044. this.method = opts.method || 'GET';
  1045. this.uri = opts.uri;
  1046. this.xd = !!opts.xd;
  1047. this.xs = !!opts.xs;
  1048. this.async = false !== opts.async;
  1049. this.data = undefined != opts.data ? opts.data : null;
  1050. this.agent = opts.agent;
  1051. this.isBinary = opts.isBinary;
  1052. this.supportsBinary = opts.supportsBinary;
  1053. this.enablesXDR = opts.enablesXDR;
  1054. this.create();
  1055. }
  1056. /**
  1057. * Mix in `Emitter`.
  1058. */
  1059. Emitter(Request.prototype);
  1060. /**
  1061. * Creates the XHR object and sends the request.
  1062. *
  1063. * @api private
  1064. */
  1065. Request.prototype.create = function(){
  1066. var xhr = this.xhr = new XMLHttpRequest({ agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR });
  1067. var self = this;
  1068. try {
  1069. debug('xhr open %s: %s', this.method, this.uri);
  1070. xhr.open(this.method, this.uri, this.async);
  1071. if (this.supportsBinary) {
  1072. // This has to be done after open because Firefox is stupid
  1073. // http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension
  1074. xhr.responseType = 'arraybuffer';
  1075. }
  1076. if ('POST' == this.method) {
  1077. try {
  1078. if (this.isBinary) {
  1079. xhr.setRequestHeader('Content-type', 'application/octet-stream');
  1080. } else {
  1081. xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
  1082. }
  1083. } catch (e) {}
  1084. }
  1085. // ie6 check
  1086. if ('withCredentials' in xhr) {
  1087. xhr.withCredentials = true;
  1088. }
  1089. if (this.hasXDR()) {
  1090. xhr.onload = function(){
  1091. self.onLoad();
  1092. };
  1093. xhr.onerror = function(){
  1094. self.onError(xhr.responseText);
  1095. };
  1096. } else {
  1097. xhr.onreadystatechange = function(){
  1098. if (4 != xhr.readyState) return;
  1099. if (200 == xhr.status || 1223 == xhr.status) {
  1100. self.onLoad();
  1101. } else {
  1102. // make sure the `error` event handler that's user-set
  1103. // does not throw in the same tick and gets caught here
  1104. setTimeout(function(){
  1105. self.onError(xhr.status);
  1106. }, 0);
  1107. }
  1108. };
  1109. }
  1110. debug('xhr data %s', this.data);
  1111. xhr.send(this.data);
  1112. } catch (e) {
  1113. // Need to defer since .create() is called directly fhrom the constructor
  1114. // and thus the 'error' event can only be only bound *after* this exception
  1115. // occurs. Therefore, also, we cannot throw here at all.
  1116. setTimeout(function() {
  1117. self.onError(e);
  1118. }, 0);
  1119. return;
  1120. }
  1121. if (global.document) {
  1122. this.index = Request.requestsCount++;
  1123. Request.requests[this.index] = this;
  1124. }
  1125. };
  1126. /**
  1127. * Called upon successful response.
  1128. *
  1129. * @api private
  1130. */
  1131. Request.prototype.onSuccess = function(){
  1132. this.emit('success');
  1133. this.cleanup();
  1134. };
  1135. /**
  1136. * Called if we have data.
  1137. *
  1138. * @api private
  1139. */
  1140. Request.prototype.onData = function(data){
  1141. this.emit('data', data);
  1142. this.onSuccess();
  1143. };
  1144. /**
  1145. * Called upon error.
  1146. *
  1147. * @api private
  1148. */
  1149. Request.prototype.onError = function(err){
  1150. this.emit('error', err);
  1151. this.cleanup();
  1152. };
  1153. /**
  1154. * Cleans up house.
  1155. *
  1156. * @api private
  1157. */
  1158. Request.prototype.cleanup = function(){
  1159. if ('undefined' == typeof this.xhr || null === this.xhr) {
  1160. return;
  1161. }
  1162. // xmlhttprequest
  1163. if (this.hasXDR()) {
  1164. this.xhr.onload = this.xhr.onerror = empty;
  1165. } else {
  1166. this.xhr.onreadystatechange = empty;
  1167. }
  1168. try {
  1169. this.xhr.abort();
  1170. } catch(e) {}
  1171. if (global.document) {
  1172. delete Request.requests[this.index];
  1173. }
  1174. this.xhr = null;
  1175. };
  1176. /**
  1177. * Called upon load.
  1178. *
  1179. * @api private
  1180. */
  1181. Request.prototype.onLoad = function(){
  1182. var data;
  1183. try {
  1184. var contentType;
  1185. try {
  1186. contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0];
  1187. } catch (e) {}
  1188. if (contentType === 'application/octet-stream') {
  1189. data = this.xhr.response;
  1190. } else {
  1191. if (!this.supportsBinary) {
  1192. data = this.xhr.responseText;
  1193. } else {
  1194. data = 'ok';
  1195. }
  1196. }
  1197. } catch (e) {
  1198. this.onError(e);
  1199. }
  1200. if (null != data) {
  1201. this.onData(data);
  1202. }
  1203. };
  1204. /**
  1205. * Check if it has XDomainRequest.
  1206. *
  1207. * @api private
  1208. */
  1209. Request.prototype.hasXDR = function(){
  1210. return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR;
  1211. };
  1212. /**
  1213. * Aborts the request.
  1214. *
  1215. * @api public
  1216. */
  1217. Request.prototype.abort = function(){
  1218. this.cleanup();
  1219. };
  1220. /**
  1221. * Aborts pending requests when unloading the window. This is needed to prevent
  1222. * memory leaks (e.g. when using IE) and to ensure that no spurious error is
  1223. * emitted.
  1224. */
  1225. if (global.document) {
  1226. Request.requestsCount = 0;
  1227. Request.requests = {};
  1228. if (global.attachEvent) {
  1229. global.attachEvent('onunload', unloadHandler);
  1230. } else if (global.addEventListener) {
  1231. global.addEventListener('beforeunload', unloadHandler, false);
  1232. }
  1233. }
  1234. function unloadHandler() {
  1235. for (var i in Request.requests) {
  1236. if (Request.requests.hasOwnProperty(i)) {
  1237. Request.requests[i].abort();
  1238. }
  1239. }
  1240. }
  1241. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1242. },{"./polling":8,"component-emitter":12,"component-inherit":13,"debug":14,"xmlhttprequest":10}],8:[function(_dereq_,module,exports){
  1243. /**
  1244. * Module dependencies.
  1245. */
  1246. var Transport = _dereq_('../transport');
  1247. var parseqs = _dereq_('parseqs');
  1248. var parser = _dereq_('engine.io-parser');
  1249. var inherit = _dereq_('component-inherit');
  1250. var debug = _dereq_('debug')('engine.io-client:polling');
  1251. /**
  1252. * Module exports.
  1253. */
  1254. module.exports = Polling;
  1255. /**
  1256. * Is XHR2 supported?
  1257. */
  1258. var hasXHR2 = (function() {
  1259. var XMLHttpRequest = _dereq_('xmlhttprequest');
  1260. var xhr = new XMLHttpRequest({ xdomain: false });
  1261. return null != xhr.responseType;
  1262. })();
  1263. /**
  1264. * Polling interface.
  1265. *
  1266. * @param {Object} opts
  1267. * @api private
  1268. */
  1269. function Polling(opts){
  1270. var forceBase64 = (opts && opts.forceBase64);
  1271. if (!hasXHR2 || forceBase64) {
  1272. this.supportsBinary = false;
  1273. }
  1274. Transport.call(this, opts);
  1275. }
  1276. /**
  1277. * Inherits from Transport.
  1278. */
  1279. inherit(Polling, Transport);
  1280. /**
  1281. * Transport name.
  1282. */
  1283. Polling.prototype.name = 'polling';
  1284. /**
  1285. * Opens the socket (triggers polling). We write a PING message to determine
  1286. * when the transport is open.
  1287. *
  1288. * @api private
  1289. */
  1290. Polling.prototype.doOpen = function(){
  1291. this.poll();
  1292. };
  1293. /**
  1294. * Pauses polling.
  1295. *
  1296. * @param {Function} callback upon buffers are flushed and transport is paused
  1297. * @api private
  1298. */
  1299. Polling.prototype.pause = function(onPause){
  1300. var pending = 0;
  1301. var self = this;
  1302. this.readyState = 'pausing';
  1303. function pause(){
  1304. debug('paused');
  1305. self.readyState = 'paused';
  1306. onPause();
  1307. }
  1308. if (this.polling || !this.writable) {
  1309. var total = 0;
  1310. if (this.polling) {
  1311. debug('we are currently polling - waiting to pause');
  1312. total++;
  1313. this.once('pollComplete', function(){
  1314. debug('pre-pause polling complete');
  1315. --total || pause();
  1316. });
  1317. }
  1318. if (!this.writable) {
  1319. debug('we are currently writing - waiting to pause');
  1320. total++;
  1321. this.once('drain', function(){
  1322. debug('pre-pause writing complete');
  1323. --total || pause();
  1324. });
  1325. }
  1326. } else {
  1327. pause();
  1328. }
  1329. };
  1330. /**
  1331. * Starts polling cycle.
  1332. *
  1333. * @api public
  1334. */
  1335. Polling.prototype.poll = function(){
  1336. debug('polling');
  1337. this.polling = true;
  1338. this.doPoll();
  1339. this.emit('poll');
  1340. };
  1341. /**
  1342. * Overloads onData to detect payloads.
  1343. *
  1344. * @api private
  1345. */
  1346. Polling.prototype.onData = function(data){
  1347. var self = this;
  1348. debug('polling got data %s', data);
  1349. var callback = function(packet, index, total) {
  1350. // if its the first message we consider the transport open
  1351. if ('opening' == self.readyState) {
  1352. self.onOpen();
  1353. }
  1354. // if its a close packet, we close the ongoing requests
  1355. if ('close' == packet.type) {
  1356. self.onClose();
  1357. return false;
  1358. }
  1359. // otherwise bypass onData and handle the message
  1360. self.onPacket(packet);
  1361. };
  1362. // decode payload
  1363. parser.decodePayload(data, this.socket.binaryType, callback);
  1364. // if an event did not trigger closing
  1365. if ('closed' != this.readyState) {
  1366. // if we got data we're not polling
  1367. this.polling = false;
  1368. this.emit('pollComplete');
  1369. if ('open' == this.readyState) {
  1370. this.poll();
  1371. } else {
  1372. debug('ignoring poll - transport state "%s"', this.readyState);
  1373. }
  1374. }
  1375. };
  1376. /**
  1377. * For polling, send a close packet.
  1378. *
  1379. * @api private
  1380. */
  1381. Polling.prototype.doClose = function(){
  1382. var self = this;
  1383. function close(){
  1384. debug('writing close packet');
  1385. self.write([{ type: 'close' }]);
  1386. }
  1387. if ('open' == this.readyState) {
  1388. debug('transport open - closing');
  1389. close();
  1390. } else {
  1391. // in case we're trying to close while
  1392. // handshaking is in progress (GH-164)
  1393. debug('transport not open - deferring close');
  1394. this.once('open', close);
  1395. }
  1396. };
  1397. /**
  1398. * Writes a packets payload.
  1399. *
  1400. * @param {Array} data packets
  1401. * @param {Function} drain callback
  1402. * @api private
  1403. */
  1404. Polling.prototype.write = function(packets){
  1405. var self = this;
  1406. this.writable = false;
  1407. var callbackfn = function() {
  1408. self.writable = true;
  1409. self.emit('drain');
  1410. };
  1411. var self = this;
  1412. parser.encodePayload(packets, this.supportsBinary, function(data) {
  1413. self.doWrite(data, callbackfn);
  1414. });
  1415. };
  1416. /**
  1417. * Generates uri for connection.
  1418. *
  1419. * @api private
  1420. */
  1421. Polling.prototype.uri = function(){
  1422. var query = this.query || {};
  1423. var schema = this.secure ? 'https' : 'http';
  1424. var port = '';
  1425. // cache busting is forced
  1426. if (false !== this.timestampRequests) {
  1427. query[this.timestampParam] = +new Date + '-' + Transport.timestamps++;
  1428. }
  1429. if (!this.supportsBinary && !query.sid) {
  1430. query.b64 = 1;
  1431. }
  1432. query = parseqs.encode(query);
  1433. // avoid port if default for schema
  1434. if (this.port && (('https' == schema && this.port != 443) ||
  1435. ('http' == schema && this.port != 80))) {
  1436. port = ':' + this.port;
  1437. }
  1438. // prepend ? to query
  1439. if (query.length) {
  1440. query = '?' + query;
  1441. }
  1442. return schema + '://' + this.hostname + port + this.path + query;
  1443. };
  1444. },{"../transport":4,"component-inherit":13,"debug":14,"engine.io-parser":17,"parseqs":27,"xmlhttprequest":10}],9:[function(_dereq_,module,exports){
  1445. /**
  1446. * Module dependencies.
  1447. */
  1448. var Transport = _dereq_('../transport');
  1449. var parser = _dereq_('engine.io-parser');
  1450. var parseqs = _dereq_('parseqs');
  1451. var inherit = _dereq_('component-inherit');
  1452. var debug = _dereq_('debug')('engine.io-client:websocket');
  1453. /**
  1454. * `ws` exposes a WebSocket-compatible interface in
  1455. * Node, or the `WebSocket` or `MozWebSocket` globals
  1456. * in the browser.
  1457. */
  1458. var WebSocket = _dereq_('ws');
  1459. /**
  1460. * Module exports.
  1461. */
  1462. module.exports = WS;
  1463. /**
  1464. * WebSocket transport constructor.
  1465. *
  1466. * @api {Object} connection options
  1467. * @api public
  1468. */
  1469. function WS(opts){
  1470. var forceBase64 = (opts && opts.forceBase64);
  1471. if (forceBase64) {
  1472. this.supportsBinary = false;
  1473. }
  1474. Transport.call(this, opts);
  1475. }
  1476. /**
  1477. * Inherits from Transport.
  1478. */
  1479. inherit(WS, Transport);
  1480. /**
  1481. * Transport name.
  1482. *
  1483. * @api public
  1484. */
  1485. WS.prototype.name = 'websocket';
  1486. /*
  1487. * WebSockets support binary
  1488. */
  1489. WS.prototype.supportsBinary = true;
  1490. /**
  1491. * Opens socket.
  1492. *
  1493. * @api private
  1494. */
  1495. WS.prototype.doOpen = function(){
  1496. if (!this.check()) {
  1497. // let probe timeout
  1498. return;
  1499. }
  1500. var self = this;
  1501. var uri = this.uri();
  1502. var protocols = void(0);
  1503. var opts = { agent: this.agent };
  1504. this.ws = new WebSocket(uri, protocols, opts);
  1505. if (this.ws.binaryType === undefined) {
  1506. this.supportsBinary = false;
  1507. }
  1508. this.ws.binaryType = 'arraybuffer';
  1509. this.addEventListeners();
  1510. };
  1511. /**
  1512. * Adds event listeners to the socket
  1513. *
  1514. * @api private
  1515. */
  1516. WS.prototype.addEventListeners = function(){
  1517. var self = this;
  1518. this.ws.onopen = function(){
  1519. self.onOpen();
  1520. };
  1521. this.ws.onclose = function(){
  1522. self.onClose();
  1523. };
  1524. this.ws.onmessage = function(ev){
  1525. self.onData(ev.data);
  1526. };
  1527. this.ws.onerror = function(e){
  1528. self.onError('websocket error', e);
  1529. };
  1530. };
  1531. /**
  1532. * Override `onData` to use a timer on iOS.
  1533. * See: https://gist.github.com/mloughran/2052006
  1534. *
  1535. * @api private
  1536. */
  1537. if ('undefined' != typeof navigator
  1538. && /iPad|iPhone|iPod/i.test(navigator.userAgent)) {
  1539. WS.prototype.onData = function(data){
  1540. var self = this;
  1541. setTimeout(function(){
  1542. Transport.prototype.onData.call(self, data);
  1543. }, 0);
  1544. };
  1545. }
  1546. /**
  1547. * Writes data to socket.
  1548. *
  1549. * @param {Array} array of packets.
  1550. * @api private
  1551. */
  1552. WS.prototype.write = function(packets){
  1553. var self = this;
  1554. this.writable = false;
  1555. // encodePacket efficient as it uses WS framing
  1556. // no need for encodePayload
  1557. for (var i = 0, l = packets.length; i < l; i++) {
  1558. parser.encodePacket(packets[i], this.supportsBinary, function(data) {
  1559. //Sometimes the websocket has already been closed but the browser didn't
  1560. //have a chance of informing us about it yet, in that case send will
  1561. //throw an error
  1562. try {
  1563. self.ws.send(data);
  1564. } catch (e){
  1565. debug('websocket closed before onclose event');
  1566. }
  1567. });
  1568. }
  1569. function ondrain() {
  1570. self.writable = true;
  1571. self.emit('drain');
  1572. }
  1573. // fake drain
  1574. // defer to next tick to allow Socket to clear writeBuffer
  1575. setTimeout(ondrain, 0);
  1576. };
  1577. /**
  1578. * Called upon close
  1579. *
  1580. * @api private
  1581. */
  1582. WS.prototype.onClose = function(){
  1583. Transport.prototype.onClose.call(this);
  1584. };
  1585. /**
  1586. * Closes socket.
  1587. *
  1588. * @api private
  1589. */
  1590. WS.prototype.doClose = function(){
  1591. if (typeof this.ws !== 'undefined') {
  1592. this.ws.close();
  1593. }
  1594. };
  1595. /**
  1596. * Generates uri for connection.
  1597. *
  1598. * @api private
  1599. */
  1600. WS.prototype.uri = function(){
  1601. var query = this.query || {};
  1602. var schema = this.secure ? 'wss' : 'ws';
  1603. var port = '';
  1604. // avoid port if default for schema
  1605. if (this.port && (('wss' == schema && this.port != 443)
  1606. || ('ws' == schema && this.port != 80))) {
  1607. port = ':' + this.port;
  1608. }
  1609. // append timestamp to URI
  1610. if (this.timestampRequests) {
  1611. query[this.timestampParam] = +new Date;
  1612. }
  1613. // communicate binary support capabilities
  1614. if (!this.supportsBinary) {
  1615. query.b64 = 1;
  1616. }
  1617. query = parseqs.encode(query);
  1618. // prepend ? to query
  1619. if (query.length) {
  1620. query = '?' + query;
  1621. }
  1622. return schema + '://' + this.hostname + port + this.path + query;
  1623. };
  1624. /**
  1625. * Feature detection for WebSocket.
  1626. *
  1627. * @return {Boolean} whether this transport is available.
  1628. * @api public
  1629. */
  1630. WS.prototype.check = function(){
  1631. return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name);
  1632. };
  1633. },{"../transport":4,"component-inherit":13,"debug":14,"engine.io-parser":17,"parseqs":27,"ws":29}],10:[function(_dereq_,module,exports){
  1634. // browser shim for xmlhttprequest module
  1635. var hasCORS = _dereq_('has-cors');
  1636. module.exports = function(opts) {
  1637. var xdomain = opts.xdomain;
  1638. // scheme must be same when usign XDomainRequest
  1639. // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
  1640. var xscheme = opts.xscheme;
  1641. // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default.
  1642. // https://github.com/Automattic/engine.io-client/pull/217
  1643. var enablesXDR = opts.enablesXDR;
  1644. // XMLHttpRequest can be disabled on IE
  1645. try {
  1646. if ('undefined' != typeof XMLHttpRequest && (!xdomain || hasCORS)) {
  1647. return new XMLHttpRequest();
  1648. }
  1649. } catch (e) { }
  1650. // Use XDomainRequest for IE8 if enablesXDR is true
  1651. // because loading bar keeps flashing when using jsonp-polling
  1652. // https://github.com/yujiosaka/socke.io-ie8-loading-example
  1653. try {
  1654. if ('undefined' != typeof XDomainRequest && !xscheme && enablesXDR) {
  1655. return new XDomainRequest();
  1656. }
  1657. } catch (e) { }
  1658. if (!xdomain) {
  1659. try {
  1660. return new ActiveXObject('Microsoft.XMLHTTP');
  1661. } catch(e) { }
  1662. }
  1663. }
  1664. },{"has-cors":23}],11:[function(_dereq_,module,exports){
  1665. (function (global){
  1666. /**
  1667. * Create a blob builder even when vendor prefixes exist
  1668. */
  1669. var BlobBuilder = global.BlobBuilder
  1670. || global.WebKitBlobBuilder
  1671. || global.MSBlobBuilder
  1672. || global.MozBlobBuilder;
  1673. /**
  1674. * Check if Blob constructor is supported
  1675. */
  1676. var blobSupported = (function() {
  1677. try {
  1678. var b = new Blob(['hi']);
  1679. return b.size == 2;
  1680. } catch(e) {
  1681. return false;
  1682. }
  1683. })();
  1684. /**
  1685. * Check if BlobBuilder is supported
  1686. */
  1687. var blobBuilderSupported = BlobBuilder
  1688. && BlobBuilder.prototype.append
  1689. && BlobBuilder.prototype.getBlob;
  1690. function BlobBuilderConstructor(ary, options) {
  1691. options = options || {};
  1692. var bb = new BlobBuilder();
  1693. for (var i = 0; i < ary.length; i++) {
  1694. bb.append(ary[i]);
  1695. }
  1696. return (options.type) ? bb.getBlob(options.type) : bb.getBlob();
  1697. };
  1698. module.exports = (function() {
  1699. if (blobSupported) {
  1700. return global.Blob;
  1701. } else if (blobBuilderSupported) {
  1702. return BlobBuilderConstructor;
  1703. } else {
  1704. return undefined;
  1705. }
  1706. })();
  1707. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1708. },{}],12:[function(_dereq_,module,exports){
  1709. /**
  1710. * Expose `Emitter`.
  1711. */
  1712. module.exports = Emitter;
  1713. /**
  1714. * Initialize a new `Emitter`.
  1715. *
  1716. * @api public
  1717. */
  1718. function Emitter(obj) {
  1719. if (obj) return mixin(obj);
  1720. };
  1721. /**
  1722. * Mixin the emitter properties.
  1723. *
  1724. * @param {Object} obj
  1725. * @return {Object}
  1726. * @api private
  1727. */
  1728. function mixin(obj) {
  1729. for (var key in Emitter.prototype) {
  1730. obj[key] = Emitter.prototype[key];
  1731. }
  1732. return obj;
  1733. }
  1734. /**
  1735. * Listen on the given `event` with `fn`.
  1736. *
  1737. * @param {String} event
  1738. * @param {Function} fn
  1739. * @return {Emitter}
  1740. * @api public
  1741. */
  1742. Emitter.prototype.on =
  1743. Emitter.prototype.addEventListener = function(event, fn){
  1744. this._callbacks = this._callbacks || {};
  1745. (this._callbacks[event] = this._callbacks[event] || [])
  1746. .push(fn);
  1747. return this;
  1748. };
  1749. /**
  1750. * Adds an `event` listener that will be invoked a single
  1751. * time then automatically removed.
  1752. *
  1753. * @param {String} event
  1754. * @param {Function} fn
  1755. * @return {Emitter}
  1756. * @api public
  1757. */
  1758. Emitter.prototype.once = function(event, fn){
  1759. var self = this;
  1760. this._callbacks = this._callbacks || {};
  1761. function on() {
  1762. self.off(event, on);
  1763. fn.apply(this, arguments);
  1764. }
  1765. on.fn = fn;
  1766. this.on(event, on);
  1767. return this;
  1768. };
  1769. /**
  1770. * Remove the given callback for `event` or all
  1771. * registered callbacks.
  1772. *
  1773. * @param {String} event
  1774. * @param {Function} fn
  1775. * @return {Emitter}
  1776. * @api public
  1777. */
  1778. Emitter.prototype.off =
  1779. Emitter.prototype.removeListener =
  1780. Emitter.prototype.removeAllListeners =
  1781. Emitter.prototype.removeEventListener = function(event, fn){
  1782. this._callbacks = this._callbacks || {};
  1783. // all
  1784. if (0 == arguments.length) {
  1785. this._callbacks = {};
  1786. return this;
  1787. }
  1788. // specific event
  1789. var callbacks = this._callbacks[event];
  1790. if (!callbacks) return this;
  1791. // remove all handlers
  1792. if (1 == arguments.length) {
  1793. delete this._callbacks[event];
  1794. return this;
  1795. }
  1796. // remove specific handler
  1797. var cb;
  1798. for (var i = 0; i < callbacks.length; i++) {
  1799. cb = callbacks[i];
  1800. if (cb === fn || cb.fn === fn) {
  1801. callbacks.splice(i, 1);
  1802. break;
  1803. }
  1804. }
  1805. return this;
  1806. };
  1807. /**
  1808. * Emit `event` with the given args.
  1809. *
  1810. * @param {String} event
  1811. * @param {Mixed} ...
  1812. * @return {Emitter}
  1813. */
  1814. Emitter.prototype.emit = function(event){
  1815. this._callbacks = this._callbacks || {};
  1816. var args = [].slice.call(arguments, 1)
  1817. , callbacks = this._callbacks[event];
  1818. if (callbacks) {
  1819. callbacks = callbacks.slice(0);
  1820. for (var i = 0, len = callbacks.length; i < len; ++i) {
  1821. callbacks[i].apply(this, args);
  1822. }
  1823. }
  1824. return this;
  1825. };
  1826. /**
  1827. * Return array of callbacks for `event`.
  1828. *
  1829. * @param {String} event
  1830. * @return {Array}
  1831. * @api public
  1832. */
  1833. Emitter.prototype.listeners = function(event){
  1834. this._callbacks = this._callbacks || {};
  1835. return this._callbacks[event] || [];
  1836. };
  1837. /**
  1838. * Check if this emitter has `event` handlers.
  1839. *
  1840. * @param {String} event
  1841. * @return {Boolean}
  1842. * @api public
  1843. */
  1844. Emitter.prototype.hasListeners = function(event){
  1845. return !! this.listeners(event).length;
  1846. };
  1847. },{}],13:[function(_dereq_,module,exports){
  1848. module.exports = function(a, b){
  1849. var fn = function(){};
  1850. fn.prototype = b.prototype;
  1851. a.prototype = new fn;
  1852. a.prototype.constructor = a;
  1853. };
  1854. },{}],14:[function(_dereq_,module,exports){
  1855. /**
  1856. * This is the web browser implementation of `debug()`.
  1857. *
  1858. * Expose `debug()` as the module.
  1859. */
  1860. exports = module.exports = _dereq_('./debug');
  1861. exports.log = log;
  1862. exports.formatArgs = formatArgs;
  1863. exports.save = save;
  1864. exports.load = load;
  1865. exports.useColors = useColors;
  1866. /**
  1867. * Colors.
  1868. */
  1869. exports.colors = [
  1870. 'lightseagreen',
  1871. 'forestgreen',
  1872. 'goldenrod',
  1873. 'dodgerblue',
  1874. 'darkorchid',
  1875. 'crimson'
  1876. ];
  1877. /**
  1878. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  1879. * and the Firebug extension (any Firefox version) are known
  1880. * to support "%c" CSS customizations.
  1881. *
  1882. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  1883. */
  1884. function useColors() {
  1885. // is webkit? http://stackoverflow.com/a/16459606/376773
  1886. return ('WebkitAppearance' in document.documentElement.style) ||
  1887. // is firebug? http://stackoverflow.com/a/398120/376773
  1888. (window.console && (console.firebug || (console.exception && console.table))) ||
  1889. // is firefox >= v31?
  1890. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messag…

Large files files are truncated, but you can click here to view the full file