thibauts / node-castv2

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

Propagate bus 'close' #21

Closed Ivshti closed 8 years ago

Ivshti commented 8 years ago

This may be related to the other 2 pending PRs, but seems to be clean and to make sense on all levels - and most importantly, address the root of the issue.

Here's what happens: sometimes, the connection closes. The lib/client Client instance would handle it properly, calling it's onclose, which nulls .ps.

Then all sorts of hell would break loose as various things would try to call castv2 methods, but .ps is null.

The solution - propagate the close up the chain, so that whoever uses castv2 and castv2-client will know the connection has closed and handle it properly.

Ivshti commented 8 years ago

This does not solve the root cause of the issue, so I'm withdrawing it

thibauts commented 8 years ago

Hi @Ivshti, thanks for your PR in any case. What would solve it ? How does your PR does not solve it ? This is a recurring issue so whatever the way, I'd like to provide a solution for users.

Ivshti commented 8 years ago

@thibauts it seems that one didn't need propagation, as the upper level got the close event. I don't know how exactly, but it does.

However, on the topmost level in node-castv2-client, the close isn't propagated from this.client to this.

So if you open a client using node-castv2-client, you can do: client.client.on('close') and still listen to close. Even though client.on('close') would be better, I didn't PR before fixing the issue entirely - which is, chromecast disconnecting after some minutes of inactivity, after which it's unusable.

What @mafintosh did in his mafintosh/chromecasts module is a re-connection logic, which simply initializes a new client on the same address. The problem with that is that there is some kind of state somewhere which I cannot figure out - after re-connecting, I get flooded with errors, one of them cannot read 'mediaSessionId' of undefined, the others related to data serialization.

I will post more details once I look into it