i got a little problem with my project. I have a Server written in Java and some clients written in html/js. Connecting works somehow, but as soon as i want to send a message from the client to the server it returns an error: "Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state"
Hopefully some of you awesome guys can look over my code and help me :)
Server Code:
Server.java
public class Server {
static ArrayList<Clients> clientsArrayList = new ArrayList<>();
private static int clientCount = 1;
private static int port;
private static ServerSocket ss;
private Socket socket;
private Clients clienthandler;
static boolean isRunning = true;
public Server(int port) throws IOException {
    this.port = port;
    setSs(new ServerSocket(port));
}
public void run() throws IOException {
    while (isRunning) {
        log("Listening on " + port + "...");
        socket = getSs().accept();
        log("Receiving client... " + socket);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));        
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        String s;
        while ((s = in.readLine()) != null) {
            log(s);
            if (s.isEmpty()) {
                break;
            }
        }
        log("Creating a new handler for this client...");
        clienthandler = new Clients(socket, "Client " + clientCount, in, out);
        Thread t = new Thread(clienthandler);
        clientsArrayList.add(clienthandler);
        log("Added to client list");
        t.start();
        clientCount++;
        GUI.texttoclientlog();
    }
}
public static ServerSocket getSs() {
    return ss;
}
public static void setSs(ServerSocket ss) {
    Server.ss = ss;
}
public void log(String logtext) {
    System.out.println(logtext);
    GUI.texttolog(logtext);
}
}
Clients.java
public class Clients implements Runnable {
private String name;
final BufferedReader in;
final PrintWriter out;
Socket socket;
boolean isloggedin;
public Clients(Socket socket, String name, BufferedReader in, PrintWriter out) {
    this.out = out;
    this.in = in;
    this.name = name;
    this.socket = socket;
    this.isloggedin = true;
}
@Override
public void run() {
    String received;
    while (true) {
        try {
            // receive the string
            received = in.readLine();
            System.out.println(received);
            GUI.messagehandler(this.getName() + ": " + received);
            if (received.equals("logout")) {
                this.isloggedin = false;
                this.socket.close();
                break;
            }
            this.in.close();
            this.out.close();
            this.out.flush();
            this.out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public String getName() {
    return name;
}
}
And the JS client code:
    <script>
  var connection;
  connection = new WebSocket("ws://localhost:6788/");
  console.log("connection established");
  connection.onmessage = function (e) { console.log(e.data); };
  connection.onopen = () => conn.send("Connection established");
  connection.onerror = function (error) {
    console.log("WebSocket Error" + error);
  };
  function Send() {
    if (connection.readyState === 1) {
      connection.send("test");
    }
        console.log("error sending");
    }
</script>
 
     
    