Skip to content

Commit

Permalink
add support for drain event and send response boolean to make sockjs …
Browse files Browse the repository at this point in the history
…compliant with Writeable api that allows for handling back pressure
  • Loading branch information
avistramer committed Nov 4, 2024
1 parent 0e8d891 commit 8bff03f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ SockJS.prototype.send = function(data) {
if (this.readyState !== SockJS.OPEN) {
return;
}
this._transport.send(escape.quote(data));
return this._transport.send(escape.quote(data));
};

SockJS.version = require('./version');
Expand Down Expand Up @@ -223,6 +223,10 @@ SockJS.prototype._connect = function() {
debug('transport url', transportUrl);
var transportObj = new Transport(transportUrl, this._transUrl, options);
transportObj.on('message', this._transportMessage.bind(this));
// for transports that support backpressure handle drain event
transportObj.on('drain', function() {
self.dispatchEvent(new Event('drain'));
});
transportObj.once('close', this._transportClose.bind(this));
transportObj.transportName = Transport.transportName;
this._transport = transportObj;
Expand Down
1 change: 1 addition & 0 deletions lib/transport/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ IframeTransport.prototype.postMessage = function(type, data) {
IframeTransport.prototype.send = function(message) {
debug('send', message);
this.postMessage('m', message);
return true;
};

IframeTransport.enabled = function() {
Expand Down
1 change: 1 addition & 0 deletions lib/transport/lib/buffered-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BufferedSender.prototype.send = function(message) {
if (!this.sendStop) {
this.sendSchedule();
}
return true;
};

// For polling transports in a situation when in the message callback,
Expand Down
5 changes: 4 additions & 1 deletion lib/transport/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ function WebSocketTransport(transUrl, ignore, options) {
self.emit('close', 1006, 'WebSocket connection broken');
self._cleanup();
};
this.ws.on('drain', function() {
self.emit('drain');
})
}

inherits(WebSocketTransport, EventEmitter);

WebSocketTransport.prototype.send = function(data) {
var msg = '[' + data + ']';
debug('send', msg);
this.ws.send(msg);
return this.ws.send(msg);
};

WebSocketTransport.prototype.close = function() {
Expand Down

0 comments on commit 8bff03f

Please sign in to comment.