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
39 changes: 39 additions & 0 deletions packages/socket.io-client/test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,45 @@ describe("connection", () => {
});
});

it("should accept 0 as the value of 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);
// @ts-ignore property 'backoff' is private
expect(manager.backoff.duration()).to.be(0);
});

it("should not try to reconnect when requested zero attempts", () => {
return wrap((done) => {
const manager = new Manager(BASE_URL, {
reconnection: true,
timeout: 0,
reconnectionAttempts: 0,
reconnectionDelay: 10,
});
let socket;

manager.on("reconnect_attempt", () => {
done(new Error("should not happen"));
});

manager.on("reconnect_failed", () => {
socket.close();
manager._close();
done();
});

socket = manager.socket("/timeout");
});
});

it("should fire reconnect_* events on manager", () => {
return wrap((done) => {
const manager = new Manager(BASE_URL, {
Expand Down