diff --git a/lib/client.js b/lib/client.js index c6b851f..c08ec44 100644 --- a/lib/client.js +++ b/lib/client.js @@ -167,9 +167,7 @@ StompClient.prototype.disconnect = function (callback) { var frame = new StompFrame({ command: 'DISCONNECT' - }).send(this.stream); - - process.nextTick(function() { + }).send(this.stream, function() { self.stream.end(); }); } @@ -304,14 +302,14 @@ StompClient.prototype.unsubscribe = function (queue, headers) { return this; }; -StompClient.prototype.publish = function(queue, message, headers) { +StompClient.prototype.publish = function(queue, message, headers, cb) { headers = _extend({}, headers); headers.destination = queue; new StompFrame({ command: 'SEND', headers: headers, body: message - }).send(this.stream); + }).send(this.stream, cb); return this; }; diff --git a/lib/frame.js b/lib/frame.js index 65f0fd5..a5d5706 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -16,7 +16,7 @@ StompFrame.prototype.toString = function() { }); }; -StompFrame.prototype.send = function(stream) { +StompFrame.prototype.send = function(stream, cb) { // Avoid small writes, they get sent in their own tcp packet, which // is not efficient (and v8 does fast string concat). var frame = this.command + '\n'; @@ -34,7 +34,7 @@ StompFrame.prototype.send = function(stream) { } frame += '\0'; if(frame) - stream.write(frame); + stream.write(frame, cb); }; StompFrame.prototype.setCommand = function(command) { diff --git a/test/client.test.js b/test/client.test.js index ffa5369..49b0f7b 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -42,10 +42,10 @@ module.exports = testCase({ }; oldSend = StompFrame.prototype.send; - StompFrame.prototype.send = function(stream) { + StompFrame.prototype.send = function(stream, cb) { var self = this; process.nextTick(function () { - sendHook(self); + sendHook(self, cb); }); }; @@ -603,10 +603,11 @@ module.exports = testCase({ self.stompClient.connect(function() { // Assert next outbound STOMP frame is a DISCONNECT - sendHook = function (stompFrame) { + sendHook = function (stompFrame, cb) { test.equal(stompFrame.command, 'DISCONNECT'); test.deepEqual(stompFrame.headers, {}); test.equal(stompFrame.body, ''); + cb(); }; // Set disconnection callback to ensure it is called appropriately