Skip to content

Commit 5993721

Browse files
authored
Merge pull request #32 from lahsivjar/issue-31
refs #31 Disconnect stomp client on component unmount.
2 parents 12f3d99 + 33274c3 commit 5993721

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-stomp",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "React websocket component with STOMP over SockJS",
55
"main": "./dist/client.js",
66
"scripts": {

src/client.jsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,33 @@ class SockJsClient extends React.Component {
4242
}
4343

4444
componentWillUnmount() {
45-
this.subscriptions.forEach((subid, topic) => {
46-
this.unsubscribe(topic);
47-
});
45+
this.disconnect();
4846
}
4947

5048
render() {
5149
return (<div></div>);
5250
}
5351

52+
_initStompClient = () => {
53+
// Websocket held by stompjs can be opened only once
54+
this.client = Stomp.over(new SockJS(this.props.url));
55+
if (!this.props.debug) {
56+
this.client.debug = () => {};
57+
}
58+
}
59+
60+
_cleanUp = () => {
61+
this.setState({ connected: false });
62+
this.retryCount = 0;
63+
this.subscriptions.clear();
64+
}
65+
66+
_log = (msg) => {
67+
if (this.props.debug) {
68+
console.log(msg);
69+
}
70+
}
71+
5472
connect = () => {
5573
this._initStompClient();
5674
this.client.connect(this.props.headers, () => {
@@ -66,23 +84,26 @@ class SockJsClient extends React.Component {
6684
this.props.onDisconnect();
6785
}
6886
if (this.props.autoReconnect) {
69-
setTimeout(this.connect, this.props.getRetryInterval(this.retryCount++));
87+
this._timeoutId = setTimeout(this.connect, this.props.getRetryInterval(this.retryCount++));
7088
}
7189
});
7290
}
7391

74-
_initStompClient = () => {
75-
// Websocket held by stompjs can be opened only once
76-
this.client = Stomp.over(new SockJS(this.props.url));
77-
if (!this.props.debug) {
78-
this.client.debug = () => {};
92+
disconnect = () => {
93+
// On calling disconnect explicitly no effort will be made to reconnect
94+
// Clear timeoutId in case the component is trying to reconnect
95+
if (this._timeoutId) {
96+
clearTimeout(this._timeoutId);
97+
}
98+
if (this.state.connected) {
99+
this.subscriptions.forEach((subid, topic) => {
100+
this.unsubscribe(topic);
101+
});
102+
this.client.disconnect(() => {
103+
this._cleanUp();
104+
this._log("Stomp client is successfully disconnected!");
105+
});
79106
}
80-
}
81-
82-
_cleanUp = () => {
83-
this.setState({ connected: false });
84-
this.retryCount = 0;
85-
this.subscriptions.clear();
86107
}
87108

88109
subscribe = (topic) => {

test/client.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ describe("<SockJsClient />", () => {
3232
connectSpy.restore();
3333
mountedComponent.unmount();
3434
});
35+
36+
it("Connection is closed on unmount", () => {
37+
const disconnectSpy = sinon.spy(SockJsClient.prototype, "componentWillUnmount");
38+
const mountedComponent = mount(clientTypes.onlyRequired);
39+
mountedComponent.unmount();
40+
expect(disconnectSpy.calledOnce).to.be.true;
41+
disconnectSpy.restore();
42+
});
3543
});

0 commit comments

Comments
 (0)