Skip to content
Snippets Groups Projects
Commit 7dad1f1a authored by Mathis Neumann's avatar Mathis Neumann
Browse files

properly disconnect changelogstream if the system changed

parent 64d66c80
No related branches found
No related tags found
No related merge requests found
......@@ -3,19 +3,17 @@ import ENV from 'iobserve-ui/config/environment';
export default Ember.Service.extend({
changelogQueue: Ember.inject.service(),
shouldClose: false,
init() {
this._super(...arguments);
this.debug('session', this.get('systemId'));
},
connect(systemId) {
if(this.get('socket')) {
const oldSocket = this.get('socket');
if(oldSocket) {
this.debug('already connected, disconnecting first');
this.disconnect();
}
this.set('shouldClose', false);
this.debug('setting up websocket', systemId);
const socket = new WebSocket(`${ENV.APP.WEBSOCKET_ROOT}/v1/changelogstream/${systemId}`);
this.set('socket', socket);
......@@ -27,13 +25,11 @@ export default Ember.Service.extend({
// automatically reconnect
if(ENV.APP.WEBSOCKET_RECONNECT) {
socket.onclose = () => {
if(!this.get('shouldClose')) {
this.debug('connection lost, reconnecting!');
this.set('reconnectionTimeout', setTimeout(() => {
this.connect(systemId);
this.set('reconnectionTimeout', null);
}, 500));
}
this.debug('connection lost, reconnecting!');
this.set('reconnectionTimeout', setTimeout(() => {
this.connect(systemId);
this.set('reconnectionTimeout', null);
}, 500));
};
}
......@@ -43,7 +39,12 @@ export default Ember.Service.extend({
disconnect() {
this.get('changelogQueue').reset();
this.debug('disconnect');
this.set('shouldClose', true);
// remove handlers to avoid reconnects and unexpected message handling
this.set('socket.onclose', null);
this.set('socket.onerror', null);
this.set('socket.onopen', null);
this.set('socket.onmessage', null);
this.get('socket').close();
this.set('socket', null);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment