Skip to content

Commit

Permalink
Concept of splitLargeFrames flag - allow large packets with Spring We…
Browse files Browse the repository at this point in the history
…bSocket/STOMP.
  • Loading branch information
kum-deepak committed Jan 12, 2019
1 parent f86d9c5 commit 8ad4485
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions src/stomp-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/stomp-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class StompHandler {

public logRawCommunication: boolean;

public splitLargeFrames: boolean;

public maxWebSocketChunkSize: number = 8000;

get connectedVersion(): string {
return this._connectedVersion;
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8ad4485

Please sign in to comment.