joewalnes / reconnecting-websocket

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

WebSocket is closed before the connection is established #6

Open nightswimmer opened 11 years ago

nightswimmer commented 11 years ago

Hello. I get this error "WebSocket is closed before the connection is established." when I try to connect and the server is inacessible. The error seems to come from the line: localWs.close(); when localWs.readyState has the value 0

Is this error normal/unavoidable?

yovchev commented 11 years ago

I think need to be put in try catch so don't trigger the error

        try
        {
          localWs.close(); 
        }
        catch(err){
            //err.message
        }
yovchev commented 11 years ago

even better check if the readyState is not 3 - CLOSED

        if( ! localWs.readyState === 3 )
        {
          localWs.close();
        } 
myadzel commented 10 years ago

@daniel3d, better use the defined constant for this cases:

localWs.readyState === WebSocket.CLOSED

nickponline commented 10 years ago

Did this solve your problem @nightswimmer ?

nightswimmer commented 10 years ago

I used the try/catch and it seemed to work. But now in some situations it doesn't seem to reconnect, don't know if it could be because of that.

lukevers commented 10 years ago

I was having the same problem and doing this fixed it:

if (!localWs.readyState === WebSocket.CLOSED)
    localWs.close();
myadzel commented 10 years ago

@lukevers, you have a logical error.

In expression like "!a === b" variable "a" turns to boolean, when in expression "a !== b" all good.

lukevers commented 10 years ago

@myadzel haha, thanks for point that out! I saw @daniel3d's response and then your response to his to use Websocket.CLOSED instead of 3 and I just threw it all together without thinking about it.

kasaiee commented 6 years ago

Hello all dear. I am pretty new in WebSocket. I have this problem too. This is my javascript to test connecting to server:

$(function () {
    // Correctly decide between ws:// and wss://
    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
    var ws_path = ws_scheme + '://' + window.location.host + "/chat/stream/";
    console.log("Connecting to " + ws_path);
    var socket = new ReconnectingWebSocket(ws_path);

    // Helpful debugging
    socket.onopen = function () {
        console.log("Connected to chat socket");
    };
    socket.onclose = function () {
        console.log("Disconnected from chat socket");
    }
});

but this is the result that printed in Chrome developer console: WebSocket connection to 'ws://127.0.0.1:8000/chat/stream/' failed: WebSocket is closed before the connection is established please help! thanks a lot.

David-dp- commented 4 years ago

I have the same problem as kasaiee when opening a wss url using the minified library, which I downloaded from this project just 2 months ago.

The connection later becomes usable, so I'd like to avoid the false alarms that this triggers in our reporting system.

Update: This problem occurs every time I use recent Chrome on Win10 to make an initial wss connection, but when I try Safari/iOS 13.1 it doesn't occur.

I submitted this PR: https://github.com/joewalnes/reconnecting-websocket/pull/112/commits/63361538d6be9ebffdb255b2140cb60fec21b2f9