feross/chrome-dgram

create and bind should not be executed within the same method

thomasdelaet opened this issue · 4 comments

I get an error trying to use setMulticastTTL and setMulticastLoopback methods.

As far as I can see, the reason for this is that both methods need to be excuted in chrome after create method is called (since you need SocketID) but before bind (since you can not execute these on a connected socket).

In the current chrome-dgram implementation, create and bind are executed in the same method.

I would propose to put create in the constructor. What I'm not able to figure out is if it's possible to wait untill the callback function of the create method is called before returning in the constructor.

Thanks for catching this. I've never used the setMulticastTTL or setMulticastLoopback methods, so I'm not surprised that they don't work. I don't use this library anymore, but I'll attempt a quick, untested fix for this.

You can't wait until the create callback function is called before returning from the constructor. Constructors are synchronous. The easiest way to accomplish this is probably to detect whether the socket is connected in the multicast methods and if not, wait until that happens before executing the method.

Going forward, I'm happy to add you as a GitHub collaborator and npm owner for this package if you send a PR or two that are good.

Great suggestion!

I'm happy to send a PR for this in the next days

Attempted to fix this (seems to work) and released it as 2.1.5.