5 var Transport = require('../transport')
6 , util = require('../util')
7 , parser = require('../parser')
8 , debug = require('debug')('engine.io-client:polling');
14 module.exports = Polling;
20 var global = 'undefined' != typeof window ? window : global;
25 * @param {Object} opts
29 function Polling(opts){
30 Transport.call(this, opts);
34 * Inherits from Transport.
37 util.inherits(Polling, Transport);
43 Polling.prototype.name = 'polling';
46 * Opens the socket (triggers polling). We write a PING message to determine
47 * when the transport is open.
52 Polling.prototype.doOpen = function(){
59 * @param {Function} callback upon buffers are flushed and transport is paused
63 Polling.prototype.pause = function(onPause){
67 this.readyState = 'pausing';
71 self.readyState = 'paused';
75 if (this.polling || !this.writable) {
79 debug('we are currently polling - waiting to pause');
81 this.once('pollComplete', function(){
82 debug('pre-pause polling complete');
88 debug('we are currently writing - waiting to pause');
90 this.once('drain', function(){
91 debug('pre-pause writing complete');
101 * Starts polling cycle.
106 Polling.prototype.poll = function(){
114 * Overloads onData to detect payloads.
119 Polling.prototype.onData = function(data){
120 debug('polling got data %s', data);
122 var packets = parser.decodePayload(data);
124 for (var i = 0, l = packets.length; i < l; i++) {
125 // if its the first message we consider the trnasport open
126 if ('opening' == this.readyState) {
130 // if its a close packet, we close the ongoing requests
131 if ('close' == packets[i].type) {
136 // otherwise bypass onData and handle the message
137 this.onPacket(packets[i]);
140 // if we got data we're not polling
141 this.polling = false;
142 this.emit('pollComplete');
144 if ('open' == this.readyState) {
147 debug('ignoring poll - transport state "%s"', this.readyState);
152 * For polling, send a close packet.
157 Polling.prototype.doClose = function(){
158 debug('sending close packet');
159 this.send([{ type: 'close' }]);
163 * Writes a packets payload.
165 * @param {Array} data packets
166 * @param {Function} drain callback
170 Polling.prototype.write = function(packets){
172 this.writable = false;
173 this.doWrite(parser.encodePayload(packets), function(){
174 self.writable = true;
180 * Generates uri for connection.
185 Polling.prototype.uri = function(){
186 var query = this.query || {};
187 var schema = this.secure ? 'https' : 'http';
190 // cache busting is forced for IE / android / iOS6 ಠ_ಠ
191 if (global.ActiveXObject || util.ua.android || util.ua.ios6
192 || this.timestampRequests) {
193 query[this.timestampParam] = +new Date;
196 query = util.qs(query);
198 // avoid port if default for schema
199 if (this.port && (('https' == schema && this.port != 443)
200 || ('http' == schema && this.port != 80))) {
201 port = ':' + this.port;
204 // prepend ? to query
209 return schema + '://' + this.host + port + this.path + query;