square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Home Page:https://square.github.io/okhttp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Websocket unable to prompt correctly after network disconnection

Jackjun724 opened this issue · comments

Bug Code

    private static final OkHttpClient CLIENT = new OkHttpClient().newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .readTimeout(Duration.ofSeconds(10))
            .callTimeout(Duration.ofSeconds(10))
            .writeTimeout(Duration.ofSeconds(10))
            .build();
    
    CLIENT.newWebSocket(myRequest, myWebSocketListener)

When I try to shut down the local network(wifi), the both onClosed and onFailure event of the WebSocketListener is not triggered. I have been disconnected for more than 2 minutes. When I use the wscat cli for testing, it returns the 1006 status code after a period of time and shuts down. I try to set the timeout in the OkHttpClient. It has no effect. I cannot know that my websocket connection has been lost because of network problems, It makes me miserable.

What's more sad is that once after disconnecting the connection for a certain period of time, I couldn't get its status. He didn't normally receive websocket messages, but no errors were thrown, and no closed and failure events were triggered.

My OkHttp version is 4.9.3, my platform is macOS 12.2.1(M1)

I realized the network outage detection by configuring pingInterval.

Is it working with that set?

Is it working with that set?

OkHttpClient CLIENT = new OkHttpClient().newBuilder()
            .pingInterval(Duration.ofSeconds(15))
            .build()

It will trigger onFailure 15 seconds after the network is disconnected`. If you don't set it, it will enter an unexpected state when the network is disconnected. I think maybe it should have a default value.