joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
MIT License
4.21k stars 968 forks source link

Added Passable options #32

Closed Jakobud closed 9 years ago

Jakobud commented 9 years ago

I changed settings to an object, and added passable options on instantiation. This allows someone to define settings immediately upon instantiation instead of after. It works like so:

socket = new ReconnectingWebSocket(url, null, {debug:true, reconnectInterval:10000});

The settings object compared to the options and then added to the object. Options that don't exist in the settings will be ignored. So for example, if you passed an option of "hello":"world", it would be ignored, since there is no default setting called "hello". The options are completely optional. You can still change these settings after the fact, just like you can now:

socket = new ReconnectingWebSocket(...);
socket.debug = true;

Therefore, backwards compatibility is preserved with this pull request.

Overall this change will allow some more flexibility in the future. For example, this will allow an "automaticOpen" option to be passed, as previously discussed. Some other things to consider is this approach would open the door for a change that allows event handlers to be passed through these options as well. So for example, you could eventually do:

socket = new ReconnectingWebSocket(url, null, {
    debug:true,
    reconnectInterval:5000,
    onconnecting: function(){
        // some code
    }
});

That approach would fix the previously discussed issue about the socket connecting before the onconnecting event handler can be defined. This pull request does not allow you to do this, but it opens the door for this approach to be implemented at a later time.

Jakobud commented 9 years ago

This pull request opens the door to address both of the problems discussed in #30.

joewalnes commented 9 years ago

Nice.

Can you add some documentation explaining this to README and the JS header comments.

Jakobud commented 9 years ago

Done. I added quite a bit to the docs.

joewalnes commented 9 years ago

Thanks!