Skip to content

Commit 88b2e68

Browse files
committed
prevent issue with multiple start/stop calls on same device
1 parent 1411439 commit 88b2e68

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Of course I'm also very glad about issues and pull requests if you find a bug /
173173
Placeholder for next versions (this needs to be indented):
174174
### __WORK IN PROGRESS__
175175
-->
176+
### __WORK IN PROGRESS__
177+
* prevent issue with multiple start/stop calls on the same device.
178+
176179
### 0.5.4 (2023-04-28)
177180
* fix 'Server sent no subprotocol' error and ignore ts warnings.
178181

index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,20 @@ class WebSocketClient extends EventEmitter.EventEmitter {
140140
* Ends socket connection
141141
*/
142142
disconnect() {
143-
if (this._device.socket) {
144-
this._device.socket.close();
145-
setTimeout(this._device.socket.terminate, 500); //force close after some time.
143+
const socket = this._device.socket; //keep copy of socket here. Might want to reconnect, then terminate is not good, might be called on new socket.
144+
if (socket && typeof socket.close === 'function') {
145+
socket.close();
146+
this._device.debug('Socket closing, demanded by calling diconnect()');
147+
setTimeout(() => {
148+
if (socket && socket.terminate === 'function') {
149+
try {
150+
socket.terminate();
151+
this._device.debug('Socket terminated, demanded by calling diconnect()');
152+
} catch (e) {
153+
this._device.debug('Could not terminate socket: ' + e.stack);
154+
}
155+
}
156+
}, 500); //force close after some time
146157
}
147158
if (this._device.pingHandler) {
148159
clearTimeout(this._device.pingHandler);

0 commit comments

Comments
 (0)