jmesnil/stomp-websocket — pull request #80 by Puigcerber (2014-08-14 15:32:15+00:00)
Title: Add different handlers for the socket events 'error' and 'close'.

I noticed that when the connection to the STOMP broker fails due to a network issue 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 treating 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;
};

So I have decided to add an `onerror` method for handling the errors.

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) could be better handled:

var error_callback = function(error, had_error) {
if (had_error) {
// Reconnect.
} else {
// Handle error.
}
};

This pull request recreates #67 but from an specific branch.