Network server handler for disconection (mostly a question for @pspeed since he made it)

I m trying to get when a player leave the server and was wondering if my code was the problem or if i misread and the code I’m using isn’t suppose to do this.
I m refering to this function public void connectionRemoved(){}

Maybe it possible i close the server too fast and it doesn’t have the time to execute the disconnection?

public class ServerHandler implements ConnectionListener {

    private ServerInstance serverInstance;
    private EntityServerPool entityServerPool;

    public ServerHandler(ServerInstance serverInstance) {
        this.serverInstance = serverInstance;
    }

    @Override
    public void connectionAdded(Server server, HostedConnection conn) {
       
    }

    @Override//this code
    public void connectionRemoved(Server server, HostedConnection conn) {
        System.out.println("Disconnect Client");
        //conn.close("Disconect"); Edited since it not supose to be there haha.
    }

}

Did you add this listener to your Server instance?

@Override//this code
    public void connectionRemoved(Server server, HostedConnection conn) {
        conn.close("Disconect");
    }

This method will be executed when a connection already has been closed from the server. So, there is not really a sense to “close” a closed connection. You might overthink your code one more time, maybe.

lol yea i know, i mean it s a mistake i should have remove this line. I m trying to get when some one leave the server on the client side. But it seem to never trigger for the client hosting, I was wondering if it was my code the problem since it never trigger maybe i close the server too fast.

And yes the class as been added to the server listener!

Actually I m trying to save the player character on the server file but since it never go there I lose everything. I forgot to test if an external client connected was saving correctly… I will do the test then come back and delete the topic if my code is the problem.
(My bad for all the spell and grammar mistake, seem like I can’t spell)

Like i though, the connectionRemoved never run when a client cleanly disconnect. I though it was suppose to be run when a disconnection occur (any kind). Must have misunderstood the usage ^^ :stuck_out_tongue:.

connectionRemoved() should be called for any disconnect, regardless of how it happened. It’s called on the server… and it’s definitely called no matter how the user disconnects or I have a whole bunch of broken code in my network games.

Haha then i did something wrong some where! Since my game is broken and cant save the player when they leave xD haha .

Try putting together a simple test case and see if it happens there.

Yea i just did it, it seem everything work fine. Only thing not working was the client.send(“Message Position”);

Really tough for me to help debug code that I can’t see… but did you really wrap it in a message or just try to send a raw string? Did you wait until the client was fully started? (like with the client state listener?)

It s an Object, it was to make it more readable so i put a String. But I don’t know why but the server seem to close to fast and my client hosting the server seem to be faster at closing the server and the character doesn’t get saved at all sadly i will work on a function to make sure everything is saved when the host exit and the server is closed.

I don’t think i could send any readable code at the moment haha. But thx for the small tip I think it s all my code the problem here, only me can do something about it :stuck_out_tongue: .

If you put together a simple test case then the code should be more readable… or enough to see what the problem is.

And yea the thing about the client to be fully start… I didn’t know at first, my server was always crashing while sending to unready client. It took me quite a while to understand there was a StateManager to verify if a client is ready to receive :open_mouth: .

As soon as the server is notified about a connection being added then you can start sending to it.

Only after receiving the clientConnected() call on a Client can you start sending messages back.

The internal state of the connection handling is setup this way. The client should have already added all of its listeners before calling start(). The server knows this. The client knows this. The connection is fully setup before these listener methods are called.

Are you sure about this? When i do Server.Broadcast(Message);
I get plenty of error if it goes to fast when a client get connected :sweat:

I’m kind of done with this thread until you start providing more information.

“Errors” is 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000% useless to me.

It s hard to give the actual error if it doesn’t seem to appear anymore after I’ve rethink the connection process.

If you want to know it was happening when:

new Thread(){
    Server server = new Server();
    do{
       server.broadcast(Message);
   }
}
   //Serialize stuff
   Client client = new Client();
   client.start();

The server crash from sending to a Client not yet ready
That was the error i was getting. I can’t specify anymore since I remove the client from the update loop until he send a message first once it is fully ready.

By update loop i mean i did a list with all the fully connect client.

    new Thread(){
        Server server = new Server();
        server.start();
        do{
           //server.broadcast(Message);
           for(HostedConnection source : list){
               source.send(Message);
           }
       }
    }
    new Listener(HostedConnection source, Message message){
        if(message instanceof Ready.class){
            list.add(soucre);
       }
    }

*all the code above is theorical (not 100% working as it is).

And there’s the rub… because you never start your server in these examples. (Actually, half the code is total nonsense anyway.)

Put together a simple test case that illustrates the issues you are having and post it. Else I can’t help.

that was the exact situation in a test case. And i just did a mistake and forgot the start.