Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for websocket connection to send a message #98

Open
fabioDMFerreira opened this issue Jun 8, 2018 · 1 comment
Open

Wait for websocket connection to send a message #98

fabioDMFerreira opened this issue Jun 8, 2018 · 1 comment

Comments

@fabioDMFerreira
Copy link

fabioDMFerreira commented Jun 8, 2018

I noticed that my application breaks on sending a message while websocket still be connecting to the server. This can be handled in application side, but I think it would be good to have some timer or have a queue of messages to send on reconnecting in this library side. This isn't elegant but here it is what I did to solve the problem.

this.send = function (data) {
        if (ws) {
            if (self.debug || ReconnectingWebSocket.debugAll) {
                console.debug('ReconnectingWebSocket', 'send', self.url, data);
            }
            //if socket is connecting wait 500ms to retry send the message
            if(ws.readyState === 0){
                return setTimeout(()=>this.send(data),500);
            }
            return ws.send(data);
        } else {
            throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
        }
    };
@qianlongo
Copy link

@fabioDMFerreira That way may not solve the problem completely. After 500 milliseconds, the WS state may not be 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants