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
4 changes: 2 additions & 2 deletions packages/socket.io-client/lib/contrib/backo2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

export function Backoff(opts) {
opts = opts || {};
this.ms = opts.min || 100;
this.max = opts.max || 10000;
this.ms = opts.min ?? 100;
this.max = opts.max ?? 10000;
this.factor = opts.factor || 2;
this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
this.attempts = 0;
Expand Down
6 changes: 3 additions & 3 deletions packages/socket.io-client/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ export class Manager<
this.opts = opts;
installTimerFunctions(this, opts);
this.reconnection(opts.reconnection !== false);
this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
this.reconnectionDelay(opts.reconnectionDelay || 1000);
this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
this.reconnectionAttempts(opts.reconnectionAttempts ?? Infinity);
this.reconnectionDelay(opts.reconnectionDelay ?? 1000);
this.reconnectionDelayMax(opts.reconnectionDelayMax ?? 5000);
this.randomizationFactor(opts.randomizationFactor ?? 0.5);
this.backoff = new Backoff({
min: this.reconnectionDelay(),
Expand Down
43 changes: 43 additions & 0 deletions packages/socket.io-client/test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,49 @@ describe("connection", () => {
}
});

it("should keep a zero value for the reconnection options", () => {
const manager = new Manager(BASE_URL, {
autoConnect: false,
reconnectionAttempts: 0,
reconnectionDelay: 0,
reconnectionDelayMax: 0,
});

expect(manager.reconnectionAttempts()).to.be(0);
expect(manager.reconnectionDelay()).to.be(0);
expect(manager.reconnectionDelayMax()).to.be(0);

// the backoff object should honor the zero delay too
// @ts-ignore
const backoff = manager.backoff;
backoff.attempts = 5;
expect(backoff.duration()).to.be(0);

manager._close();
});

it("should not attempt to reconnect when reconnectionAttempts is 0", () => {
return wrap((done) => {
const manager = new Manager(BASE_URL, {
reconnection: true,
timeout: 0,
reconnectionAttempts: 0,
reconnectionDelay: 0,
});
const socket = manager.socket("/timeout");
let attempts = 0;
manager.on("reconnect_attempt", () => {
attempts++;
});
manager.on("reconnect_failed", () => {
expect(attempts).to.be(0);
socket.close();
manager._close();
done();
});
});
});

it("should not reconnect when force closed", () => {
return wrap((done) => {
const socket = io(BASE_URL + "/invalid", {
Expand Down