Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WebSocket SocketTimeoutException adding onFailure on KucoinAPICallback #56

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

denistorresan
Copy link

While socket running gets the exception below, The socket can work for a maximum of 1 day without any errors:

2022-06-11 13:03:47.876 ERROR 22068 --- [.kucoin.com/...] c.k.s.w.l.KucoinPublicWebsocketListener  : Error on private socket

java.net.SocketTimeoutException: sent ping but didn't receive pong within 30000ms (after 225 successful ping/pongs)
        at okhttp3.internal.ws.RealWebSocket.writePingFrame(RealWebSocket.java:545)
        at okhttp3.internal.ws.RealWebSocket$PingRunnable.run(RealWebSocket.java:529)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)

I fixed adding the KucoinAPICallback.onFailure method, that allow to manage a reconnection programmatically, please look in the tests on KucoinPublicWSClientTest and KucoinPrivateWSClientTest for a reference implementation.
I also updated the Readme.MD with new code, here an example:

public void onLevel2Data() throws Exception {
        kucoinPublicWSClient.onLevel2Data(new KucoinAPICallback<KucoinEvent<Level2ChangeEvent>>() {

			@Override
			public void onResponse(KucoinEvent<Level2ChangeEvent> response) throws KucoinApiException {
	            event.set(response.getData());
	            kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2, "ETH-BTC", "KCS-BTC");
	            gotEvent.countDown();
			}

			@Override
			public void onFailure(Throwable cause) {
				System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());

				//reinitializeWSConnection();	//implement this method
			}
        	
        }, "ETH-BTC", "KCS-BTC");
}

the reinitializeWSConnection() may be something like this:

private static KucoinPublicWSClient kucoinPublicWSClient;

public reinitializeWSConnection() {
        kucoinPublicWSClient = new KucoinClientBuilder().withBaseUrl("https://openapi-sandbox.kucoin.com")
                .buildPublicWSClient();

       onLevel2Data();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants