diff --git a/src/DeviceRenderer.js b/src/DeviceRenderer.js index 689a9d2..f628e09 100644 --- a/src/DeviceRenderer.js +++ b/src/DeviceRenderer.js @@ -876,14 +876,20 @@ module.exports = class DeviceRenderer { * This method also calls recursively the destroy methods on the plugins if they exist. */ destroy() { - this.removeAllListeners(); - this.disconnect(); - this.peerConnectionStats?.destroy(); - delete this.peerConnectionStats; - delete this.video; - delete this.wrapper; - delete this.videoWrapper; - delete this.root; - delete this.stream; + /** + * if the websocket is in connecting state, + * we can't destroy the instance cause object is not fully initialized + */ + if (this.webRTCWebsocket.readyState !== 0) { + this.removeAllListeners(); + this.disconnect(); + this.peerConnectionStats?.destroy(); + delete this.peerConnectionStats; + delete this.video; + delete this.wrapper; + delete this.videoWrapper; + delete this.root; + delete this.stream; + } } };