Skip to content

Commit

Permalink
fix(VncConsole): disconnect VNC session when unmounting (patternfly#1123
Browse files Browse the repository at this point in the history
)

The VNC session is properly disconnected when unmounting the VncConsole component. This fixes
memory-leak and asynchronously fired events to an unmounted component.
  • Loading branch information
mareklibra authored and jeff-phillips-18 committed Jan 7, 2019
1 parent 758b23f commit b7df8dc
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/patternfly-3/react-console/src/VncConsole/VncConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const { noop } = helpers;
class VncConsole extends React.Component {
state = { status: CONNECTING };

addEventListeners() {
this.rfb.addEventListener('connect', this.onConnected);
this.rfb.addEventListener('disconnect', this.onDisconnected);
this.rfb.addEventListener('securityfailure', this.onSecurityFailure);
}

componentDidMount() {
const {
host,
Expand Down Expand Up @@ -45,9 +51,7 @@ class VncConsole extends React.Component {
};

this.rfb = new RFB(this.novncElem, url, options);
this.rfb.addEventListener('connect', this.onConnected);
this.rfb.addEventListener('disconnect', this.onDisconnected);
this.rfb.addEventListener('securityfailure', this.onSecurityFailure);
this.addEventListeners();
this.rfb.viewOnly = viewOnly;
this.rfb.scaleViewport = false; // if the remote session is smaller than HTML container, the view will be centered
this.rfb.resizeSession = resizeSession;
Expand All @@ -57,6 +61,19 @@ class VncConsole extends React.Component {
}
}

componentWillUnmount() {
this.removeEventListeners();
this.disconnect();
}

disconnect() {
if (!this.rfb) {
return;
}
this.rfb.disconnect();
this.rfb = undefined;
}

onConnected = () => {
this.setState({ status: CONNECTED });
};
Expand All @@ -78,6 +95,12 @@ class VncConsole extends React.Component {
this.props.onSecurityFailure(e);
};

removeEventListeners() {
this.rfb.removeEventListener('connect', this.onConnected);
this.rfb.removeEventListener('disconnect', this.onDisconnected);
this.rfb.removeEventListener('securityfailure', this.onSecurityFailure);
}

setNovncElem = e => {
this.novncElem = e;
};
Expand Down

0 comments on commit b7df8dc

Please sign in to comment.