diff --git a/src/client.ts b/src/client.ts index 5819cef6..ea1fe5dc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -75,6 +75,26 @@ export class Client { */ public heartbeatOutgoing: number = 10000; + /** + * This switches on a non standard behavior while sending WebSocket packets. + * It splits larger (text) packets into chunks of [maxWebSocketChunkSize]{@link Client#maxWebSocketChunkSize}. + * Only Java Spring brokers seems to use this mode. + * + * WebSockets split large (text) packets, so it is not needed with a truly compliant STOMP/WebSocket broker. + * Actually setting it for such broker will cause large messages to fail. + * + * `false` by default. + * + * Binary frames are never split. + */ + public splitLargeFrames: boolean = false; + + /** + * See [splitLargeFrames]{@link Client#splitLargeFrames}. + * This has no effect if [splitLargeFrames]{@link Client#splitLargeFrames} is `false`. + */ + public maxWebSocketChunkSize: number = 8 * 1024; + /** * Underlying WebSocket instance, READONLY. */ @@ -320,6 +340,8 @@ export class Client { disconnectHeaders: this._disconnectHeaders, heartbeatIncoming: this.heartbeatIncoming, heartbeatOutgoing: this.heartbeatOutgoing, + splitLargeFrames: this.splitLargeFrames, + maxWebSocketChunkSize: this.maxWebSocketChunkSize, logRawCommunication: this.logRawCommunication, onConnect: (frame) => { diff --git a/src/stomp-config.ts b/src/stomp-config.ts index fd66b07a..99b1893a 100644 --- a/src/stomp-config.ts +++ b/src/stomp-config.ts @@ -46,6 +46,16 @@ export class StompConfig { */ public heartbeatOutgoing?: number; + /** + * See [Client#splitLargeFrames]{@link Client#splitLargeFrames}. + */ + public splitLargeFrames?: boolean; + + /** + * See [Client#maxWebSocketChunkSize]{@link Client#maxWebSocketChunkSize}. + */ + public maxWebSocketChunkSize?: number; + /** * See [Client#connectHeaders]{@link Client#connectHeaders}. */ diff --git a/src/stomp-handler.ts b/src/stomp-handler.ts index 141ba813..bbf88fce 100644 --- a/src/stomp-handler.ts +++ b/src/stomp-handler.ts @@ -55,6 +55,10 @@ export class StompHandler { public logRawCommunication: boolean; + public splitLargeFrames: boolean; + + public maxWebSocketChunkSize: number = 8000; + get connectedVersion(): string { return this._connectedVersion; } @@ -270,7 +274,17 @@ export class StompHandler { this.debug(`>>> ${frame}`); } - this._webSocket.send(rawChunk); + if (frame.isBinaryBody || !this.splitLargeFrames) { + this._webSocket.send(rawChunk); + } else { + let out = rawChunk as string; + while (out.length > 0) { + const chunk = out.substring(0, this.maxWebSocketChunkSize); + out = out.substring(this.maxWebSocketChunkSize); + this._webSocket.send(chunk); + this.debug(`chunk sent = ${chunk.length}, remaining = ${out.length}`); + } + } } public dispose(): void {