okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Okhttp-Websocket is not responding to server ping correctly, causing SocketTimeoutException

Open am5a03 opened this issue 11 years ago • 3 comments

I'm implementing a websocket client and encounter this error. The server implements the long-polling protocol, and it seems that after connection has been established for a while, the client will get the SocketTimeoutException and connection is killed

I've tried with another client, AutobahnAndroid, andt it is responding to the server ping correctly and the connection won't be killed.

Here is the stacktrace:

1-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err: java.net.SocketTimeoutException
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at okio.Okio$2.read(Okio.java:139)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at okio.RealBufferedSource.request(RealBufferedSource.java:71)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at okio.RealBufferedSource.require(RealBufferedSource.java:64)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at okio.RealBufferedSource.readByte(RealBufferedSource.java:77)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:108)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:97)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.internal.ws.RealWebSocket.readMessage(RealWebSocket.java:95)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.ws.WebSocketCall.createWebSocket(WebSocketCall.java:171)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.ws.WebSocketCall.access$000(WebSocketCall.java:42)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.ws.WebSocketCall$1.onResponse(WebSocketCall.java:102)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-07 12:57:01.672 2917-2967/dnomyar.layouttest W/System.err:     at java.lang.Thread.run(Thread.java:841)

Here is my code

OkHttpClient c = new OkHttpClient();
        WebSocketCall call = WebSocketCall.create(c, new Request.Builder().url(uri).build());
        call.enqueue(new WebSocketListener() {
            @Override
            public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Response response) {

            }

            @Override
            public void onFailure(IOException e, Response response) {
                e.printStackTrace();
            }

            @Override
            public void onMessage(BufferedSource payload, com.squareup.okhttp.ws.WebSocket.PayloadType type) throws IOException {
                Log.d(TAG, "onMessage: " + payload.readUtf8());
                payload.close();
            }

            @Override
            public void onPong(Buffer payload) {
                Log.d(TAG, "onPong: ");
            }

            @Override
            public void onClose(int code, String reason) {

            }
        });

am5a03 avatar Nov 07 '15 18:11 am5a03

ok, I've found the problem. it's because the server pings at every 10 seconds, which is the same as the default read timeout in okhttp. I increased the read timeout for okhttp to 15 seconds and it's working now.

am5a03 avatar Nov 08 '15 14:11 am5a03

I encounter the same problem ,should always be the server send pings? if i want the client to send ,so i cant use this library?

keepfit avatar Jan 11 '16 09:01 keepfit

I prefer client and server both can send ping pong, I will open another issue to track it.

Dev-Tony avatar Jan 14 '16 08:01 Dev-Tony