The WebSocket CN1Lib offers the method autoReconnect(int time) that helps to keep the connection alive... but the drawback is that I cannot close the connection. Is there something wrong in my code that I can fix or is it a bug of the library?
On the server-side, there is a timer that send a WebSocket text message every second to the client with id "1234". It's only a test, I mostly copied that server code from the Codename One Academy, so I assume that it's correct.
On the client-side, I wrote this test case:
String uniqueId = "1234";
Form hi = new Form("WebSocket Test", BoxLayout.y());
hi.show();
final WebSocket sock = new WebSocket("ws://localhost:8080/wsMsg") {
@Override
protected void onOpen() {
Log.p("WebSocket onOpen - Sending uniqueId: " + uniqueId);
hi.add("WebSocket onOpen - Sending uniqueId: " + uniqueId);
hi.animateLayout(400);
send(uniqueId);
}
@Override
protected void onClose(int statusCode, String reason) {
Log.p("WebSocket onClose");
hi.add("WebSocket onClose");
hi.animateLayout(400);
}
@Override
protected void onMessage(String message) {
Log.p("WebSocket onMessage: " + message);
hi.add(new SpanLabel("WebSocket onMessage: " + message));
hi.animateLayout(400);
}
@Override
protected void onMessage(byte[] message) {
Log.p("WebSocket onMessage (byte[]): " + message.toString());
}
@Override
protected void onError(Exception ex) {
Log.e(ex);
hi.add(new SpanLabel("WebSocket onError:\n" + ex.getMessage()));
}
};
sock.connect();
sock.autoReconnect(5000);
UITimer.timer(5000, false, hi, () -> {
sock.close();
});
The output is:
As you can see, after closing the WebSocket connection, autoReconnect reopened it. The expected behavior is that autoReconnect must reconnect only on network errors, and not when close is explicitly invoked.
