Websockets not working with java version

#1

I have a networked paint app hosted at paint.unubo.cloud. I am using socket.io for the networking. Whenever I run the app on a localhost, everything works just fine, but when I try to connect to the app on unubo, the socket client immediately closes with code -1. I am using Too Tall Nate’s Java Library (https://github.com/TooTallNate/Java-WebSocket). Here is the client code:

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;

Client client;
boolean isOpen = false;
int x = -50;
int y = -50;

public void setup() {
  size(600, 600);
  background(100);
  fill(0);
  try {
    client = new Client(new URI("wss://paint.unubo.cloud/socket.io/?EIO=3&transport=websocket"));
    client.connect();
  } 
  catch(Exception e) {
    println(e);
  }
}

public void draw() {
  ellipse(x, y, 10, 10);
}

public void mouseDragged() {
  if (isOpen) {
    client.send("42[\"message\",{\"x\":" + mouseX + ",\"y\":" + mouseY + "}]");
  }
}

public void keyPressed() {
  client.connect();
}
class Client extends WebSocketClient {
  public Client(URI serverURI) {
    super(serverURI);
  }
  public void onError(Exception e) {
    println(e);
  }
  public void onClose(int reason, String details, boolean remote) {
    println("Closed: " + reason + " " + details);
    isOpen = false;
    exit();
  }
  public void onOpen(ServerHandshake hs) {
    println("Connected");
    isOpen = true;
  }
  public void onMessage(String message) {
    if (message.length() > 2 && message.substring(0, 2).equals("42")) {
      String data = message.substring(13, message.length() - 1);
      JSONObject json = JSONObject.parse(data);
      println(json.toString());
      x = json.getInt("x");
      y = json.getInt("y");
      ellipse(json.getInt("x"), json.getInt("y"), 10, 10);
    }
  }
}

Can anyone help?

Also posted on stack overflow:

https://stackoverflow.com/questions/55263235/websockets-disconnecting-with-code-1-with-too-tall-nate-java-library

1 Like

#2

Does anyone at least know what it means when websockets close with code -1?

0 Likes

#3

just curious why you use

wss://_host_/socket.io/?EIO=3&transport=websocket

where you get that example from?

and not like in the example
https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/SSLClientExample.java ,

wss://_host_:8887

using a PORT

1 Like

#4

I was using the processing websockets library, and in the github issues for that, it tells you to use that to connect to a socket.io server. I switched because that library didn’t support wss. Also, I don’t know the port number. It’s like heroku.

0 Likes