diff --git a/lib/manager.ts b/lib/manager.ts index 4d6caa2be..6a980c9fd 100644 --- a/lib/manager.ts +++ b/lib/manager.ts @@ -257,6 +257,18 @@ export interface ManagerOptions extends EngineOptions { * the parser to use. Defaults to an instance of the Parser that ships with socket.io. */ parser: any; + + /** + * Should we buffer receive events if socket is not connected + * @default true + */ + enableReceiveBuffer: boolean; + + /** + * Should we buffer send events if socket is not connected + * @default true + */ + enableSendBuffer: boolean; } export class Manager extends Emitter { @@ -278,6 +290,14 @@ export class Manager extends Emitter { * @private */ _reconnecting: boolean; + /** + * @private + */ + _enableReceiveBuffer: boolean; + /** + * @private + */ + _enableSendBuffer: boolean; private readonly uri: string; private readonly opts: object; @@ -333,6 +353,8 @@ export class Manager extends Emitter { this.encoder = new _parser.Encoder(); this.decoder = new _parser.Decoder(); this._autoConnect = opts.autoConnect !== false; + this._enableReceiveBuffer = opts.enableReceiveBuffer !== false; + this._enableSendBuffer = opts.enableSendBuffer !== false; if (this._autoConnect) this.open(); } diff --git a/lib/socket.ts b/lib/socket.ts index dbcb9285c..60bea2b39 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -162,7 +162,7 @@ export class Socket extends Emitter { debug("discard packet as the transport is not currently writable"); } else if (this.connected) { this.packet(packet); - } else { + } else if (this.io._enableSendBuffer) { this.sendBuffer.push(packet); } @@ -273,7 +273,7 @@ export class Socket extends Emitter { args.push(this.ack(packet.id)); } - if (this.connected) { + if (this.connected || !this.io._enableReceiveBuffer) { this.emitEvent(args); } else { this.receiveBuffer.push(args);