import { Transport } from "../transport.js" ; import debugModule from "debug" ; import yeast from "yeast" ; import parseqs from "parseqs" ; import { encodePayload , decodePayload } from "engine.io-parser" ; const debug = debugModule ( "engine.io-client:polling" ) ; export abstract class Polling extends Transport { private polling : boolean = false ; /**
     * Transport name.
     */ get name ( ) { return "polling" ; } api doOpen ( ) { this . poll ( ) ; } param { Function } callback api pause ( onPause ) { this . readyState = "pausing" ; const pause = ( ) => { debug ( "paused" ) ; this . readyState = "paused" ; onPause ( ) ; } ; if ( this . polling || ! this . writable ) { let total = 0 ; if ( this . polling ) { debug ( "we are currently polling - waiting to pause" ) ; total ++ ; this . once ( "pollComplete" , function ( ) { debug ( "pre-pause polling complete" ) ; -- total || pause ( ) ; } ) ; } if ( ! this . writable ) { debug ( "we are currently writing - waiting to pause" ) ; total ++ ; this . once ( "drain" , function ( ) { debug ( "pre-pause writing complete" ) ; -- total || pause ( ) ; } ) ; } } else { pause ( ) ; } } api poll ( ) { debug ( "polling" ) ; this . polling = true ; this . doPoll ( ) ; this . emit ( "poll" ) ; } api onData ( data ) { debug ( "polling got data %s" , data ) ; const callback = packet => { if ( "opening" === this . readyState && packet . type === "open" ) { this . onOpen ( ) ; } if ( "close" === packet . type ) { this . onClose ( ) ; return false ; } this . onPacket ( packet ) ; } ; decodePayload ( data , this . socket . binaryType ) . forEach ( callback ) ; if ( "closed" !== this . readyState ) { this . polling = false ; this . emit ( "pollComplete" ) ; if ( "open" === this . readyState ) { this . poll ( ) ; } else { debug ( 'ignoring poll - transport state "%s"' , this . readyState ) ; } } } api doClose ( ) { const close = ( ) => { debug ( "writing close packet" ) ; this . write ( [ { type : "close" } ] ) ; } ; if ( "open" === this . readyState ) { debug ( "transport open - closing" ) ; close ( ) ; } else { debug ( "transport not open - deferring close" ) ; this . once ( "open" , close ) ; } } param { Array } data param { Function } drain api write ( packets ) { this . writable = false ; encodePayload ( packets , data => { this . doWrite ( data , ( ) => { this . writable = true ; this . emit ( "drain" ) ; } ) ; } ) ; } api uri ( ) { let query = this . query || { } ; const schema = this . opts . secure ? "https" : "http" ; let port = "" ; if ( false !== this . opts . timestampRequests ) { query [ this . opts . timestampParam ] = yeast ( ) ; } if ( ! this . supportsBinary && ! query . sid ) { query . b64 = 1 ; } if ( this . opts . port && ( ( "https" === schema && Number ( this . opts . port ) !== 443 ) || ( "http" === schema && Number ( this . opts . port ) !== 80 ) ) ) { port = ":" + this . opts . port ; } const encodedQuery = parseqs . encode ( query ) ; const ipv6 = this . opts . hostname . indexOf ( ":" ) !== - 1 ; return ( schema + "://" + ( ipv6 ? "[" + this . opts . hostname + "]" : this . opts . hostname ) + port + this . opts . path + ( encodedQuery . length ? "?" + encodedQuery : "" ) ) ; } abstract doPoll ( ) ; abstract doWrite ( data , callback ) ; }