Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ export class Socket extends Emitter<
debug('probing transport "%s"', name);
let transport = this.createTransport(name);
let failed = false;
let upgrading = false;

Socket.priorWebsocketSuccess = false;

Expand All @@ -622,6 +623,7 @@ export class Socket extends Emitter<
if (failed) return;
if ("pong" === msg.type && "probe" === msg.data) {
debug('probe transport "%s" pong', name);
upgrading = true;
this.upgrading = true;
this.emitReserved("upgrading", transport);
if (!transport) return;
Expand All @@ -637,6 +639,7 @@ export class Socket extends Emitter<

this.setTransport(transport);
transport.send([{ type: "upgrade" }]);
upgrading = false;
this.upgrading = false;
this.emitReserved("upgrade", transport);
transport = null;
Expand Down Expand Up @@ -672,9 +675,25 @@ export class Socket extends Emitter<

freezeTransport();

const wasUpgrading = upgrading;
if (upgrading) {
upgrading = false;
this.upgrading = false;
if ("websocket" === name) {
Socket.priorWebsocketSuccess = false;
}
if ("closed" !== this.readyState) {
this.transport.resume();
}
}

debug('probe transport "%s" failed because of error: %s', name, err);

this.emitReserved("upgradeError", error);

if (wasUpgrading && "closed" !== this.readyState) {
this.flush();
}
};

function onTransportClose() {
Expand Down
5 changes: 5 additions & 0 deletions packages/engine.io-client/lib/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export abstract class Transport extends Emitter<
*/
public pause(onPause: () => void) {}

/**
* Resumes the transport after an aborted upgrade pause.
*/
public resume() {}

protected createUri(schema: string, query: Record<string, unknown> = {}) {
return (
schema +
Expand Down
61 changes: 56 additions & 5 deletions packages/engine.io-client/lib/transports/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = debugModule("engine.io-client:polling"); // debug()

export abstract class Polling extends Transport {
private _polling: boolean = false;
private _abortPause?: () => void;

override get name() {
return "polling";
Expand All @@ -29,39 +30,89 @@ export abstract class Polling extends Transport {
* @package
*/
override pause(onPause) {
this._abortPause?.();
this._abortPause = undefined;
this.readyState = "pausing";

let aborted = false;

const pause = () => {
if (aborted || "pausing" !== this.readyState) return;
debug("paused");
this._abortPause = undefined;
this.readyState = "paused";
onPause();
};

if (this._polling || !this.writable) {
let total = 0;
let onPollComplete: (() => void) | undefined;
let onDrain: (() => void) | undefined;

if (this._polling) {
debug("we are currently polling - waiting to pause");
total++;
this.once("pollComplete", function () {
onPollComplete = () => {
if (aborted) return;
debug("pre-pause polling complete");
onPollComplete = undefined;
--total || pause();
});
};
this.once("pollComplete", onPollComplete);
}

if (!this.writable) {
debug("we are currently writing - waiting to pause");
total++;
this.once("drain", function () {
onDrain = () => {
if (aborted) return;
debug("pre-pause writing complete");
onDrain = undefined;
--total || pause();
});
};
this.once("drain", onDrain);
}

this._abortPause = () => {
aborted = true;
if (onPollComplete) {
this.off("pollComplete", onPollComplete);
onPollComplete = undefined;
}
if (onDrain) {
this.off("drain", onDrain);
onDrain = undefined;
}
};
} else {
pause();
}
}

/**
* Resumes polling after an aborted pause.
*
* @package
*/
override resume() {
if ("pausing" !== this.readyState && "paused" !== this.readyState) {
return;
}

debug("resuming polling");
this._abortPause?.();
this._abortPause = undefined;
this.readyState = "open";

if (
!this._polling &&
"closing" !== this.socket?.readyState &&
"closed" !== this.socket?.readyState
) {
this._poll();
}
}

/**
* Starts polling cycle.
*
Expand Down Expand Up @@ -106,7 +157,7 @@ export abstract class Polling extends Transport {
this._polling = false;
this.emitReserved("pollComplete");

if ("open" === this.readyState) {
if ("open" === this.readyState && !this._polling) {
this._poll();
} else {
debug('ignoring poll - transport state "%s"', this.readyState);
Expand Down
Loading