Unable to catch a "connection refused"

This piece of code throws an error which i need it to ignore. What am i doing wrong?

import websockets.*;

WebsocketClient ws;

void setup() {
  size(400, 400);
  
  try {
    ws = new WebsocketClient(this, "ws://localhost:8000/");
  } catch (Exception e) {}
}

Hi @Lednev,

Nothing! The exception handling is done internally by WebsocketClient and not raised to the sketch…
Which error do you see ?

Cheers
— mnse

Here it is:

onError(ConnectException: Connection refused: no further information)
java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at org.eclipse.jetty.io.SelectorManager.finishConnect(SelectorManager.java:337)
	at org.eclipse.jetty.io.ManagedSelector.processConnect(ManagedSelector.java:341)
	at org.eclipse.jetty.io.ManagedSelector.access$900(ManagedSelector.java:56)
	at org.eclipse.jetty.io.ManagedSelector$SelectorProducer.processSelected(ManagedSelector.java:278)
	at org.eclipse.jetty.io.ManagedSelector$SelectorProducer.produce(ManagedSelector.java:170)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:162)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.execute(ExecuteProduceConsume.java:101)
	at org.eclipse.jetty.io.ManagedSelector.run(ManagedSelector.java:136)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
	at java.lang.Thread.run(Thread.java:748)

Is it possible to catch this?

Hi @Lednev,

You not need to catch this. The library is catching this for you and just print that out for debug info.
Means your sketch will proceed even if you got this message on console … just without an established connection … effectively same as ignored …

Cheers
— mnse

The reason i am trying to catch it for is that i need the sketch to continue executing if the server is currently unavailable and when this message is shown it stops. Here’s a version that should continue trying to connect after a “connection refused” if i understand correcly, but it stops after the message.

import websockets.WebsocketClient;

WebsocketClient ws;

void setup() {
  size(400, 400);
  ws = new WebsocketClient(this, "ws://localhost:34563/");
}

void draw() {
  background(255);
  try {
    ws.sendMessage("Hello, WebSocket server!");
  } catch (Exception e) {
    ws = new WebsocketClient(this, "ws://localhost:34563/");
  }
}

hmmm!

Yes! I’ve checked the sources of WebsocketClient and also not understand why this approach for error handling is implemented as it not gives you any useful possibilities to get it managed…

And It is even worst …
As soon the exception occures (even in setup) the animation thread stops working … but keeps the sketch window open, so also no chance to be able to check in the draw loop (ie. if you be able to reconnect) …

So I guess I have no choice but to recommend you to take the code from the github repo and build your own variant of the WebsocketClient module that allows you a better error handling. :man_shrugging:

They are also looking for people which do further maintenance of the project. So this is your chance to make the world a little bit better. :joy:

Cheers
— mnse

Oh, that’s daunting. My experience with websockets and servers in general only includes one tiny project, so i don’t think i’m up for the task. I tried to look at the sources from the github of the library but didn’t understand a thing xD. I guess i’ll try using processing.net with a regular server. Thanks for the help!