jmesnil/stomp-websocket — pull request #67 by Puigcerber (2014-05-27 13:14:22+00:00)
Title: Add different handlers for the socket events 'error' and 'close'.

I noticed that when the connection to the STOMP broker fails the error callback is fired twice.

This is due to the [net specification](http://nodejs.org/api/net.html#net_event_error_1) where the 'close' event will be called directly following the 'error' event. And the fact that we are listening to both events:

socket.on('error', function(e) {
return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
});
socket.on('close', function(e) {
return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
});

But threatening them on the same way and not paying attention to the value of the parameter error:

this.ws.onclose = function() {
var msg;
msg = "Whoops! Lost connection to " + _this.ws.url;
if (typeof _this.debug === "function") {
_this.debug(msg);
}
_this._cleanUp();
return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
};

Although we could use just the current `onclose` method I have decided to add an `onerror` method passing different objects to the error callback.

In the [documentation](http://jmesnil.net/stomp-websocket/doc/#connection) of the library it's defined that the error callback will be called with a single argument corresponding to a STOMP ERROR frame. So I'm emulating the frame building an object with a message in the headers. This way anybody could do the defined:

var error_callback = function(error) {
// display the error's message header:
alert(error.headers.message);
};

With this modification reconnecting on error as you described [here](http://stackoverflow.com/a/22403521/706246) can be better handled:

var error_callback = function(error) {
if (error.command === 'CLOSE') {
// Reconnect.
} else {
// Handle error.
}
};