Skip to content

Commit

Permalink
Added ability to callback when a message has been sent
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmonkey committed Nov 14, 2010
1 parent 0cbeeae commit 3ac6763
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions stomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ function Connection(host, port, login, password){
this.processMessages();
}

Connection.prototype.transmit = function(msg){
this.conn.write(msg);
Connection.prototype.transmit = function(msg, callback){
if (this.conn.write(msg)) {
if (typeof callback == 'function') callback(msg);
} else {
if (typeof callback == 'function') {
this.conn.on('drain', function() {
callback(msg);
});
}
}
};

Connection.prototype.prepareMessage = function(msg){
Expand All @@ -71,7 +79,7 @@ Connection.prototype.processCommands = function(){
if(this.conn.readyState == "open"){
var m = null;
while(m = this.commands.shift()){
this.transmit(this.prepareMessage(m));
this.transmit(this.prepareMessage(m), m.callback);
}
}

Expand Down Expand Up @@ -132,9 +140,9 @@ Connection.prototype.processMessage = function(data){
}
};

Connection.prototype.publish = function(destination, data, headers){
Connection.prototype.publish = function(destination, data, headers, callback){
headers && (headers["destination"] = destination) || (headers = {"destination": destination});
this.commands.push({command: "SEND", headers: headers, data: data});
this.commands.push({command: "SEND", headers: headers, data: data, callback: callback});
};

Connection.prototype.subscribe = function(destination, headers, callback){
Expand Down Expand Up @@ -183,8 +191,8 @@ function Client(host, port, login, password){
this.conn = new Connection(host, port, login, password);
};

Client.prototype.publish = function(destination, data, headers){
this.conn.publish(destination, data, headers);
Client.prototype.publish = function(destination, data, headers, callback){
this.conn.publish(destination, data, headers, callback);
};

Client.prototype.subscribe = function(destination, headers, callback){
Expand Down

0 comments on commit 3ac6763

Please sign in to comment.