thibauts / node-castv2

An implementation of the Chromecast CASTV2 protocol
MIT License
768 stars 100 forks source link

When socket cannot connect, the close tried to use self.ps which was not #23

Closed angelnu closed 8 years ago

angelnu commented 8 years ago

Fix uncaught exception on client.close() when socket did not connect

Client error: {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"192.168.2.106","port":33009}

2015-12-31 10:55:49.687  - error: uncaught exception: Cannot read property 'removeListener' of null
2015-12-31 10:55:49.708  - error: TypeError: Cannot read property 'removeListener' of null
    at TLSSocket.onclose (/opt/iobroker/node_modules/iobroker.chromecast/node_modules/castv2-client/node_modules/castv2/lib/client.js:53:12)
    at emitOne (events.js:82:20)
    at TLSSocket.emit (events.js:169:7)
    at TCP._onclose (net.js:469:12)
---------------------------------------------
    at Client.connect (/opt/iobroker/node_modules/iobroker.chromecast/node_modules/castv2-client/node_modules/castv2/lib/client.js:44:15)
    at PlatformSender.connect (/opt/iobroker/node_modules/iobroker.chromecast/node_modules/castv2-client/lib/senders/platform.js:27:15)
thibauts commented 8 years ago

First thank you for your PR. For clarification before merging : are you positive no error event is triggered on ECONNREFUSED ? Do you get only a close event ? I think that should be signaled as an error at a lower level ... This leaves me wondering.

angelnu commented 8 years ago

Yes, the error is reported (I use it to print a log and trigger a delayer reconnect) but a close event is triggered as well.

I even tried to register for errors with on("error",cb) instead of once("error", cb) with the hope of capturing the exception in the client close handler but this did not work. I am new to Javascrip so maybe there is another way for the code using the client to capture the exception but I did not find any...

Thanks for the quick response!

thibauts commented 8 years ago

Ok cheched. Please use curly brackets in your if to match the global style. Can you also nullify ps only if it not already null ? Grouping ps handling this way would clarify the code for a newcomer.

thibauts commented 8 years ago

Published to npm \o/ thanks for your PR :)