TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.53k stars 2.58k forks source link

How the client handles reconnection #1404

Closed wzgl998877 closed 4 months ago

wzgl998877 commented 8 months ago

My code is as follows `@Slf4j public class DianPingWebSocketClient extends WebSocketClient { @Autowired private IDianPingMessageHandle dianPingMessageHandle;

public DianPingWebSocketClient(URI serverUri) {
    super(serverUri);
}

@Override
public void onOpen(ServerHandshake serverHandshake) {
    log.info("成功连接到大众点评websocket");
}

@SneakyThrows
@Override
public void onMessage(String message) {
    log.info("接收到大众点评消息:{}", message);
    ServerMessage serverMessage = JsonUtils.toBean(message, ServerMessage.class);
    ClientMessage clientMessage = new ClientMessage(serverMessage.getMsg_id(), serverMessage.getType(), "ack");
    dianPingMessageHandle.handleMessage(serverMessage);
    send(JsonUtils.toJson(clientMessage));
}

@Override
public void onClose(int code, String reason, boolean remote) {
    log.error("大众点评websocket断开连接,原因:{}", code);
    reconnect();
}

@Override
public void onError(Exception ex) {
    log.error("大众点评websocket连接异常:", ex);
}

}` I'm thinking now, as a client, how should I handle reconnection?

marci4 commented 4 months ago

I normally trigger a worker to reconnect, when I receive a onClose.