Does not try to reconnect when connection is killed by server
When the socket server is restarted, the client socket connection gets the onclose event with code 1006. Since this isn't in _reconnectableStatusCodes, it never tries to reconnect again. Is there are reason that only code 4000 is in that list? What do the codes mean? Shouldn't it simply always try to reconnect unless the client explicitly close the connection?
The other weird thing: when it does try to reconnect (I've added code 1006 to the list), it always does it twice.
My changes (added some logs, switched this.notifyOpenCallbacks(event) and this._reconnectAttempts = 0 because of #19):
$WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) {
console.log('onOpen');
this.notifyOpenCallbacks(event);
this._reconnectAttempts = 0;
this.fireQueue();
};
$WebSocket.prototype._onCloseHandler = function _onCloseHandler(event) {
console.log('onClose');
this.notifyCloseCallbacks(event);
if (this._reconnectableStatusCodes.indexOf(event.code) > -1) {
console.log('trying to reconnect');
this.reconnect();
} else {
console.log('NOT trying to reconnect, code: ', event.code);
}
};
This is the how it unfolds:
angular-websocket.js:206 onClose
angular-websocket.js:209 trying to reconnect
angular-websocket.js:199 onOpen
angular-websocket.js:206 onClose
angular-websocket.js:209 trying to reconnect
angular-websocket.js:199 onOpen
If the first reconnect failed, then why did it call onOpen? Or if it did not fail, why was it immediately followed by an onClose and another reconnect and onOpen?
(I made sure that this isn't a bug caused by me switching this.notifyOpenCallbacks(event) and this._reconnectAttempts = 0)
@kevinrenskers you make any progress with this? i'm trying to figure out how to make this library work with reconnects too, and just found your issues here. wondering if you resolved it 9 days in?
i'm guessing the short fix is to handle all the reconnect logic outside the library which is not ideal.
I'm now using my own angular websockets wrapper instead of this library. It's basically the same code, but for example I'm now always trying to reconnect.
Hi, I had the same issue and I pushed a fix. Now if you should be fine if you do: ws = $websocket('ws://127.0.0.1:8888', null, {reconnectIfNotNormalClose: true}); I still have no idea why 1006 is not a _reconnectableStatusCodes though.
is there a release planned with this change? Edit: & can the change also include a user-defined list of reconnectable codes please, I also think 1006 needs to be in there...
Hi, I'm the developer who added (and issued the pull request containing) the reconnectIfNotNormalClose flag. If you set that flag to true, then all codes except for 1000, which is CLOSE_NORMAL, become reconnectable codes. So in that case, 1006 would be a reconnectable code.
thanks, sounds ideal. I'll hope for a release soon!
+1 - would be great to have a new release out soon!
Is this issue resolved in the new release? https://github.com/gdi2290/angular-websocket/releases/tag/v1.0.13
Yes, the patch d20de8870b02ab4c04c16affff7ab292d28e13f4 rebuild the affected objects and landed on 7th April. I'm using this cdn link and it reconnects fine:
<script type="text/javascript" src="https://cdn.rawgit.com/gdi2290/angular-websocket/v1.0.13/angular-websocket.min.js"></script>
Not so sure what I do wrong, but $websocket('ws://127.0.0.1:8888', null, {reconnectIfNotNormalClose: true}); does not work. I.e. when it's closed it stays closed.
Instead, my current workaround looks like that (which does work, but feels unnecessary...):
/* @ngInject */
function WSData($websocket) {
// Open a WebSocket connection
return {
ws: $websocket(location.origin.replace(/^http/, 'ws')),
init: function() {
this.ws = $websocket(location.origin.replace(/^http/, 'ws'));
},
ensureConnected: function() {
//readyState:
//CONNECTING 0 The connection is not yet open.
//OPEN 1 The connection is open and ready to communicate.
//CLOSING 2 The connection is in the process of closing.
//CLOSED 3 The connection is closed or couldn't be opened.
if(this.ws && this.ws.readyState > 1) {
this.init();
}
}
};
}
And I always have to call WSData.ensureConnected() when a controller is loading to ensure it's indeed connected... Would be happy if it's instead natively supported.