我有一个交互式绘画应用程序托管在paint.unubo.cloud上,每当我尝试使用处理和Too Tall Nate的Java Websocket库连接到该应用程序时,它都会立即以断开连接代码-1断开连接。关于-1的含义,我什么也找不到,我也不知道如何解决。这是我的代码:
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class WebPaint extends PApplet {
Client client;
boolean isOpen = false;
int x = -50;
int y = -50;
public void setup() {
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);
}
}
}
public void settings() { size(600, 600); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "WebPaint" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}