OkHttp WebSocket ping 和 pong

OkHttp WebSocket ping and pong

我正在使用 okhttp-4.9.3 创建一个连接到 Binance 的 websocket。

在他们的文档中,他们说他们每 3 分钟发送一次 ping,我需要回复它。

但是我不知道怎么做。

我有这个 WebSocketListener:

public static void main(String[] args){

    Request request = new Request.Builder()
                .url("wss://stream.binance.com:9443/ws")
                .build();

    webSocketBinance = okHttpClient.newWebSocket(request, new SocketListen);

    webSocketBinance.send("{\"method\":\"SUBSCRIBE\",\"params\":[\"egldusdt@depth5@100ms\"],\"id\":1}");

}


public static class SocketListen extends okhttp3.WebSocketListener{

    @Override
    public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response){
        System.out.println("onOpen");
        super.onOpen(webSocket, response);
    }

    @Override
    public void onMessage(@NotNull WebSocket webSocket, @NotNull String text){
        System.out.println(text);
        super.onMessage(webSocket, text);
    }

    @Override
    public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes){
        onMessage(webSocket, bytes.toString());
    }

    @Override
    public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason){
        System.out.println("onClosed: " + code + " - " + reason);
        super.onClosed(webSocket, code, reason);
    }

    @Override
    public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason){
        System.out.println("onClosing: " + code + " - " + reason);
        super.onClosing(webSocket, code, reason);
    }

    @Override
    public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response){
        t.printStackTrace();
        super.onFailure(webSocket, t, response);
    }

}

我没有方法 pingpong 可以覆盖 okhttp3.WebSocketListener

我该怎么做?

OkHttp 自动为您响应 ping。您无需执行任何操作。