reconnecting-websocket icon indicating copy to clipboard operation
reconnecting-websocket copied to clipboard

WebSocket is closed before the connection is established

Open nightswimmer opened this issue 12 years ago • 10 comments

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?

nightswimmer avatar Jun 14 '13 21:06 nightswimmer

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

        try
        {
          localWs.close(); 
        }
        catch(err){
            //err.message
        }

yovchev avatar Jul 16 '13 15:07 yovchev

even better check if the readyState is not 3 - CLOSED

        if( ! localWs.readyState === 3 )
        {
          localWs.close();
        } 

yovchev avatar Jul 16 '13 15:07 yovchev

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

localWs.readyState === WebSocket.CLOSED

myadzel avatar Dec 13 '13 22:12 myadzel

Did this solve your problem @nightswimmer ?

nickponline avatar Mar 05 '14 20:03 nickponline

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.

nightswimmer avatar Mar 06 '14 01:03 nightswimmer

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

if (!localWs.readyState === WebSocket.CLOSED)
    localWs.close();

lukevers avatar Apr 08 '14 00:04 lukevers

@lukevers, you have a logical error.

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

myadzel avatar Apr 08 '14 15:04 myadzel

@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.

lukevers avatar Apr 08 '14 16:04 lukevers

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.

kasaiee avatar Sep 21 '17 06:09 kasaiee

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

David-dp- avatar Apr 29 '20 19:04 David-dp-