joewalnes / reconnecting-websocket

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

Can you override open w/o screwing up the library? #18

Closed Jakobud closed 10 years ago

Jakobud commented 10 years ago

I know this isn't an issue or pull request but I don't know where else to ask this question. Sorry.

Normally with my websocket connection I, after a connection is opened, there is various amounts of data that I need to send to the server. I usually do this:

    ws = new WebSocket(url);
    ws.onopen = function()
    {
        ws.send('data to be sent');
    };

That way, I only attempt to send my data after the connection has definitely been made.

However, when I use Reconnecting WebSocket, since it already has the onopen method defined, I assume I shouldn't "redefine" onopen, right? If I can't redefine onopen then what is the best way for me to recognize when the connection has been established so I can do stuff? Use a setInterval() method and check periodically if the connection is made?

Jakobud commented 10 years ago

I just realized that the WebSocket onopen event is passed to the Reconnecting WebSocket onopen method so I can override it without issue.