TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

Client doesnt respond with a ping & server doesnt receive messages #1337

Closed JanCantCode closed 1 year ago

JanCantCode commented 1 year ago

Describe what you would like to know or do I have been trying to implement a simple chat system with a few tweaks. The issue that i stumble upon is, that, whilst the connection is established fine, and messages from the server are received on the client just fine, messages sent to the server using WebsocketClient#send are not received on the server. In addition to that, after some period of time the server closes the socket with the client with error code 1006 and the error message The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection. I assume this is just a very stupid mistake in my code tbh.

Describe the solution you'd considered I have assumed that i use some form of while loop in my client that stops it from actually sending anything, whilst handling messages is probaply done in a seperate thread or similar.

Additional context All of this is being run on localhost all Constants used in Code are Strings and only Strings.

my client code:

    public Client() {
        super(URI.create("ws://localhost:8080"));
        connect();
        boolean done = false;
        while (!done) {
            String text = c.nextLine();
            this.send(Constants.ANSWER_INDICATOR + " " + text);
            if (Objects.equals(text, "exit")) done = true;
            }
        }

    public static void main(String[] args) throws InterruptedException {
        Client client = new Client();
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        System.out.println("connected");
    }

    @Override
    public void onMessage(String message) {
        parse(message);
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        System.out.println("closed because " + s + " " + i);
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
    }
}

SERVER CODE


public class Server extends WebSocketServer {
    private final Game game;
    public Server(int port) {
        super(new InetSocketAddress(port));
        this.game = new Game();

        this.start();
    }
    @Override
    public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
        System.out.println("socket connection was opened");
        if (game.onTryJoinPacket(webSocket)) {
            webSocket.close(400, "The game you tried to join was full!");
        }
    }

    @Override
    public void onClose(WebSocket webSocket, int i, String s, boolean b) {
        System.out.println("websocket closed because "+s + " " + i);
    }

    @Override
    public void onMessage(WebSocket webSocket, String s) {
        System.out.println("connection: "+s); // NOTHING IS EVER SPIT OUT HERE
        String type = s.split(" ")[0];
        String field = toString(fromIndex(1, s.split(" ")));

        if (Objects.equals(type, ANSWER_INDICATOR)) {
            game.answer(field);
            System.out.println("answer from client "+ field);
        }
    }

    @Override
    public void onError(WebSocket webSocket, Exception e) {
        e.printStackTrace();
    }
}
JanCantCode commented 1 year ago

resolved it, initializiting the Game class in the constructor of the server started a while loop that wouldnt end ^^