nv-websocket-client icon indicating copy to clipboard operation
nv-websocket-client copied to clipboard

Closing a web socket while in the process of connecting

Open barelyreal opened this issue 8 years ago • 7 comments

Hi, I've recently run into a problem where I need to close the web socket before it has finished connecting. Looking at the code in Websocket.disconnect() reveals this:

        switch (mStateManager.getState())
        {
            case CREATED:
                finishAsynchronously();
                return this;

            case OPEN:
                break;

            default:
                // - CONNECTING
                //     It won't happen unless the programmer dare call
                //     open() and disconnect() in parallel.
                //
                // - CLOSING
                //     A closing handshake has already been started.
                //
                // - CLOSED
                //     The connection has already been closed.
                return this;
        }

What I'm doing currently is to get the raw socket and call close() on it, which generally works, but sometimes doesn't. Waiting for the OPEN to occur is another option, but if the network is really slow, it's really not ideal for my purposes. Can we add some behavior to cancel the connection if it's in progress?

barelyreal avatar Mar 11 '16 10:03 barelyreal

Isn't WebSocketFactory.setConnectionTimeout(int) applicable to your case?

TakahikoKawasaki avatar Mar 11 '16 10:03 TakahikoKawasaki

Not in this case, because sometimes I have to immediately shut down all connections in response to other input.

barelyreal avatar Mar 11 '16 10:03 barelyreal

Calling close() on the raw socket closes the raw socket. What does "sometimes doesn't (work)" mean? What do you expect to happen after close()? I'm sorry I don't understand well what behavior you expect yet.

TakahikoKawasaki avatar Mar 11 '16 11:03 TakahikoKawasaki

Ah, sorry, when calling close(), the WebSocket object will sometimes continue to open (perhaps the socket hasn't been opened yet), and sometimes will fail to send a CLOSING and CLOSED state.

barelyreal avatar Mar 11 '16 11:03 barelyreal

Thank you. I understood what you meant. Give me some time to think about it.

TakahikoKawasaki avatar Mar 11 '16 11:03 TakahikoKawasaki

@TakahikoKawasaki So did you think about it since that day? 😉

AFAIK these lines

               //     It won't happen unless the programmer dare call
               //     open() and disconnect() in parallel.

are not true because there's also async open method, and there are very valid reasons to do disconnect call while the connection is still opening — for example if user decided to abort the action which required the WS connection, and the only logical thing to do at this point is to terminate the WS connection. And it could be in "opening" state at this moment, if the user is sufficiently fast or the network is sufficiently slow.

sp-1234 avatar Apr 10 '17 07:04 sp-1234

Any progress on this? Why can't we close the connection while the state is CONNECTING? Is there any reason I don't understand yet?

UPDATE 14/12/2022: This is a workaround until this issue fixed. I think the correct implementation would be sending close signal to the polling thread, so that it can stop itself whenever it is convenient.

private lateinit var mClient: WebSocket
...
fun close() {
      val client = mClient;
      CoroutineScope(Dispatchers.IO).launch {
          while (client.state == WebSocketState.CONNECTING) {
              delay(10)
          }
          client.disconnect()
      }
}

wachidsusilo avatar Dec 14 '22 08:12 wachidsusilo