Open
Description
Describe the bug
I continuously get a generic error upon attempting to connect with a simple socket.io implemented server; as a client. The error is: server error
.
To Reproduce
Server
Socket.IO server version: unknown
Client
Socket.IO java client version: 2.0.1
public class Callback
{
private final int MAX_RECONNECTION_ATTEMPTS;
private Socket client;
private CountDownLatch awaiter;
public Callback(String url) {
MAX_RECONNECTION_ATTEMPTS = ConfigHandler.getInstance().getWebSocketSettings().getMaxConnectionRetries();
try{
client = IO.socket(
(new URI(url)),
IO.Options.builder().setTimeout(60000).build()
);
} catch (URISyntaxException e) {
System.err.println(e.getMessage());
}
awaiter = new CountDownLatch(1);
}
private void await() {
try{
awaiter.await(client.io().timeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
await();
}
}
private void resetAwaiter() {
awaiter = new CountDownLatch(1);
}
private void _connect() {
//setup lifecycle listeners:
if (!client.hasListeners(Socket.EVENT_CONNECT))
client.once(Socket.EVENT_CONNECT, ((Emitter.Listener)(args) -> {
LOG.info(
"*CaseWorker (" + ConfigHandler.getInstance().getConfig().getTestEnvironment() +
") WEBSOCKET CONNECTED*"
);
awaiter.countDown();
}));
client.once(Socket.EVENT_CONNECT_ERROR, ((Emitter.Listener)(args) -> {
StringBuilder fold = new StringBuilder();
for (Object arg : args) {
if (arg instanceof Exception)
fold.append(System.lineSeparator()).append(((Exception)arg).getMessage());
} LOG.info(
"*CaseWorker ("+ConfigHandler.getInstance().getConfig().getTestEnvironment()+
") WEBSOCKET CONNECTION ERROR*: "+fold.toString()
);
awaiter.countDown();
}));
if (!client.hasListeners(Socket.EVENT_DISCONNECT))
client.on(Socket.EVENT_DISCONNECT, ((Emitter.Listener)(args) -> {
StringBuilder fold = new StringBuilder();
for (Object arg : args) {
if (arg instanceof String)
fold.append(System.lineSeparator()).append((String)arg);
} LOG.info(
"*CaseWorker (" + ConfigHandler.getInstance().getConfig().getTestEnvironment() +
") WEBSOCKET DISCONNECTED*: " + fold.toString()
);
}));
client.open().connect();
//await connection or err:
await();
resetAwaiter();
}
public void connect() throws SocketIOException {
int remainingAttempts = MAX_RECONNECTION_ATTEMPTS;
while ((remainingAttempts > 0)&&(!client.connected())){
LOG.info(
'*'+TestHelpers.ordinalOf(MAX_RECONNECTION_ATTEMPTS - remainingAttempts + 1)+
" ATTEMPT TO ESTABLISH WEBSOCKET WITH CaseWorker ("+
ConfigHandler.getInstance().getConfig().getTestEnvironment()+")* ..."
);
_connect();
--remainingAttempts;
}
if (!client.connected())
throw new SocketIOException(
"*COULD NOT ESTABLISH WEBSOCKET WITH CaseWorker (" +
ConfigHandler.getInstance().getConfig().getTestEnvironment()+
")*");
}
public void awaitCaseUpdated(@NotNull() String caseID) throws SocketIOException {
if (client.connected()) {
client.emit("joinRoom", caseID).
once("joinRoom", ((Emitter.Listener)((msg) -> {
LOG.info("*joinRoom*: "+ Arrays.toString(msg));
//@TODO: assumption that it's the 2nd component
if (msg[1] instanceof String) {
if (((String)msg[1]).contains("caseUpdated"))
awaiter.countDown();
}
})));
await();
resetAwaiter();
} else
throw new SocketIOException("Cannot awaitCaseUpdated as not connected yet");
}
public void disconnect() throws SocketIOException {
if (client.connected())
client.disconnect().close();
else
throw new SocketIOException("cannot disconnect given no connection");
}
}
Then, this class is applied in another class:
//omitted..
//in the constructor:
callback = new Callback(
ConfigHandler.getInstance().getWebSocketSettings().getCarFinanceEndpoint() +
"&token="+getToken()
);
//omitted..
public void awaitCallbackForCase(String caseID) throws SocketIOException {
callback.connect();
callback.awaitCaseUpdated(caseID);
callback.disconnect();
}
Expected behavior
Should, at least, successfully connect.
Platform:
- Device: PC (Lenovo ThinkPad)
- OS: Windows 10
Additional context
I am only trying to 'listen' on 1 event joinRoom
. Then I literally just wait until some new record pattern-matches caseUpdated
.