File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments