hobbyquaker / lgtv2

Control LG WebOS TV using node.js :tv:
MIT License
334 stars 44 forks source link

Client connection gets stuck forever #50

Open seizu opened 8 months ago

seizu commented 8 months ago

If you disconnect the LG TV from the power supply and then switch it on again, there is a small time window during the boot-up sequence where a TCP connection can be established. Approx. 7 seconds. If the LGTV2 client establishes the connection to the LG TV exactly during this time, the client hangs forever and blocks any further processes.

Do you have an idea or a workaround to prevent this?

You can test it by pinging the TV during boot-up. If the server responds, try to establish a connection with your LGTV2 client. Important: Before disconnecting the LG TV from the power supply, switch off the TV with the remote control and wait 10 minutes until the pixel cleaning process has been completed. This process runs in the background after switching off the TV.

(Windows ping) c:\>ping 192.168.1.70 -t

Pinging 192.168.1.70 with 32 bytes of data: Reply from 192.168.1.10: Destination host unreachable. <--- Boot up LGTV Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.70: bytes=32 time=5ms TTL=63 <--- Connect here Reply from 192.168.1.70: bytes=32 time=4ms TTL=63 Reply from 192.168.1.70: bytes=32 time=5ms TTL=63 Reply from 192.168.1.70: bytes=32 time=5ms TTL=63 Reply from 192.168.1.70: bytes=32 time=5ms TTL=63 Reply from 192.168.1.70: bytes=32 time=5ms TTL=63 Request timed out. Request timed out. Request timed out. Request timed out. Request timed out. Request timed out. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable. Reply from 192.168.1.10: Destination host unreachable.

seizu commented 8 months ago

After further research, I found out that it is more likely to be a websocket related issue which is described here. Websocket-Node No timeout

A fix for LGTV2 might look like this...

In the constructor, pass a user-defined parameter for the connection timeout, e.g. conTimeout

var lgtv2 = require("lgtv2");
var lgtv =  new lgtv2({url: `${wsurl}`, keyFile : 'keyFile', reconnect: 5000, wsconfig:{'conTimeout':5000, ... });
...

WebSocketClient.js (line 254)

    var req = this._req = (this.secure ? https : http).request(requestOptions);    
+    req.on('socket', function (socket) {
+       socket.setTimeout(self.config['conTimeout']);  
+       socket.on('timeout', function() {
+           req.destroy();
+      });
+   });

WebSocketClient.js (line 87) extend the config

+      conTimeout: 5000,

Edited: The previous solution did not work! I had to patch the WebSocketClient.js after all :(